From d308513c998b32b158e0a9603262d9ed0bf4a8bc Mon Sep 17 00:00:00 2001 From: Nikita Galaiko Date: Sun, 3 Dec 2023 16:40:48 +0100 Subject: [PATCH] include created into file metadata --- impl/src/lib.rs | 6 +++++- readme.md | 1 + tests/metadata.rs | 10 ++++++++++ utils/src/lib.rs | 22 ++++++++++++++++++++-- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/impl/src/lib.rs b/impl/src/lib.rs index 62235ac..47182aa 100644 --- a/impl/src/lib.rs +++ b/impl/src/lib.rs @@ -209,6 +209,10 @@ fn embed_file(folder_path: Option<&str>, ident: &syn::Ident, rel_path: &str, ful Some(last_modified) => quote! { Some(#last_modified) }, None => quote! { None }, }; + let created = match file.metadata.created() { + Some(created) => quote! { Some(#created) }, + None => quote! { None }, + }; #[cfg(feature = "mime-guess")] let mimetype_tokens = { let mt = file.metadata.mimetype(); @@ -241,7 +245,7 @@ fn embed_file(folder_path: Option<&str>, ident: &syn::Ident, rel_path: &str, ful rust_embed::EmbeddedFile { data: std::borrow::Cow::Borrowed(&BYTES), - metadata: rust_embed::Metadata::__rust_embed_new([#(#hash),*], #last_modified #mimetype_tokens) + metadata: rust_embed::Metadata::__rust_embed_new([#(#hash),*], #last_modified, #created #mimetype_tokens) } } }) diff --git a/readme.md b/readme.md index eedf3de..4940ab5 100644 --- a/readme.md +++ b/readme.md @@ -55,6 +55,7 @@ pub struct EmbeddedFile { pub struct Metadata { hash: [u8; 32], last_modified: Option, + created: Option, } ``` diff --git a/tests/metadata.rs b/tests/metadata.rs index 66326a5..30bddf9 100644 --- a/tests/metadata.rs +++ b/tests/metadata.rs @@ -25,3 +25,13 @@ fn last_modified_is_accurate() { assert_eq!(index_file.metadata.last_modified(), Some(expected_datetime_utc)); } + +#[test] +fn create_is_accurate() { + let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists"); + + let metadata = fs::metadata(format!("{}/examples/public/index.html", env!("CARGO_MANIFEST_DIR"))).unwrap(); + let expected_datetime_utc = metadata.created().unwrap().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs(); + + assert_eq!(index_file.metadata.created(), Some(expected_datetime_utc)); +} diff --git a/utils/src/lib.rs b/utils/src/lib.rs index 899f109..a363b08 100644 --- a/utils/src/lib.rs +++ b/utils/src/lib.rs @@ -89,16 +89,20 @@ pub struct EmbeddedFile { pub struct Metadata { hash: [u8; 32], last_modified: Option, + created: Option, #[cfg(feature = "mime-guess")] mimetype: Cow<'static, str>, } impl Metadata { #[doc(hidden)] - pub const fn __rust_embed_new(hash: [u8; 32], last_modified: Option, #[cfg(feature = "mime-guess")] mimetype: &'static str) -> Self { + pub const fn __rust_embed_new( + hash: [u8; 32], last_modified: Option, created: Option, #[cfg(feature = "mime-guess")] mimetype: &'static str, + ) -> Self { Self { hash, last_modified, + created, #[cfg(feature = "mime-guess")] mimetype: Cow::Borrowed(mimetype), } @@ -115,6 +119,12 @@ impl Metadata { self.last_modified } + /// The created data in seconds since the UNIX epoch. If the underlying + /// platform/file-system does not support this, None is returned. + pub fn created(&self) -> Option { + self.created + } + /// The mime type of the file #[cfg(feature = "mime-guess")] pub fn mimetype(&self) -> &str { @@ -135,12 +145,19 @@ pub fn read_file_from_fs(file_path: &Path) -> io::Result { Err(_) => None, }; - let last_modified = fs::metadata(file_path)?.modified().ok().map(|last_modified| { + let metadata = fs::metadata(file_path)?; + let last_modified = metadata.modified().ok().map(|last_modified| { last_modified .duration_since(SystemTime::UNIX_EPOCH) .expect("Time before the UNIX epoch is unsupported") .as_secs() }); + let created = metadata.created().ok().map(|created| { + created + .duration_since(SystemTime::UNIX_EPOCH) + .expect("Time before the UNIX epoch is unsupported") + .as_secs() + }); #[cfg(feature = "mime-guess")] let mimetype = mime_guess::from_path(file_path).first_or_octet_stream().to_string(); @@ -150,6 +167,7 @@ pub fn read_file_from_fs(file_path: &Path) -> io::Result { metadata: Metadata { hash, last_modified: source_date_epoch.or(last_modified), + created: source_date_epoch.or(created), #[cfg(feature = "mime-guess")] mimetype: mimetype.into(), },