Skip to content
Open
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
2 changes: 1 addition & 1 deletion include_dir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ pub use crate::metadata::Metadata;
pub use crate::{dir::Dir, dir_entry::DirEntry, file::File};
pub use include_dir_macros::include_dir;

#[doc = include_str!("../README.md")]
#[doc = include_str!("../../README.md")]
#[allow(dead_code)]
fn check_readme_examples() {}
21 changes: 21 additions & 0 deletions include_dir/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,24 @@ fn msrv_is_in_sync() {
.unwrap();
assert_eq!(workflow_msrv, msrv);
}

#[test]
fn call_from_other_macro() {
macro_rules! call_include_dir {
($path:literal) => {
include_dir!($path)
};
}

macro_rules! call_include_dir_2 {
($path:literal) => {
call_include_dir!($path)
};
}

static DIR: Dir<'_> = call_include_dir!("$CARGO_MANIFEST_DIR");
static DIR2: Dir<'_> = call_include_dir_2!("$CARGO_MANIFEST_DIR");

assert!(DIR.contains("src/lib.rs"));
assert!(DIR2.contains("src/lib.rs"));
}
10 changes: 9 additions & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! You probably don't want to use this crate directly.
#![cfg_attr(feature = "nightly", feature(track_path, proc_macro_tracked_env))]

use proc_macro::{TokenStream, TokenTree};
use proc_macro::{Delimiter, TokenStream, TokenTree};
use proc_macro2::Literal;
use quote::quote;
use std::{
Expand All @@ -20,6 +20,14 @@ pub fn include_dir(input: TokenStream) -> TokenStream {

let path = match tokens.as_slice() {
[TokenTree::Literal(lit)] => unwrap_string_literal(lit),
// Delimiter::None is used when the macro is invoked from another macro
[TokenTree::Group(g)] if g.delimiter() == Delimiter::None => {
let stream: Vec<_> = g.stream().into_iter().collect();
match stream.as_slice() {
[TokenTree::Literal(lit)] => unwrap_string_literal(lit),
_ => panic!("This macro only accepts a single, non-empty string argument"),
}
}
_ => panic!("This macro only accepts a single, non-empty string argument"),
};

Expand Down