Skip to content

Commit

Permalink
chrono replaced with time, local_offset feature flag added (#110)
Browse files Browse the repository at this point in the history
* removed chrono for time, put local_offset support behind a feature flag

* docs update

* cargo fmt
  • Loading branch information
jasonozias committed Mar 9, 2022
1 parent e9efb23 commit 678bb48
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 93 deletions.
12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,31 @@ repository = "https://github.com/rustyhorde/vergen"
version = "6.0.2"

[package.metadata.cargo-all-features]
denylist = ["chrono","git2","rustc_version","sysinfo"]
denylist = ["local_offset","time","git2","rustc_version","sysinfo"]

[features]
default = ["build", "cargo", "git", "rustc", "si"]
build = ["chrono"]
build = ["time"]
cargo = []
git = ["chrono", "git2"]
git = ["time", "git2"]
rustc = ["rustc_version"]
si = ["sysinfo"]
local_offset = ["time/local-offset"]

[dependencies]
anyhow = "1"
cfg-if = "1"
chrono = { version = "0", optional = true, default-features = false }
enum-iterator = "0"
getset = "0"
git2 = { version = "0", optional = true, default-features = false }
rustc_version = { version = "0.4.0", optional = true }
sysinfo = { version = "=0.23", optional = true, default-features = false }
thiserror = "1"
time = { version = "0.3.7", optional = true }

[build-dependencies]
chrono = "0"
anyhow = "1.0.56"
time = { version = "0.3.7", features = ["formatting"] }
rustversion = "1"

[dev-dependencies]
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ Generate `build`, `git`, `rustc`, `cargo`, and `sysinfo` related [`cargo:rustc-e
Special thanks to the sponsors of this project
* [tyretool](https://github.com/tryretool)

## Release 7.0 Breaking Changes
* `chrono` has been replaced with `time`. The `time` crate is better maintained.
* The local timezone support in the `build` and `git` features has been moved behind a `local_offset` feature flag. This is
due to a [potential segfault](https://github.com/rustsec/advisory-db/blob/main/crates/time/RUSTSEC-2020-0071.md) when using `localtime_r` as the `time` crate does.
* To build with the `local_offset` feature, you must also explicitly delare that you are acknowledging the potential unsoundness by
setting `RUSTFLAGS="--cfg unsound_local_offset"`. Per the `time` docs this isn't tested, so use with extreme caution.

## Release 6.0 Breaking Changes
* The `Copy` implementation was dropped from the [`Config`](https://github.com/rustyhorde/vergen/blob/24ed6bc2269320ab98962edc8b736fcc6e3c7d64/src/config.rs#L94-L148) struct to allow the base git directory to be specified. This is a breaking API change necessitating a new major release.
## Current Release
Expand Down
10 changes: 6 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use chrono::Utc;
use anyhow::Result;
use time::{format_description::well_known::Rfc3339, OffsetDateTime};

pub fn main() {
pub fn main() -> Result<()> {
println!("cargo:rerun-if-changed=build.rs");
// These are here so some doc tests work
let now = Utc::now();
let now = OffsetDateTime::now_utc();
println!(
"cargo:rustc-env=VERGEN_BUILD_TIMESTAMP={}",
now.to_rfc3339()
now.format(&Rfc3339)?
);
println!("cargo:rustc-env=VERGEN_GIT_SEMVER=v3.2.0-86-g95fc0f5");
nightly_lints();
beta_lints();
stable_lints();
msrv_lints();
Ok(())
}

#[rustversion::nightly]
Expand Down
18 changes: 15 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ use std::{
///
/// ```
/// use vergen::Config;
#[cfg_attr(feature = "git", doc = r##"use vergen::TimeZone;"##)]
#[cfg_attr(feature = "git", doc = r##"use vergen::ShaKind;"##)]
#[cfg_attr(
all(feature = "git", feature = "local_offset"),
doc = r##"use vergen::TimeZone;"##
)]
///
/// let mut config = Config::default();
#[cfg_attr(
Expand All @@ -65,7 +69,15 @@ use std::{
#[cfg_attr(
feature = "git",
doc = r##"
// Change the commit timestamp timezone to local
// Change the SHA output to the short variant
*config.git_mut().sha_kind_mut() = ShaKind::Short;
"##
)]
#[cfg_attr(
all(feature = "git", feature = "local_offset"),
doc = r##"
// Change the SHA output to the short variant
*config.git_mut().sha_kind_mut() = ShaKind::Short;
*config.git_mut().commit_timestamp_timezone_mut() = TimeZone::Local;
"##
)]
Expand Down Expand Up @@ -137,7 +149,7 @@ impl Instructions {
{
let mut config = Config::default();

configure_build(&self, &mut config);
configure_build(&self, &mut config)?;
configure_git(&self, repo_path, &mut config)?;
configure_rustc(&self, &mut config)?;
configure_cargo(&self, &mut config);
Expand Down
95 changes: 57 additions & 38 deletions src/feature/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
//! `vergen` build feature implementation

use crate::config::{Config, Instructions};
use anyhow::Result;
#[cfg(feature = "build")]
use {
crate::{
config::VergenKey,
feature::{add_entry, TimeZone, TimestampKind},
},
chrono::{DateTime, Local, Utc},
getset::{Getters, MutGetters},
std::env,
time::{format_description, OffsetDateTime},
};

/// Configuration for the `VERGEN_BUILD_*` instructions
Expand All @@ -42,7 +43,11 @@ use {
/// ```
/// # use anyhow::Result;
/// use vergen::{vergen, Config};
#[cfg_attr(feature = "build", doc = r##"use vergen::{TimestampKind, TimeZone};"##)]
#[cfg_attr(feature = "build", doc = r##"use vergen::TimestampKind;"##)]
#[cfg_attr(
all(feature = "build", feature = "local_offset"),
doc = r##"use vergen::TimeZone;"##
)]
///
/// # pub fn main() -> Result<()> {
/// let mut config = Config::default();
Expand All @@ -51,7 +56,17 @@ use {
doc = r##"
// Generate all three date/time instructions
*config.build_mut().kind_mut() = TimestampKind::All;
// Change the date/time instructions to show `Local` time
// Generate the instructions
vergen(config)?;
"##
)]
#[cfg_attr(
all(feature = "build", feature = "local_offset"),
doc = r##"
// Generate all three date/time instructions
*config.build_mut().kind_mut() = TimestampKind::All;
// Generate the output time in the local timezone
*config.build_mut().timezone_mut() = TimeZone::Local;
// Generate the instructions
Expand Down Expand Up @@ -97,14 +112,19 @@ impl Build {
}

#[cfg(feature = "build")]
pub(crate) fn configure_build(instructions: &Instructions, config: &mut Config) {
pub(crate) fn configure_build(instructions: &Instructions, config: &mut Config) -> Result<()> {
let build_config = instructions.build();

if build_config.has_enabled() {
if *build_config.timestamp() {
match build_config.timezone() {
TimeZone::Utc => add_config_entries(config, *build_config, &Utc::now()),
TimeZone::Local => add_config_entries(config, *build_config, &Local::now()),
TimeZone::Utc => {
add_config_entries(config, *build_config, &OffsetDateTime::now_utc())?;
}
#[cfg(feature = "local_offset")]
TimeZone::Local => {
add_config_entries(config, *build_config, &OffsetDateTime::now_local()?)?;
}
};
}

Expand All @@ -116,71 +136,70 @@ pub(crate) fn configure_build(instructions: &Instructions, config: &mut Config)
);
}
}
Ok(())
}

#[cfg(feature = "build")]
fn add_config_entries<T>(config: &mut Config, build_config: Build, now: &DateTime<T>)
where
T: chrono::TimeZone,
T::Offset: std::fmt::Display,
{
fn add_config_entries(
config: &mut Config,
build_config: Build,
now: &OffsetDateTime,
) -> Result<()> {
match build_config.kind() {
TimestampKind::DateOnly => add_date_entry(config, now),
TimestampKind::TimeOnly => add_time_entry(config, now),
TimestampKind::DateOnly => add_date_entry(config, now)?,
TimestampKind::TimeOnly => add_time_entry(config, now)?,
TimestampKind::DateAndTime => {
add_date_entry(config, now);
add_time_entry(config, now);
add_date_entry(config, now)?;
add_time_entry(config, now)?;
}
TimestampKind::Timestamp => add_timestamp_entry(config, now),
TimestampKind::Timestamp => add_timestamp_entry(config, now)?,
TimestampKind::All => {
add_date_entry(config, now);
add_time_entry(config, now);
add_timestamp_entry(config, now);
add_date_entry(config, now)?;
add_time_entry(config, now)?;
add_timestamp_entry(config, now)?;
}
}
Ok(())
}

#[cfg(feature = "build")]
fn add_date_entry<T>(config: &mut Config, now: &DateTime<T>)
where
T: chrono::TimeZone,
T::Offset: std::fmt::Display,
{
fn add_date_entry(config: &mut Config, now: &OffsetDateTime) -> Result<()> {
let format = format_description::parse("[year]-[month]-[day]")?;
add_entry(
config.cfg_map_mut(),
VergenKey::BuildDate,
Some(now.format("%Y-%m-%d").to_string()),
Some(now.format(&format)?),
);
Ok(())
}

#[cfg(feature = "build")]
fn add_time_entry<T>(config: &mut Config, now: &DateTime<T>)
where
T: chrono::TimeZone,
T::Offset: std::fmt::Display,
{
fn add_time_entry(config: &mut Config, now: &OffsetDateTime) -> Result<()> {
let format = format_description::parse("[hour]:[minute]:[second]")?;
add_entry(
config.cfg_map_mut(),
VergenKey::BuildTime,
Some(now.format("%H:%M:%S").to_string()),
Some(now.format(&format)?),
);
Ok(())
}

#[cfg(feature = "build")]
fn add_timestamp_entry<T>(config: &mut Config, now: &DateTime<T>)
where
T: chrono::TimeZone,
T::Offset: std::fmt::Display,
{
fn add_timestamp_entry(config: &mut Config, now: &OffsetDateTime) -> Result<()> {
use time::format_description::well_known::Rfc3339;

add_entry(
config.cfg_map_mut(),
VergenKey::BuildTimestamp,
Some(now.to_rfc3339()),
Some(now.format(&Rfc3339)?),
);
Ok(())
}

#[cfg(not(feature = "build"))]
pub(crate) fn configure_build(_instructions: &Instructions, _config: &mut Config) {}
pub(crate) fn configure_build(_instructions: &Instructions, _config: &mut Config) -> Result<()> {
Ok(())
}

#[cfg(all(test, feature = "build"))]
mod test {
Expand Down
Loading

0 comments on commit 678bb48

Please sign in to comment.