[librsvg/rustify-rsvg-convert: 6/14] rsvg-convert: Set PDF creation date from environment variable




commit 2439cf751d9962d34cea8963f42a762d48487a00
Author: Sven Neumann <sven svenfoo org>
Date:   Tue Nov 3 00:26:35 2020 +0100

    rsvg-convert: Set PDF creation date from environment variable
    
    This adds a dependency on chrono, which should be revisited as it's
    only needed by the rsvg-convert binary.

 Cargo.toml                      |  3 ++-
 src/bin/rsvg-convert/main.rs    |  1 +
 src/bin/rsvg-convert/surface.rs | 26 ++++++++++++++++++++++++++
 3 files changed, 29 insertions(+), 1 deletion(-)
---
diff --git a/Cargo.toml b/Cargo.toml
index 47b3470e..94abf3ac 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -16,7 +16,8 @@ bitflags = "1.0"
 cairo-rs = { version="0.8.0", features=["v1_16", "png", "pdf", "ps", "svg"] }
 cairo-sys-rs = "0.9.0"
 cast = "0.2.3"
-clap = "~2.33.0"
+chrono = "0.4.0" # rsvg-convert
+clap = "~2.33.0" # rsvg-convert
 cssparser = "0.27.1"
 data-url = "0.1"
 encoding = "0.2.33"
diff --git a/src/bin/rsvg-convert/main.rs b/src/bin/rsvg-convert/main.rs
index cf21a830..5ca17134 100644
--- a/src/bin/rsvg-convert/main.rs
+++ b/src/bin/rsvg-convert/main.rs
@@ -13,6 +13,7 @@ use crate::cli::Args;
 use crate::output::Stream;
 use crate::surface::Surface;
 
+#[macro_export]
 macro_rules! exit {
     () => (exit!("Error"));
     ($($arg:tt)*) => ({
diff --git a/src/bin/rsvg-convert/surface.rs b/src/bin/rsvg-convert/surface.rs
index 616fc6ad..b99f11d2 100644
--- a/src/bin/rsvg-convert/surface.rs
+++ b/src/bin/rsvg-convert/surface.rs
@@ -50,6 +50,9 @@ impl Surface {
 
     fn new_for_pdf(width: f64, height: f64, stream: Stream) -> Result<Self, cairo::Status> {
         let surface = cairo::PdfSurface::for_stream(width, height, stream)?;
+        if let Some(date) = metadata::creation_date() {
+            surface.set_metadata(cairo::PdfMetadata::CreateDate, &date)?;
+        }
         Ok(Self::Pdf(surface, (width, height)))
     }
 
@@ -122,3 +125,26 @@ impl Surface {
         }
     }
 }
+
+mod metadata {
+    use chrono::prelude::*;
+    use std::env;
+    use std::str::FromStr;
+
+    use super::super::exit;
+
+    pub fn creation_date() -> Option<String> {
+        match env::var("SOURCE_DATE_EPOCH") {
+            Ok(epoch) => {
+                let seconds = i64::from_str(&epoch)
+                    .unwrap_or_else(|e| exit!("Environment variable $SOURCE_DATE_EPOCH: {}", e));
+                let datetime = Utc.timestamp(seconds, 0);
+                Some(datetime.to_rfc3339())
+            }
+            Err(env::VarError::NotPresent) => None,
+            Err(env::VarError::NotUnicode(_)) => {
+                exit!("Environment variable $SOURCE_DATE_EPOCH is not valid Unicode")
+            }
+        }
+    }
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]