Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add allow_missing option to derive macro #256

Merged
merged 5 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rust_embed_utils::PathMatcher;
use std::{
collections::BTreeMap,
env,
io::ErrorKind,
iter::FromIterator,
path::{Path, PathBuf},
};
Expand Down Expand Up @@ -135,7 +136,14 @@ fn dynamic(
.map(|mut file| { file.data = ::std::default::Default::default(); file })
});

let canonical_folder_path = Path::new(&folder_path).canonicalize().expect("folder path must resolve to an absolute path");
let non_canonical_folder_path = Path::new(&folder_path);
let canonical_folder_path = non_canonical_folder_path
.canonicalize()
.or_else(|err| match err {
err if err.kind() == ErrorKind::NotFound => Ok(non_canonical_folder_path.to_owned()),
err => Err(err),
})
.expect("folder path must resolve to an absolute path");
let canonical_folder_path = canonical_folder_path.to_str().expect("absolute folder path must be valid unicode");

quote! {
Expand Down Expand Up @@ -339,6 +347,7 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> syn::Result<TokenStream2> {
let includes = find_attribute_values(ast, "include");
let excludes = find_attribute_values(ast, "exclude");
let metadata_only = find_bool_attribute(ast, "metadata_only").unwrap_or(false);
let allow_missing = find_bool_attribute(ast, "allow_missing").unwrap_or(false);

#[cfg(not(feature = "include-exclude"))]
if !includes.is_empty() || !excludes.is_empty() {
Expand Down Expand Up @@ -368,7 +377,7 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> syn::Result<TokenStream2> {
(None, folder_path)
};

if !Path::new(&absolute_folder_path).exists() {
if !Path::new(&absolute_folder_path).exists() && !allow_missing {
let mut message = format!(
"#[derive(RustEmbed)] folder '{}' does not exist. cwd: '{}'",
absolute_folder_path,
Expand Down Expand Up @@ -397,7 +406,7 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> syn::Result<TokenStream2> {
)
}

#[proc_macro_derive(RustEmbed, attributes(folder, prefix, include, exclude, metadata_only, crate_path))]
#[proc_macro_derive(RustEmbed, attributes(folder, prefix, include, exclude, allow_missing, metadata_only, crate_path))]
pub fn derive_input_object(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
match impl_rust_embed(&ast) {
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ be included in the file paths returned by `iter`.
You can add `#[metadata_only = true]` to the `RustEmbed` struct to exclude file contents from the
binary. Only file paths and metadata will be embedded.

### `allow_missing`

You can add `#[allow_missing = true]` to the `RustEmbed` struct to allow the embedded folder to be missing.
In that case, RustEmbed will be empty.

## Features

### `debug-embed`
Expand Down
15 changes: 15 additions & 0 deletions tests/allow_missing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::{path::PathBuf, str::FromStr};

use rust_embed::Embed;

#[derive(Embed)]
#[folder = "examples/missing/"]
#[allow_missing = true]
struct Asset;

#[test]
fn missing_is_empty() {
let path = PathBuf::from_str("./examples/missing").unwrap();
assert!(!path.exists());
assert_eq!(Asset::iter().count(), 0);
}
Loading