Skip to content

Commit

Permalink
support specifying path for macro
Browse files Browse the repository at this point in the history
Signed-off-by: Bugen Zhao <[email protected]>
  • Loading branch information
BugenZhao committed Nov 23, 2023
1 parent 6d12414 commit 14d4d7d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
35 changes: 28 additions & 7 deletions derive/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use either::{for_both, Either};
use proc_macro2::TokenStream;
use quote::{format_ident, quote, ToTokens};
use syn::{
spanned::Spanned, DeriveInput, GenericArgument, Ident, Member, PathArguments, Result, Type,
Visibility,
spanned::Spanned, DeriveInput, GenericArgument, Ident, LitStr, Member, PathArguments, Result,
Type, Visibility,
};

use crate::thiserror::ast::{Field, Input, Variant};
Expand Down Expand Up @@ -131,12 +131,14 @@ struct DeriveMeta {
impl_type: Ident,
backtrace: bool,
macro_mangle: bool,
macro_path: Option<TokenStream>,
}

fn resolve_meta(input: &DeriveInput) -> Result<DeriveMeta> {
let mut impl_type = None;
let mut backtrace = false;
let mut macro_mangle = false;
let mut macro_path = None;

for attr in &input.attrs {
if attr.path().is_ident("thiserror_ext") {
Expand All @@ -150,6 +152,23 @@ fn resolve_meta(input: &DeriveInput) -> Result<DeriveMeta> {
meta.parse_nested_meta(|meta| {
if meta.path.is_ident("mangle") {
macro_mangle = true;
} else if meta.path.is_ident("path") {
let value = meta.value()?;
let path: LitStr = value.parse()?;
let mut path = path.value();

if path.starts_with("crate") {
path.insert(0, '$');
if !path.ends_with("::") {
path.push_str("::");
}
macro_path = Some(path.parse()?);
} else {
return Err(syn::Error::new_spanned(
meta.path,
"macro path should start with `crate`",
));
}
}
Ok(())
})?;
Expand All @@ -165,6 +184,7 @@ fn resolve_meta(input: &DeriveInput) -> Result<DeriveMeta> {
impl_type,
backtrace,
macro_mangle,
macro_path,
})
}

Expand Down Expand Up @@ -372,6 +392,7 @@ pub fn derive_macro_inner(input: &DeriveInput, bail: bool) -> Result<TokenStream
let DeriveMeta {
impl_type,
macro_mangle,
macro_path,
..
} = resolve_meta(input)?;

Expand All @@ -394,8 +415,8 @@ pub fn derive_macro_inner(input: &DeriveInput, bail: bool) -> Result<TokenStream

let variant_name = for_both!(&variant, v => &v.ident);
let ctor_path = match &variant {
Either::Left(_s) => quote!(#input_type),
Either::Right(_v) => quote!(#input_type::#variant_name),
Either::Left(_s) => quote!(#macro_path #input_type),
Either::Right(_v) => quote!(#macro_path #input_type::#variant_name),
};

let fields = for_both!(&variant, v => &v.fields);
Expand Down Expand Up @@ -473,12 +494,12 @@ pub fn derive_macro_inner(input: &DeriveInput, bail: bool) -> Result<TokenStream

let full_inner = if bail {
quote!({
let res: #impl_type = (#ctor_expr).into();
return Err(res.into());
let res: #macro_path #impl_type = (#ctor_expr).into();
return ::std::result::Result::Err(res.into());
})
} else {
quote!({
let res: #impl_type = (#ctor_expr).into();
let res: #macro_path #impl_type = (#ctor_expr).into();
res
})
};
Expand Down
2 changes: 1 addition & 1 deletion tests/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub mod inner {
}
#[derive(Error, Debug, Macro)]
#[error("not implemented: {message}, issue: {issue:?}")]
#[thiserror_ext(macro(mangle))]
#[thiserror_ext(macro(mangle, path = "crate::inner"))]
pub struct NotImplemented {
pub issue: Option<i32>,
pub message: String,
Expand Down

0 comments on commit 14d4d7d

Please sign in to comment.