~repos /rust-embed

#rust#proc-macro#http

git clone https://pyrossh.dev/repos/rust-embed.git
Discussions: https://groups.google.com/g/rust-embed-devs

rust macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.


Files changed (2) hide show
  1. impl/src/lib.rs +23 -26
  2. utils/src/lib.rs +1 -1
impl/src/lib.rs CHANGED
@@ -25,10 +25,7 @@ fn embedded(
25
25
  let includes: Vec<&str> = includes.iter().map(AsRef::as_ref).collect();
26
26
  let excludes: Vec<&str> = excludes.iter().map(AsRef::as_ref).collect();
27
27
  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)?,
28
+ match_values.insert(rel_path.clone(), embed_file(relative_folder_path, ident, &rel_path, &full_canonical_path)?);
31
- );
32
29
 
33
30
  list_values.push(if let Some(prefix) = prefix {
34
31
  format!("{}{}", prefix, rel_path)
@@ -73,7 +70,7 @@ fn embedded(
73
70
  #not_debug_attr
74
71
  impl #ident {
75
72
  /// 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> {
77
74
  #handle_prefix
78
75
  let key = file_path.replace("\\", "/");
79
76
  const ENTRIES: &'static [(&'static str, #value_type)] = &[
@@ -83,20 +80,20 @@ fn embedded(
83
80
 
84
81
  }
85
82
 
86
- fn names() -> std::slice::Iter<'static, &'static str> {
83
+ fn names() -> ::std::slice::Iter<'static, &'static str> {
87
84
  const ITEMS: [&str; #array_len] = [#(#list_values),*];
88
85
  ITEMS.iter()
89
86
  }
90
87
 
91
88
  /// Iterates over the file paths in the folder.
92
- pub fn iter() -> impl Iterator<Item = std::borrow::Cow<'static, str>> {
89
+ pub fn iter() -> impl ::std::iter::Iterator<Item = ::std::borrow::Cow<'static, str>> {
93
- Self::names().map(|x| std::borrow::Cow::from(*x))
90
+ Self::names().map(|x| ::std::borrow::Cow::from(*x))
94
91
  }
95
92
  }
96
93
 
97
94
  #not_debug_attr
98
95
  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> {
100
97
  #ident::get(file_path)
101
98
  }
102
99
  fn iter() -> rust_embed::Filenames {
@@ -107,13 +104,13 @@ fn embedded(
107
104
  }
108
105
 
109
106
  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 {
111
108
  (
112
109
  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)) },
114
111
  )
115
112
  } else {
116
- (TokenStream2::new(), quote! { std::borrow::Cow::from(e.rel_path) })
113
+ (TokenStream2::new(), quote! { ::std::borrow::Cow::from(e.rel_path) })
117
114
  };
118
115
 
119
116
  let declare_includes = quote! {
@@ -131,49 +128,49 @@ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includ
131
128
  #[cfg(debug_assertions)]
132
129
  impl #ident {
133
130
  /// 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> {
135
132
  #handle_prefix
136
133
 
137
134
  #declare_includes
138
135
  #declare_excludes
139
136
 
140
137
  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);
142
139
 
143
140
  // Make sure the path requested does not escape the folder path
144
141
  let canonical_file_path = file_path.canonicalize().ok()?;
145
142
  if !canonical_file_path.starts_with(#canonical_folder_path) {
146
143
  // Tried to request a path that is not in the embedded folder
147
- return None;
144
+ return ::std::option::Option::None;
148
145
  }
149
146
 
150
147
  if rust_embed::utils::is_path_included(&rel_file_path, INCLUDES, EXCLUDES) {
151
148
  rust_embed::utils::read_file_from_fs(&canonical_file_path).ok()
152
149
  } else {
153
- None
150
+ ::std::option::Option::None
154
151
  }
155
152
  }
156
153
 
157
154
  /// Iterates over the file paths in the folder.
158
- pub fn iter() -> impl Iterator<Item = std::borrow::Cow<'static, str>> {
155
+ pub fn iter() -> impl ::std::iter::Iterator<Item = ::std::borrow::Cow<'static, str>> {
159
- use std::path::Path;
156
+ use ::std::path::Path;
160
157
 
161
158
  #declare_includes
162
159
  #declare_excludes
163
160
 
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)
165
162
  .map(|e| #map_iter)
166
163
  }
167
164
  }
168
165
 
169
166
  #[cfg(debug_assertions)]
170
167
  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> {
172
169
  #ident::get(file_path)
173
170
  }
174
171
  fn iter() -> rust_embed::Filenames {
175
172
  // 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()))
177
174
  }
178
175
  }
179
176
  }
@@ -206,12 +203,12 @@ fn embed_file(folder_path: Option<&str>, ident: &syn::Ident, rel_path: &str, ful
206
203
  let file = rust_embed_utils::read_file_from_fs(Path::new(full_canonical_path)).expect("File should be readable");
207
204
  let hash = file.metadata.sha256_hash();
208
205
  let last_modified = match file.metadata.last_modified() {
209
- Some(last_modified) => quote! { Some(#last_modified) },
206
+ Some(last_modified) => quote! { ::std::option::Option::Some(#last_modified) },
210
- None => quote! { None },
207
+ None => quote! { ::std::option::Option::None },
211
208
  };
212
209
  let created = match file.metadata.created() {
213
- Some(created) => quote! { Some(#created) },
210
+ Some(created) => quote! { ::std::option::Option::Some(#created) },
214
- None => quote! { None },
211
+ None => quote! { ::std::option::Option::None },
215
212
  };
216
213
  #[cfg(feature = "mime-guess")]
217
214
  let mimetype_tokens = {
@@ -244,7 +241,7 @@ fn embed_file(folder_path: Option<&str>, ident: &syn::Ident, rel_path: &str, ful
244
241
  #embedding_code
245
242
 
246
243
  rust_embed::EmbeddedFile {
247
- data: std::borrow::Cow::Borrowed(&BYTES),
244
+ data: ::std::borrow::Cow::Borrowed(&BYTES),
248
245
  metadata: rust_embed::Metadata::__rust_embed_new([#(#hash),*], #last_modified, #created #mimetype_tokens)
249
246
  }
250
247
  }
utils/src/lib.rs CHANGED
@@ -141,7 +141,7 @@ pub fn read_file_from_fs(file_path: &Path) -> io::Result<EmbeddedFile> {
141
141
  let hash: [u8; 32] = hasher.finalize().into();
142
142
 
143
143
  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(),
145
145
  Err(_) => None,
146
146
  };
147
147