2 use std::path::PathBuf;
4 use std::time::{SystemTime, UNIX_EPOCH};
5 use time::{OffsetDateTime, format_description::well_known::Rfc2822};
6 use serde::{Serialize, Deserialize};
9 #[derive(Serialize, Deserialize)]
12 #[serde(alias = "createdOn")]
17 pub fn read_or_create(file_path: &PathBuf) -> Metadata {
18 match Metadata::read_metadata_file(file_path) {
19 Some(metadata) => metadata,
21 let timestamp = SystemTime::now()
22 .duration_since(UNIX_EPOCH)
23 .map(|duration| duration.as_millis() as u64)
24 .unwrap_or_else(|_| 0);
26 id: timestamp.to_string(),
33 pub fn created_on_utc(&self) -> Option<String> {
34 let date = OffsetDateTime::from_unix_timestamp_nanos(
35 (self.created_on * 1_000_000).into()
37 return date.format(&Rfc2822).ok();
41 fn read_metadata_file(file_path: &PathBuf) -> Option<Metadata> {
42 let mut file = File::open(file_path).ok()?;
43 let mut contents = String::new();
44 file.read_to_string(&mut contents).ok()?;
45 serde_json::from_str(&contents).ok()