X-Git-Url: https://git.r.bdr.sh/rbdr/blog/blobdiff_plain/d7fef30ac3f539975ef9edbba8e0af4a4e9ff3de..b17907faf8d9693cef94a6048d802bd4ced9102f:/src/metadata.rs?ds=sidebyside diff --git a/src/metadata.rs b/src/metadata.rs index e016db6..fe657e4 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -14,17 +14,16 @@ pub struct Metadata { impl Metadata { pub fn read_or_create(file_path: &PathBuf) -> Metadata { - match Metadata::read_metadata_file(file_path) { - Some(metadata) => metadata, - None => { - let timestamp = SystemTime::now() - .duration_since(UNIX_EPOCH) - .map(|duration| duration.as_millis() as u64) - .unwrap_or_else(|_| 0); - return Metadata { - id: timestamp.to_string(), - created_on: timestamp, - }; + if let Some(metadata) = Metadata::read_metadata_file(file_path) { + metadata + } else { + let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).map_or_else( + |_| 0, + |duration| u64::try_from(duration.as_millis()).expect("Timestamp is too big!"), + ); + Metadata { + id: timestamp.to_string(), + created_on: timestamp, } } } @@ -32,7 +31,7 @@ impl Metadata { pub fn created_on_utc(&self) -> Option { let date = OffsetDateTime::from_unix_timestamp_nanos((self.created_on * 1_000_000).into()).ok()?; - return date.format(&Rfc2822).ok(); + date.format(&Rfc2822).ok() } fn read_metadata_file(file_path: &PathBuf) -> Option {