diff --git a/src/build.rs b/src/build.rs index 62778b1..121d067 100644 --- a/src/build.rs +++ b/src/build.rs @@ -1,5 +1,6 @@ +use crate::date_time::DEFINE_SOURCE_DATE_EPOCH; use crate::hook::HookExt; -use crate::{default_deny, SdResult, Shadow}; +use crate::{default_deny, SdResult, Shadow, DEFINE_SHADOW_RS}; use is_debug::is_debug; use std::collections::BTreeSet; use std::fmt::{Display, Formatter}; @@ -128,8 +129,8 @@ impl BuildPattern { } other_keys.for_each(|key| println!("cargo:rerun-if-env-changed={key}")); - println!("cargo:rerun-if-env-changed=SOURCE_DATE_EPOCH"); - println!("cargo:rerun-if-changed={}/shadow.rs", out_dir); + println!("cargo:rerun-if-env-changed={}", DEFINE_SOURCE_DATE_EPOCH); + println!("cargo:rerun-if-changed={}/{}", out_dir, DEFINE_SHADOW_RS); } } diff --git a/src/date_time.rs b/src/date_time.rs index 4de68f8..6206fff 100644 --- a/src/date_time.rs +++ b/src/date_time.rs @@ -10,19 +10,28 @@ pub enum DateTime { Utc(OffsetDateTime), } +pub(crate) const DEFINE_SOURCE_DATE_EPOCH: &str = "SOURCE_DATE_EPOCH"; + pub fn now_date_time() -> DateTime { // Enable reproducibility for uses of `now_date_time` by respecting the // `SOURCE_DATE_EPOCH` env variable. // // https://reproducible-builds.org/docs/source-date-epoch/ - match std::env::var_os("SOURCE_DATE_EPOCH") { + match std::env::var_os(DEFINE_SOURCE_DATE_EPOCH) { None => DateTime::now(), Some(timestamp) => { let epoch = timestamp .into_string() - .expect("Input SOURCE_DATE_EPOCH could not be parsed") + .unwrap_or_else(|_| { + panic!("Input {} could not be parsed", DEFINE_SOURCE_DATE_EPOCH) + }) .parse::() - .expect("Input SOURCE_DATE_EPOCH could not be cast to a number"); + .unwrap_or_else(|_| { + panic!( + "Input {} could not be cast to a number", + DEFINE_SOURCE_DATE_EPOCH + ) + }); DateTime::Utc(OffsetDateTime::from_unix_timestamp(epoch).unwrap()) } } @@ -177,7 +186,7 @@ mod tests { #[test] fn test_source_date_epoch() { - std::env::set_var("SOURCE_DATE_EPOCH", "1628080443"); + std::env::set_var(DEFINE_SOURCE_DATE_EPOCH, "1628080443"); let time = now_date_time(); assert_eq!(time.human_format(), "2021-08-04 12:34:03 +00:00"); } diff --git a/src/lib.rs b/src/lib.rs index f0f0c1c..93a123e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -189,7 +189,7 @@ pub trait Format { fn human_format(&self) -> String; } -const SHADOW_RS: &str = "shadow.rs"; +const DEFINE_SHADOW_RS: &str = "shadow.rs"; pub const CARGO_CLIPPY_ALLOW_ALL: &str = "#[allow(clippy::all, clippy::pedantic, clippy::restriction, clippy::nursery)]"; @@ -469,9 +469,9 @@ impl Shadow { let out = { let path = Path::new(out_path); if !out_path.ends_with('/') { - path.join(format!("{out_path}/{SHADOW_RS}")) + path.join(format!("{out_path}/{DEFINE_SHADOW_RS}")) } else { - path.join(SHADOW_RS) + path.join(DEFINE_SHADOW_RS) } }; @@ -690,7 +690,7 @@ mod tests { .src_path("./") .out_path("./") .build()?; - let shadow = fs::read_to_string("./shadow.rs")?; + let shadow = fs::read_to_string(DEFINE_SHADOW_RS)?; assert!(!shadow.is_empty()); assert!(shadow.lines().count() > 0); Ok(()) @@ -704,7 +704,7 @@ mod tests { .deny_const(BTreeSet::from([CARGO_TREE])) .build()?; - let shadow = fs::read_to_string("./shadow.rs")?; + let shadow = fs::read_to_string(DEFINE_SHADOW_RS)?; assert!(!shadow.is_empty()); assert!(shadow.lines().count() > 0); // println!("{shadow}");