~repos /rust-embed

#rust#proc-macro#http

git clone https://pyrossh.dev/repos/rust-embed.git

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


097d1878 Liran Piade

11 months ago
Add allow_missing option to derive macro
Files changed (2) hide show
  1. impl/src/lib.rs +13 -4
  2. readme.md +6 -1
impl/src/lib.rs CHANGED
@@ -10,6 +10,7 @@ use rust_embed_utils::PathMatcher;
10
10
  use std::{
11
11
  collections::BTreeMap,
12
12
  env,
13
+ io::ErrorKind,
13
14
  iter::FromIterator,
14
15
  path::{Path, PathBuf},
15
16
  };
@@ -135,7 +136,14 @@ fn dynamic(
135
136
  .map(|mut file| { file.data = ::std::default::Default::default(); file })
136
137
  });
137
138
 
139
+ let non_canonical_folder_path = Path::new(&folder_path);
140
+ let canonical_folder_path = non_canonical_folder_path
141
+ .canonicalize()
142
+ .or_else(|err| match err {
143
+ err if err.kind() == ErrorKind::NotFound => Ok(non_canonical_folder_path.to_owned()),
144
+ err => Err(err),
145
+ })
138
- let canonical_folder_path = Path::new(&folder_path).canonicalize().expect("folder path must resolve to an absolute path");
146
+ .expect("folder path must resolve to an absolute path");
139
147
  let canonical_folder_path = canonical_folder_path.to_str().expect("absolute folder path must be valid unicode");
140
148
 
141
149
  quote! {
@@ -339,6 +347,7 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> syn::Result<TokenStream2> {
339
347
  let includes = find_attribute_values(ast, "include");
340
348
  let excludes = find_attribute_values(ast, "exclude");
341
349
  let metadata_only = find_bool_attribute(ast, "metadata_only").unwrap_or(false);
350
+ let allow_missing = find_bool_attribute(ast, "allow_missing").unwrap_or(false);
342
351
 
343
352
  #[cfg(not(feature = "include-exclude"))]
344
353
  if !includes.is_empty() || !excludes.is_empty() {
@@ -368,9 +377,9 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> syn::Result<TokenStream2> {
368
377
  (None, folder_path)
369
378
  };
370
379
 
371
- if !Path::new(&absolute_folder_path).exists() {
380
+ if !Path::new(&absolute_folder_path).exists() && !allow_missing {
372
381
  let mut message = format!(
373
- "#[derive(RustEmbed)] folder '{}' does not exist. cwd: '{}'",
382
+ "#[derive(RustEmbed)] folder '{}' does not exist. To allow the folder to be missing, add #[allow_missing]. cwd: '{}'",
374
383
  absolute_folder_path,
375
384
  std::env::current_dir().unwrap().to_str().unwrap()
376
385
  );
@@ -397,7 +406,7 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> syn::Result<TokenStream2> {
397
406
  )
398
407
  }
399
408
 
400
- #[proc_macro_derive(RustEmbed, attributes(folder, prefix, include, exclude, metadata_only, crate_path))]
409
+ #[proc_macro_derive(RustEmbed, attributes(folder, prefix, include, exclude, allow_missing, metadata_only, crate_path))]
401
410
  pub fn derive_input_object(input: TokenStream) -> TokenStream {
402
411
  let ast = parse_macro_input!(input as DeriveInput);
403
412
  match impl_rust_embed(&ast) {
readme.md CHANGED
@@ -93,6 +93,11 @@ be included in the file paths returned by `iter`.
93
93
  You can add `#[metadata_only = true]` to the `RustEmbed` struct to exclude file contents from the
94
94
  binary. Only file paths and metadata will be embedded.
95
95
 
96
+ ### `allow_missing`
97
+
98
+ You can add `#[allow_missing = true]` to the `RustEmbed` struct to allow the embedded folder to be missing.
99
+ In that case, RustEmbed will be empty.
100
+
96
101
  ## Features
97
102
 
98
103
  ### `debug-embed`
@@ -116,7 +121,7 @@ This will pull the `foo` directory relative to your `Cargo.toml` file.
116
121
  Compress each file when embedding into the binary. Compression is done via [`include-flate`].
117
122
 
118
123
  ### `include-exclude`
119
- Filter files to be embedded with multiple `#[include = "*.txt"]` and `#[exclude = "*.jpg"]` attributes.
124
+ Filter files to be embedded with multiple `#[include = "*.txt"]` and `#[exclude = "*.jpg"]` attributes.
120
125
  Matching is done on relative file paths, via [`globset`].
121
126
  `exclude` attributes have higher priority than `include` attributes.
122
127
  Example: