Skip to content

Commit

Permalink
Merge pull request #4 from risingwavelabs/bz/macro-vis
Browse files Browse the repository at this point in the history
  • Loading branch information
BugenZhao authored Feb 27, 2024
2 parents f28a7b9 + 195b3e7 commit 1a08ab5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
16 changes: 13 additions & 3 deletions derive/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ struct DeriveMeta {
nt_report_debug: bool,
macro_mangle: bool,
macro_path: Option<TokenStream>,
macro_vis: Option<Visibility>,
}

fn resolve_meta(input: &DeriveInput) -> Result<DeriveMeta> {
Expand All @@ -143,6 +144,7 @@ fn resolve_meta(input: &DeriveInput) -> Result<DeriveMeta> {
let mut nt_report_debug = false;
let mut macro_mangle = false;
let mut macro_path = None;
let mut macro_vis = None;

for attr in &input.attrs {
if attr.path().is_ident("thiserror_ext") {
Expand Down Expand Up @@ -182,6 +184,13 @@ fn resolve_meta(input: &DeriveInput) -> Result<DeriveMeta> {
"macro path should start with `crate`",
));
}
} else if meta.path.is_ident("vis") {
let value = meta.value()?;
macro_vis = Some(if let Ok(lit_str) = value.parse::<LitStr>() {
lit_str.parse()?
} else {
value.parse()?
})
} else {
return Err(Error::new_spanned(meta.path, "unknown attribute"));
}
Expand All @@ -202,6 +211,7 @@ fn resolve_meta(input: &DeriveInput) -> Result<DeriveMeta> {
nt_report_debug,
macro_mangle,
macro_path,
macro_vis,
})
}

Expand Down Expand Up @@ -519,16 +529,16 @@ pub fn derive_ctor(input: &DeriveInput, t: DeriveCtorType) -> Result<TokenStream
}

pub fn derive_macro_inner(input: &DeriveInput, bail: bool) -> Result<TokenStream> {
let input_type = input.ident.clone();
let vis = &input.vis;

let DeriveMeta {
impl_type,
macro_mangle,
macro_path,
macro_vis,
..
} = resolve_meta(input)?;

let input_type = input.ident.clone();
let vis = macro_vis.unwrap_or_else(|| input.vis.clone());
let input = Input::from_syn(input)?;

let variants = match input {
Expand Down
5 changes: 3 additions & 2 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,13 @@ pub fn derive_context_into(input: TokenStream) -> TokenStream {
///
/// There's a different rule set for the visibility of the macros. The macros
/// generated by this proc-macro are marked with `#[macro_export]` only if the
/// visibility of the error type is `pub`, or they're just re-exported with
/// the same visibility as the error type and only work in the same crate.
/// visibility of the error type is `pub`, otherwise they're just re-exported
/// with the same visibility as the error type and only work in the same crate.
///
/// There're some extra configurations to help to better handle the visibility,
/// specified in `#[thiserror_ext(macro(..))]`:
///
/// - `vis = ..`: use a different visibility for the macro re-export.
/// - `mangle`: mangle the macro names so that they don't conflict with other
/// macros with the same name in the crate root.
/// - `path = "crate::.."`: the path to the current module. When specified,
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, path = "crate::inner"))]
#[thiserror_ext(macro(mangle, path = "crate::inner", vis = pub(super)))]
pub struct NotImplemented {
pub issue: Option<i32>,
pub message: String,
Expand Down

0 comments on commit 1a08ab5

Please sign in to comment.