Skip to content

Commit

Permalink
Supports both in import_attr
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhil Gupta committed Oct 9, 2023
1 parent b358fa6 commit 3d02187
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
38 changes: 27 additions & 11 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ impl ToTokens for OverridePath {
pub fn import_tokens_attr_internal<T1: Into<TokenStream2>, T2: Into<TokenStream2>>(
attr: T1,
tokens: T2,
hidden_source_path: bool,
) -> Result<TokenStream2> {
let attr = attr.into();
let mm_override_path = parse2::<OverridePath>(attr)?;
Expand Down Expand Up @@ -873,18 +874,33 @@ pub fn import_tokens_attr_internal<T1: Into<TokenStream2>, T2: Into<TokenStream2
Ok(res) => res,
Err(err) => return err.to_compile_error().into()
};
quote::quote! {
#pound resolved_mm_override_path::forward_tokens_verbatim! {
#pound path,
#orig_sig_ident,
#pound resolved_mm_override_path,
{
{ #pound attached_item },
{ #pound path },
{ #pound custom_parsed }
if #hidden_source_path {
quote::quote! {
#pound resolved_mm_override_path::forward_tokens! {
#pound path,
#orig_sig_ident,
#pound resolved_mm_override_path,
{
{ #pound attached_item },
{ #pound path },
{ #pound custom_parsed }
}
}
}
}.into()
}.into()
} else {
quote::quote! {
#pound resolved_mm_override_path::forward_tokens_verbatim! {
#pound path,
#orig_sig_ident,
#pound resolved_mm_override_path,
{
{ #pound attached_item },
{ #pound path },
{ #pound custom_parsed }
}
}
}.into()
}
}
}
};
Expand Down
10 changes: 9 additions & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,15 @@ pub fn import_tokens_proc(attr: TokenStream, tokens: TokenStream) -> TokenStream
/// For more information and an example see [`macro@with_custom_parsing`].
#[proc_macro_attribute]
pub fn import_tokens_attr(attr: TokenStream, tokens: TokenStream) -> TokenStream {
match import_tokens_attr_internal(attr, tokens) {
match import_tokens_attr_internal(attr, tokens, true) {
Ok(tokens) => tokens.into(),
Err(err) => err.to_compile_error().into(),
}
}

#[proc_macro_attribute]
pub fn import_tokens_attr_verbatim(attr: TokenStream, tokens: TokenStream) -> TokenStream {
match import_tokens_attr_internal(attr, tokens, false) {
Ok(tokens) => tokens.into(),
Err(err) => err.to_compile_error().into(),
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub use macro_magic_macros::{

#[cfg(feature = "proc_support")]
pub use macro_magic_macros::{
import_tokens, import_tokens_attr, import_tokens_proc, with_custom_parsing,
import_tokens, import_tokens_attr, import_tokens_attr_verbatim, import_tokens_proc, with_custom_parsing,
};

/// Contains re-exports required at compile-time by the macro_magic macros and support
Expand Down

0 comments on commit 3d02187

Please sign in to comment.