Skip to content

Commit

Permalink
Merge pull request #198 from baoyachi/feature/use_define
Browse files Browse the repository at this point in the history
Rename const define
  • Loading branch information
baoyachi authored Dec 31, 2024
2 parents d8d857c + 311a081 commit 15e2a23
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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);
}
}

Expand Down
17 changes: 13 additions & 4 deletions src/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i64>()
.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())
}
}
Expand Down Expand Up @@ -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");
}
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]";
Expand Down Expand Up @@ -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)
}
};

Expand Down Expand Up @@ -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(())
Expand All @@ -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}");
Expand Down

0 comments on commit 15e2a23

Please sign in to comment.