From c8e7cd5704f2ddb5ea61d79b36f44ebeabe476d3 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Tue, 23 Jul 2024 01:53:44 +0800 Subject: [PATCH 1/3] Fix compilation failures caused by unwrap --- Cargo.toml | 2 +- src/date_time.rs | 15 ++++++++------- src/git.rs | 26 ++++++++++++++------------ 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c50ffbd..12a6e9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shadow-rs" -version = "0.30.0" +version = "0.31.0" authors = ["baoyachi "] edition = "2021" description = "A build-time information stored in your rust project" diff --git a/src/date_time.rs b/src/date_time.rs index 1e399bc..003b15c 100644 --- a/src/date_time.rs +++ b/src/date_time.rs @@ -1,4 +1,4 @@ -use crate::Format; +use crate::{Format, SdResult, ShadowError}; use std::error::Error; use time::format_description::well_known::{Rfc2822, Rfc3339}; #[cfg(feature = "tzdb")] @@ -67,20 +67,21 @@ impl DateTime { Ok(DateTime::Local(local_date_time)) } - pub fn timestamp_2_utc(time_stamp: i64) -> Self { - let time = OffsetDateTime::from_unix_timestamp(time_stamp).unwrap(); - DateTime::Utc(time) + pub fn timestamp_2_utc(time_stamp: i64) -> SdResult { + let time = + OffsetDateTime::from_unix_timestamp(time_stamp).map_err(|err| ShadowError::new(err))?; + Ok(DateTime::Utc(time)) } pub fn to_rfc2822(&self) -> String { match self { - DateTime::Local(dt) | DateTime::Utc(dt) => dt.format(&Rfc2822).unwrap(), + DateTime::Local(dt) | DateTime::Utc(dt) => dt.format(&Rfc2822).unwrap_or_default(), } } pub fn to_rfc3339(&self) -> String { match self { - DateTime::Local(dt) | DateTime::Utc(dt) => dt.format(&Rfc3339).unwrap(), + DateTime::Local(dt) | DateTime::Utc(dt) => dt.format(&Rfc3339).unwrap_or_default(), } } } @@ -197,7 +198,7 @@ mod tests { #[test] fn test_timestamp_2_utc() { - let time = DateTime::timestamp_2_utc(1628080443); + let time = DateTime::timestamp_2_utc(1628080443).unwrap(); assert_eq!(time.to_rfc2822(), "Wed, 04 Aug 2021 12:34:03 +0000"); assert_eq!(time.to_rfc3339(), "2021-08-04T12:34:03Z"); assert_eq!(time.human_format(), "2021-08-04 12:34:03 +00:00"); diff --git a/src/git.rs b/src/git.rs index 37c8bfe..37e5d97 100644 --- a/src/git.rs +++ b/src/git.rs @@ -147,10 +147,11 @@ impl Git { self.update_str(COMMIT_HASH, git_info.commit); let time_stamp = git_info.date.parse::()?; - let date_time = DateTime::timestamp_2_utc(time_stamp); - self.update_str(COMMIT_DATE, date_time.human_format()); - self.update_str(COMMIT_DATE_2822, date_time.to_rfc2822()); - self.update_str(COMMIT_DATE_3339, date_time.to_rfc3339()); + if let Ok(date_time) = DateTime::timestamp_2_utc(time_stamp) { + self.update_str(COMMIT_DATE, date_time.human_format()); + self.update_str(COMMIT_DATE_2822, date_time.to_rfc2822()); + self.update_str(COMMIT_DATE_3339, date_time.to_rfc3339()); + } Ok(()) } @@ -193,14 +194,6 @@ impl Git { let commit = reference.peel_to_commit().map_err(ShadowError::new)?; - let time_stamp = commit.time().seconds().to_string().parse::()?; - let date_time = DateTime::timestamp_2_utc(time_stamp); - self.update_str(COMMIT_DATE, date_time.human_format()); - - self.update_str(COMMIT_DATE_2822, date_time.to_rfc2822()); - - self.update_str(COMMIT_DATE_3339, date_time.to_rfc3339()); - let author = commit.author(); if let Some(v) = author.email() { self.update_str(COMMIT_EMAIL, v.to_string()); @@ -216,6 +209,15 @@ impl Git { self.update_bool(GIT_CLEAN, false); } self.update_str(GIT_STATUS_FILE, status_file); + + let time_stamp = commit.time().seconds().to_string().parse::()?; + if let Ok(date_time) = DateTime::timestamp_2_utc(time_stamp) { + self.update_str(COMMIT_DATE, date_time.human_format()); + + self.update_str(COMMIT_DATE_2822, date_time.to_rfc2822()); + + self.update_str(COMMIT_DATE_3339, date_time.to_rfc3339()); + } } Ok(()) } From a3be8680aaf61a8a45024b178bb965c637c1c12e Mon Sep 17 00:00:00 2001 From: baoyachi Date: Tue, 23 Jul 2024 02:08:23 +0800 Subject: [PATCH 2/3] fix clippy --- src/date_time.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/date_time.rs b/src/date_time.rs index 003b15c..44bc46f 100644 --- a/src/date_time.rs +++ b/src/date_time.rs @@ -69,7 +69,7 @@ impl DateTime { pub fn timestamp_2_utc(time_stamp: i64) -> SdResult { let time = - OffsetDateTime::from_unix_timestamp(time_stamp).map_err(|err| ShadowError::new(err))?; + OffsetDateTime::from_unix_timestamp(time_stamp).map_err(ShadowError::new)?; Ok(DateTime::Utc(time)) } From dfb8b24adbbb6adc961ad033eab1ac29e7dbfd19 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Tue, 23 Jul 2024 02:09:32 +0800 Subject: [PATCH 3/3] cargo fmt --- src/date_time.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/date_time.rs b/src/date_time.rs index 44bc46f..55a7353 100644 --- a/src/date_time.rs +++ b/src/date_time.rs @@ -68,8 +68,7 @@ impl DateTime { } pub fn timestamp_2_utc(time_stamp: i64) -> SdResult { - let time = - OffsetDateTime::from_unix_timestamp(time_stamp).map_err(ShadowError::new)?; + let time = OffsetDateTime::from_unix_timestamp(time_stamp).map_err(ShadowError::new)?; Ok(DateTime::Utc(time)) }