From ea556a1725ab6e794e5df4093a1e71fd2342ae73 Mon Sep 17 00:00:00 2001 From: Jakub Arnold Date: Wed, 5 Apr 2023 02:58:30 +0200 Subject: [PATCH 1/5] Generalized include_dir to accept multiple args --- macros/src/lib.rs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 51e5b4cf49..c200292649 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -17,11 +17,35 @@ use std::{ #[proc_macro] pub fn include_dir(input: TokenStream) -> TokenStream { let tokens: Vec<_> = input.into_iter().collect(); + let num_tokens = tokens.len(); + + let literals = tokens + .into_iter() + .filter_map(|token| { + match token { + TokenTree::Literal(lit) => Some(lit), + TokenTree::Group(group) => { + let inside = group.stream().into_iter().collect::>(); + + if let [TokenTree::Literal(lit)] = inside.as_slice() { + Some(lit.clone()) + } else { + None + } + } + _ => None, + } + }) + .collect::>(); - let path = match tokens.as_slice() { - [TokenTree::Literal(lit)] => unwrap_string_literal(lit), - _ => panic!("This macro only accepts a single, non-empty string argument"), - }; + if literals.len() != num_tokens { + panic!("This macro only accepts a single, non-empty string argument"); + } + + let path = literals + .into_iter() + .map(|lit| unwrap_string_literal(&lit)) + .collect::(); let path = resolve_path(&path, get_env).unwrap(); From 4fc0b35d95833c964fdc163f998a446579140b0f Mon Sep 17 00:00:00 2001 From: Jakub Arnold Date: Wed, 5 Apr 2023 03:17:02 +0200 Subject: [PATCH 2/5] Better error reporting --- macros/src/lib.rs | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index c200292649..986039e214 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -16,32 +16,31 @@ use std::{ /// Embed the contents of a directory in your crate. #[proc_macro] pub fn include_dir(input: TokenStream) -> TokenStream { - let tokens: Vec<_> = input.into_iter().collect(); - let num_tokens = tokens.len(); - - let literals = tokens + let literals = input .into_iter() - .filter_map(|token| { - match token { - TokenTree::Literal(lit) => Some(lit), - TokenTree::Group(group) => { - let inside = group.stream().into_iter().collect::>(); - - if let [TokenTree::Literal(lit)] = inside.as_slice() { - Some(lit.clone()) - } else { - None - } + .map(|token| match token { + TokenTree::Literal(lit) => lit, + TokenTree::Group(ref group) => { + let inside = group.stream().into_iter().collect::>(); + + if let [TokenTree::Literal(lit)] = inside.as_slice() { + lit.clone() + } else { + panic!( + "This macro only accepts string arguments, got unexpected {:?}", + token + ); } - _ => None, + } + _ => { + panic!( + "This macro only accepts string arguments, got unexpected {:?}", + token + ); } }) .collect::>(); - if literals.len() != num_tokens { - panic!("This macro only accepts a single, non-empty string argument"); - } - let path = literals .into_iter() .map(|lit| unwrap_string_literal(&lit)) From 25d992b851e27b4f39b73cbe3a03dc7e029ce99d Mon Sep 17 00:00:00 2001 From: Jakub Arnold Date: Mon, 25 Sep 2023 00:00:51 +0200 Subject: [PATCH 3/5] Rename for republish --- include_dir/Cargo.toml | 8 ++++---- macros/Cargo.toml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include_dir/Cargo.toml b/include_dir/Cargo.toml index fc09154a0a..741d8a4f1b 100644 --- a/include_dir/Cargo.toml +++ b/include_dir/Cargo.toml @@ -1,6 +1,6 @@ [package] authors = ["Michael Bryan "] -name = "include_dir" +name = "comfy_include_dir" version = "0.7.3" description = "Embed the contents of a directory in your binary" license = "MIT" @@ -13,15 +13,15 @@ rust-version = "1.56" [dependencies] glob = { version = "0.3", optional = true } -include_dir_macros = { version = "^0.7.0", path = "../macros" } +comfy_include_dir_macros = { version = "^0.7.0", path = "../macros" } [dev-dependencies] tempfile = "3" [features] default = [] -nightly = ["include_dir_macros/nightly"] -metadata = ["include_dir_macros/metadata"] +nightly = ["comfy_include_dir_macros/nightly"] +metadata = ["comfy_include_dir_macros/metadata"] [package.metadata.docs.rs] all-features = true diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 3ce97c9e20..6418c31d40 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "include_dir_macros" +name = "comfy_include_dir_macros" version = "0.7.3" description = "The procedural macro used by include_dir" authors = ["Michael Bryan "] From cbc85c99e20e7fa9b43c7d20d605babe4bd03d99 Mon Sep 17 00:00:00 2001 From: Jakub Arnold Date: Mon, 25 Sep 2023 00:01:19 +0200 Subject: [PATCH 4/5] Update resolver=2 --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index b2b10a9da9..732a4b7581 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,3 @@ [workspace] +resolver = "2" members = ["include_dir", "macros"] From 89553d748668e712eef2a2046b926c88fc9b8f74 Mon Sep 17 00:00:00 2001 From: Jakub Arnold Date: Mon, 25 Sep 2023 00:02:07 +0200 Subject: [PATCH 5/5] Fix module name --- include_dir/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include_dir/src/lib.rs b/include_dir/src/lib.rs index b9380b931c..3567e124d6 100644 --- a/include_dir/src/lib.rs +++ b/include_dir/src/lib.rs @@ -104,7 +104,7 @@ mod globs; pub use crate::metadata::Metadata; pub use crate::{dir::Dir, dir_entry::DirEntry, file::File}; -pub use include_dir_macros::include_dir; +pub use comfy_include_dir_macros::include_dir; #[doc = include_str!("../README.md")] #[allow(dead_code)]