~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 (1) hide show
  1. impl/src/lib.rs +22 -22
impl/src/lib.rs CHANGED
@@ -73,7 +73,7 @@ fn embedded(
73
73
  #not_debug_attr
74
74
  impl #ident {
75
75
  /// Get an embedded file and its metadata.
76
- pub fn get(file_path: &str) -> Option<rust_embed::EmbeddedFile> {
76
+ pub fn get(file_path: &str) -> ::std::option::Option<rust_embed::EmbeddedFile> {
77
77
  #handle_prefix
78
78
  let key = file_path.replace("\\", "/");
79
79
  const ENTRIES: &'static [(&'static str, #value_type)] = &[
@@ -83,20 +83,20 @@ fn embedded(
83
83
 
84
84
  }
85
85
 
86
- fn names() -> std::slice::Iter<'static, &'static str> {
86
+ fn names() -> ::std::slice::Iter<'static, &'static str> {
87
87
  const ITEMS: [&str; #array_len] = [#(#list_values),*];
88
88
  ITEMS.iter()
89
89
  }
90
90
 
91
91
  /// Iterates over the file paths in the folder.
92
- pub fn iter() -> impl Iterator<Item = std::borrow::Cow<'static, str>> {
92
+ pub fn iter() -> impl ::std::iter::Iterator<Item = ::std::borrow::Cow<'static, str>> {
93
- Self::names().map(|x| std::borrow::Cow::from(*x))
93
+ Self::names().map(|x| ::std::borrow::Cow::from(*x))
94
94
  }
95
95
  }
96
96
 
97
97
  #not_debug_attr
98
98
  impl rust_embed::RustEmbed for #ident {
99
- fn get(file_path: &str) -> Option<rust_embed::EmbeddedFile> {
99
+ fn get(file_path: &str) -> ::std::option::Option<rust_embed::EmbeddedFile> {
100
100
  #ident::get(file_path)
101
101
  }
102
102
  fn iter() -> rust_embed::Filenames {
@@ -107,13 +107,13 @@ fn embedded(
107
107
  }
108
108
 
109
109
  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 {
110
+ let (handle_prefix, map_iter) = if let ::std::option::Option::Some(prefix) = prefix {
111
111
  (
112
112
  quote! { let file_path = file_path.strip_prefix(#prefix)?; },
113
- quote! { std::borrow::Cow::Owned(format!("{}{}", #prefix, e.rel_path)) },
113
+ quote! { ::std::borrow::Cow::Owned(format!("{}{}", #prefix, e.rel_path)) },
114
114
  )
115
115
  } else {
116
- (TokenStream2::new(), quote! { std::borrow::Cow::from(e.rel_path) })
116
+ (TokenStream2::new(), quote! { ::std::borrow::Cow::from(e.rel_path) })
117
117
  };
118
118
 
119
119
  let declare_includes = quote! {
@@ -131,49 +131,49 @@ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includ
131
131
  #[cfg(debug_assertions)]
132
132
  impl #ident {
133
133
  /// Get an embedded file and its metadata.
134
- pub fn get(file_path: &str) -> Option<rust_embed::EmbeddedFile> {
134
+ pub fn get(file_path: &str) -> ::std::option::Option<rust_embed::EmbeddedFile> {
135
135
  #handle_prefix
136
136
 
137
137
  #declare_includes
138
138
  #declare_excludes
139
139
 
140
140
  let rel_file_path = file_path.replace("\\", "/");
141
- let file_path = std::path::Path::new(#folder_path).join(&rel_file_path);
141
+ let file_path = ::std::path::Path::new(#folder_path).join(&rel_file_path);
142
142
 
143
143
  // Make sure the path requested does not escape the folder path
144
144
  let canonical_file_path = file_path.canonicalize().ok()?;
145
145
  if !canonical_file_path.starts_with(#canonical_folder_path) {
146
146
  // Tried to request a path that is not in the embedded folder
147
- return None;
147
+ return ::std::option::Option::None;
148
148
  }
149
149
 
150
150
  if rust_embed::utils::is_path_included(&rel_file_path, INCLUDES, EXCLUDES) {
151
151
  rust_embed::utils::read_file_from_fs(&canonical_file_path).ok()
152
152
  } else {
153
- None
153
+ ::std::option::Option::None
154
154
  }
155
155
  }
156
156
 
157
157
  /// Iterates over the file paths in the folder.
158
- pub fn iter() -> impl Iterator<Item = std::borrow::Cow<'static, str>> {
158
+ pub fn iter() -> impl ::std::iter::Iterator<Item = ::std::borrow::Cow<'static, str>> {
159
- use std::path::Path;
159
+ use ::std::path::Path;
160
160
 
161
161
  #declare_includes
162
162
  #declare_excludes
163
163
 
164
- rust_embed::utils::get_files(String::from(#folder_path), INCLUDES, EXCLUDES)
164
+ rust_embed::utils::get_files(::std::string::String::from(#folder_path), INCLUDES, EXCLUDES)
165
165
  .map(|e| #map_iter)
166
166
  }
167
167
  }
168
168
 
169
169
  #[cfg(debug_assertions)]
170
170
  impl rust_embed::RustEmbed for #ident {
171
- fn get(file_path: &str) -> Option<rust_embed::EmbeddedFile> {
171
+ fn get(file_path: &str) -> ::std::option::Option<rust_embed::EmbeddedFile> {
172
172
  #ident::get(file_path)
173
173
  }
174
174
  fn iter() -> rust_embed::Filenames {
175
175
  // the return type of iter() is unnamable, so we have to box it
176
- rust_embed::Filenames::Dynamic(Box::new(#ident::iter()))
176
+ rust_embed::Filenames::Dynamic(::std::boxed::Box::new(#ident::iter()))
177
177
  }
178
178
  }
179
179
  }
@@ -206,12 +206,12 @@ fn embed_file(folder_path: Option<&str>, ident: &syn::Ident, rel_path: &str, ful
206
206
  let file = rust_embed_utils::read_file_from_fs(Path::new(full_canonical_path)).expect("File should be readable");
207
207
  let hash = file.metadata.sha256_hash();
208
208
  let last_modified = match file.metadata.last_modified() {
209
- Some(last_modified) => quote! { Some(#last_modified) },
209
+ Some(last_modified) => quote! { ::std::option::Option::Some(#last_modified) },
210
- None => quote! { None },
210
+ None => quote! { ::std::option::Option::None },
211
211
  };
212
212
  let created = match file.metadata.created() {
213
- Some(created) => quote! { Some(#created) },
213
+ Some(created) => quote! { ::std::option::Option::Some(#created) },
214
- None => quote! { None },
214
+ None => quote! { ::std::option::Option::None },
215
215
  };
216
216
  #[cfg(feature = "mime-guess")]
217
217
  let mimetype_tokens = {
@@ -244,7 +244,7 @@ fn embed_file(folder_path: Option<&str>, ident: &syn::Ident, rel_path: &str, ful
244
244
  #embedding_code
245
245
 
246
246
  rust_embed::EmbeddedFile {
247
- data: std::borrow::Cow::Borrowed(&BYTES),
247
+ data: ::std::borrow::Cow::Borrowed(&BYTES),
248
248
  metadata: rust_embed::Metadata::__rust_embed_new([#(#hash),*], #last_modified, #created #mimetype_tokens)
249
249
  }
250
250
  }