From 6bed3e6cd43700a1dcedfa473abafe305fe14ece Mon Sep 17 00:00:00 2001 From: Hunter Wittenborn Date: Thu, 28 Dec 2023 12:57:23 -0600 Subject: [PATCH 1/2] Fix naming collisions in macros Previously the macro code wasn't using absolute paths in the code it generated, which could result in naming collisions from items the caller had in scope. This fixes such by making imports have absolute paths. Currently the 'rust_embed' crate isn't absolutely scoped, as I'm unaware of a way to handle that reliably in proc macros (i.e. when the user renames the 'rust_embed' crate in their Cargo.toml). If that should be addressed in this PR I'm more than open to doing such, but even with this in itself it's still good progress compared to what was present before. Closes #229. --- impl/src/lib.rs | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/impl/src/lib.rs b/impl/src/lib.rs index 47182aa..042abef 100644 --- a/impl/src/lib.rs +++ b/impl/src/lib.rs @@ -73,7 +73,7 @@ fn embedded( #not_debug_attr impl #ident { /// Get an embedded file and its metadata. - pub fn get(file_path: &str) -> Option { + pub fn get(file_path: &str) -> ::std::option::Option { #handle_prefix let key = file_path.replace("\\", "/"); const ENTRIES: &'static [(&'static str, #value_type)] = &[ @@ -83,20 +83,20 @@ fn embedded( } - fn names() -> std::slice::Iter<'static, &'static str> { + fn names() -> ::std::slice::Iter<'static, &'static str> { const ITEMS: [&str; #array_len] = [#(#list_values),*]; ITEMS.iter() } /// Iterates over the file paths in the folder. - pub fn iter() -> impl Iterator> { - Self::names().map(|x| std::borrow::Cow::from(*x)) + pub fn iter() -> impl ::std::iter::Iterator> { + Self::names().map(|x| ::std::borrow::Cow::from(*x)) } } #not_debug_attr impl rust_embed::RustEmbed for #ident { - fn get(file_path: &str) -> Option { + fn get(file_path: &str) -> ::std::option::Option { #ident::get(file_path) } fn iter() -> rust_embed::Filenames { @@ -107,13 +107,13 @@ fn embedded( } fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includes: &[String], excludes: &[String]) -> TokenStream2 { - let (handle_prefix, map_iter) = if let Some(prefix) = prefix { + let (handle_prefix, map_iter) = if let ::std::option::Option::Some(prefix) = prefix { ( quote! { let file_path = file_path.strip_prefix(#prefix)?; }, - quote! { std::borrow::Cow::Owned(format!("{}{}", #prefix, e.rel_path)) }, + quote! { ::std::borrow::Cow::Owned(format!("{}{}", #prefix, e.rel_path)) }, ) } else { - (TokenStream2::new(), quote! { std::borrow::Cow::from(e.rel_path) }) + (TokenStream2::new(), quote! { ::std::borrow::Cow::from(e.rel_path) }) }; let declare_includes = quote! { @@ -131,49 +131,49 @@ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includ #[cfg(debug_assertions)] impl #ident { /// Get an embedded file and its metadata. - pub fn get(file_path: &str) -> Option { + pub fn get(file_path: &str) -> ::std::option::Option { #handle_prefix #declare_includes #declare_excludes let rel_file_path = file_path.replace("\\", "/"); - let file_path = std::path::Path::new(#folder_path).join(&rel_file_path); + let file_path = ::std::path::Path::new(#folder_path).join(&rel_file_path); // Make sure the path requested does not escape the folder path let canonical_file_path = file_path.canonicalize().ok()?; if !canonical_file_path.starts_with(#canonical_folder_path) { // Tried to request a path that is not in the embedded folder - return None; + return ::std::option::Option::None; } if rust_embed::utils::is_path_included(&rel_file_path, INCLUDES, EXCLUDES) { rust_embed::utils::read_file_from_fs(&canonical_file_path).ok() } else { - None + ::std::option::Option::None } } /// Iterates over the file paths in the folder. - pub fn iter() -> impl Iterator> { - use std::path::Path; + pub fn iter() -> impl ::std::iter::Iterator> { + use ::std::path::Path; #declare_includes #declare_excludes - rust_embed::utils::get_files(String::from(#folder_path), INCLUDES, EXCLUDES) + rust_embed::utils::get_files(::std::string::String::from(#folder_path), INCLUDES, EXCLUDES) .map(|e| #map_iter) } } #[cfg(debug_assertions)] impl rust_embed::RustEmbed for #ident { - fn get(file_path: &str) -> Option { + fn get(file_path: &str) -> ::std::option::Option { #ident::get(file_path) } fn iter() -> rust_embed::Filenames { // the return type of iter() is unnamable, so we have to box it - rust_embed::Filenames::Dynamic(Box::new(#ident::iter())) + rust_embed::Filenames::Dynamic(::std::boxed::Box::new(#ident::iter())) } } } @@ -206,12 +206,12 @@ fn embed_file(folder_path: Option<&str>, ident: &syn::Ident, rel_path: &str, ful let file = rust_embed_utils::read_file_from_fs(Path::new(full_canonical_path)).expect("File should be readable"); let hash = file.metadata.sha256_hash(); let last_modified = match file.metadata.last_modified() { - Some(last_modified) => quote! { Some(#last_modified) }, - None => quote! { None }, + Some(last_modified) => quote! { ::std::option::Option::Some(#last_modified) }, + None => quote! { ::std::option::Option::None }, }; let created = match file.metadata.created() { - Some(created) => quote! { Some(#created) }, - None => quote! { None }, + Some(created) => quote! { ::std::option::Option::Some(#created) }, + None => quote! { ::std::option::Option::None }, }; #[cfg(feature = "mime-guess")] let mimetype_tokens = { @@ -244,7 +244,7 @@ fn embed_file(folder_path: Option<&str>, ident: &syn::Ident, rel_path: &str, ful #embedding_code rust_embed::EmbeddedFile { - data: std::borrow::Cow::Borrowed(&BYTES), + data: ::std::borrow::Cow::Borrowed(&BYTES), metadata: rust_embed::Metadata::__rust_embed_new([#(#hash),*], #last_modified, #created #mimetype_tokens) } } From 75a0093b913ed26ee13e7a7145c8c744eed64e5d Mon Sep 17 00:00:00 2001 From: Hunter Wittenborn Date: Thu, 28 Dec 2023 13:02:07 -0600 Subject: [PATCH 2/2] Fix clippy lints --- impl/src/lib.rs | 5 +---- utils/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/impl/src/lib.rs b/impl/src/lib.rs index 042abef..114456b 100644 --- a/impl/src/lib.rs +++ b/impl/src/lib.rs @@ -25,10 +25,7 @@ fn embedded( let includes: Vec<&str> = includes.iter().map(AsRef::as_ref).collect(); let excludes: Vec<&str> = excludes.iter().map(AsRef::as_ref).collect(); for rust_embed_utils::FileEntry { rel_path, full_canonical_path } in rust_embed_utils::get_files(absolute_folder_path.clone(), &includes, &excludes) { - match_values.insert( - rel_path.clone(), - embed_file(relative_folder_path.clone(), ident, &rel_path, &full_canonical_path)?, - ); + match_values.insert(rel_path.clone(), embed_file(relative_folder_path, ident, &rel_path, &full_canonical_path)?); list_values.push(if let Some(prefix) = prefix { format!("{}{}", prefix, rel_path) diff --git a/utils/src/lib.rs b/utils/src/lib.rs index a363b08..4c7a5c3 100644 --- a/utils/src/lib.rs +++ b/utils/src/lib.rs @@ -141,7 +141,7 @@ pub fn read_file_from_fs(file_path: &Path) -> io::Result { let hash: [u8; 32] = hasher.finalize().into(); let source_date_epoch = match std::env::var("SOURCE_DATE_EPOCH") { - Ok(value) => value.parse::().map_or(None, |v| Some(v)), + Ok(value) => value.parse::().ok(), Err(_) => None, };