Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit 786d018

Browse files
authored
Merge pull request #230 from hwittenborn/naming-collision
Fix naming collisions in macros
2 parents 253340f + 75a0093 commit 786d018

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

impl/src/lib.rs

+23-26
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ fn embedded(
2525
let includes: Vec<&str> = includes.iter().map(AsRef::as_ref).collect();
2626
let excludes: Vec<&str> = excludes.iter().map(AsRef::as_ref).collect();
2727
for rust_embed_utils::FileEntry { rel_path, full_canonical_path } in rust_embed_utils::get_files(absolute_folder_path.clone(), &includes, &excludes) {
28-
match_values.insert(
29-
rel_path.clone(),
30-
embed_file(relative_folder_path.clone(), ident, &rel_path, &full_canonical_path)?,
31-
);
28+
match_values.insert(rel_path.clone(), embed_file(relative_folder_path, ident, &rel_path, &full_canonical_path)?);
3229

3330
list_values.push(if let Some(prefix) = prefix {
3431
format!("{}{}", prefix, rel_path)
@@ -73,7 +70,7 @@ fn embedded(
7370
#not_debug_attr
7471
impl #ident {
7572
/// Get an embedded file and its metadata.
76-
pub fn get(file_path: &str) -> Option<rust_embed::EmbeddedFile> {
73+
pub fn get(file_path: &str) -> ::std::option::Option<rust_embed::EmbeddedFile> {
7774
#handle_prefix
7875
let key = file_path.replace("\\", "/");
7976
const ENTRIES: &'static [(&'static str, #value_type)] = &[
@@ -83,20 +80,20 @@ fn embedded(
8380

8481
}
8582

86-
fn names() -> std::slice::Iter<'static, &'static str> {
83+
fn names() -> ::std::slice::Iter<'static, &'static str> {
8784
const ITEMS: [&str; #array_len] = [#(#list_values),*];
8885
ITEMS.iter()
8986
}
9087

9188
/// Iterates over the file paths in the folder.
92-
pub fn iter() -> impl Iterator<Item = std::borrow::Cow<'static, str>> {
93-
Self::names().map(|x| std::borrow::Cow::from(*x))
89+
pub fn iter() -> impl ::std::iter::Iterator<Item = ::std::borrow::Cow<'static, str>> {
90+
Self::names().map(|x| ::std::borrow::Cow::from(*x))
9491
}
9592
}
9693

9794
#not_debug_attr
9895
impl rust_embed::RustEmbed for #ident {
99-
fn get(file_path: &str) -> Option<rust_embed::EmbeddedFile> {
96+
fn get(file_path: &str) -> ::std::option::Option<rust_embed::EmbeddedFile> {
10097
#ident::get(file_path)
10198
}
10299
fn iter() -> rust_embed::Filenames {
@@ -107,13 +104,13 @@ fn embedded(
107104
}
108105

109106
fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includes: &[String], excludes: &[String]) -> TokenStream2 {
110-
let (handle_prefix, map_iter) = if let Some(prefix) = prefix {
107+
let (handle_prefix, map_iter) = if let ::std::option::Option::Some(prefix) = prefix {
111108
(
112109
quote! { let file_path = file_path.strip_prefix(#prefix)?; },
113-
quote! { std::borrow::Cow::Owned(format!("{}{}", #prefix, e.rel_path)) },
110+
quote! { ::std::borrow::Cow::Owned(format!("{}{}", #prefix, e.rel_path)) },
114111
)
115112
} else {
116-
(TokenStream2::new(), quote! { std::borrow::Cow::from(e.rel_path) })
113+
(TokenStream2::new(), quote! { ::std::borrow::Cow::from(e.rel_path) })
117114
};
118115

119116
let declare_includes = quote! {
@@ -131,49 +128,49 @@ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includ
131128
#[cfg(debug_assertions)]
132129
impl #ident {
133130
/// Get an embedded file and its metadata.
134-
pub fn get(file_path: &str) -> Option<rust_embed::EmbeddedFile> {
131+
pub fn get(file_path: &str) -> ::std::option::Option<rust_embed::EmbeddedFile> {
135132
#handle_prefix
136133

137134
#declare_includes
138135
#declare_excludes
139136

140137
let rel_file_path = file_path.replace("\\", "/");
141-
let file_path = std::path::Path::new(#folder_path).join(&rel_file_path);
138+
let file_path = ::std::path::Path::new(#folder_path).join(&rel_file_path);
142139

143140
// Make sure the path requested does not escape the folder path
144141
let canonical_file_path = file_path.canonicalize().ok()?;
145142
if !canonical_file_path.starts_with(#canonical_folder_path) {
146143
// Tried to request a path that is not in the embedded folder
147-
return None;
144+
return ::std::option::Option::None;
148145
}
149146

150147
if rust_embed::utils::is_path_included(&rel_file_path, INCLUDES, EXCLUDES) {
151148
rust_embed::utils::read_file_from_fs(&canonical_file_path).ok()
152149
} else {
153-
None
150+
::std::option::Option::None
154151
}
155152
}
156153

157154
/// Iterates over the file paths in the folder.
158-
pub fn iter() -> impl Iterator<Item = std::borrow::Cow<'static, str>> {
159-
use std::path::Path;
155+
pub fn iter() -> impl ::std::iter::Iterator<Item = ::std::borrow::Cow<'static, str>> {
156+
use ::std::path::Path;
160157

161158
#declare_includes
162159
#declare_excludes
163160

164-
rust_embed::utils::get_files(String::from(#folder_path), INCLUDES, EXCLUDES)
161+
rust_embed::utils::get_files(::std::string::String::from(#folder_path), INCLUDES, EXCLUDES)
165162
.map(|e| #map_iter)
166163
}
167164
}
168165

169166
#[cfg(debug_assertions)]
170167
impl rust_embed::RustEmbed for #ident {
171-
fn get(file_path: &str) -> Option<rust_embed::EmbeddedFile> {
168+
fn get(file_path: &str) -> ::std::option::Option<rust_embed::EmbeddedFile> {
172169
#ident::get(file_path)
173170
}
174171
fn iter() -> rust_embed::Filenames {
175172
// the return type of iter() is unnamable, so we have to box it
176-
rust_embed::Filenames::Dynamic(Box::new(#ident::iter()))
173+
rust_embed::Filenames::Dynamic(::std::boxed::Box::new(#ident::iter()))
177174
}
178175
}
179176
}
@@ -206,12 +203,12 @@ fn embed_file(folder_path: Option<&str>, ident: &syn::Ident, rel_path: &str, ful
206203
let file = rust_embed_utils::read_file_from_fs(Path::new(full_canonical_path)).expect("File should be readable");
207204
let hash = file.metadata.sha256_hash();
208205
let last_modified = match file.metadata.last_modified() {
209-
Some(last_modified) => quote! { Some(#last_modified) },
210-
None => quote! { None },
206+
Some(last_modified) => quote! { ::std::option::Option::Some(#last_modified) },
207+
None => quote! { ::std::option::Option::None },
211208
};
212209
let created = match file.metadata.created() {
213-
Some(created) => quote! { Some(#created) },
214-
None => quote! { None },
210+
Some(created) => quote! { ::std::option::Option::Some(#created) },
211+
None => quote! { ::std::option::Option::None },
215212
};
216213
#[cfg(feature = "mime-guess")]
217214
let mimetype_tokens = {
@@ -244,7 +241,7 @@ fn embed_file(folder_path: Option<&str>, ident: &syn::Ident, rel_path: &str, ful
244241
#embedding_code
245242

246243
rust_embed::EmbeddedFile {
247-
data: std::borrow::Cow::Borrowed(&BYTES),
244+
data: ::std::borrow::Cow::Borrowed(&BYTES),
248245
metadata: rust_embed::Metadata::__rust_embed_new([#(#hash),*], #last_modified, #created #mimetype_tokens)
249246
}
250247
}

utils/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub fn read_file_from_fs(file_path: &Path) -> io::Result<EmbeddedFile> {
141141
let hash: [u8; 32] = hasher.finalize().into();
142142

143143
let source_date_epoch = match std::env::var("SOURCE_DATE_EPOCH") {
144-
Ok(value) => value.parse::<u64>().map_or(None, |v| Some(v)),
144+
Ok(value) => value.parse::<u64>().ok(),
145145
Err(_) => None,
146146
};
147147

0 commit comments

Comments
 (0)