diff --git a/CHANGELOG.md b/CHANGELOG.md index ddbac16..942974c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## 0.5.0 - 2022-08-04 +[81](https://github.com/BrynCooke/buildstructor/issues/81) +Fix derive builder when used with module level visibility. + [77](https://github.com/BrynCooke/buildstructor/issues/77) Remove validation of visibility specifier.**** diff --git a/src/buildstructor/lower.rs b/src/buildstructor/lower.rs index e8d7f63..6d5249d 100644 --- a/src/buildstructor/lower.rs +++ b/src/buildstructor/lower.rs @@ -139,7 +139,11 @@ fn builder_visibility( ) -> Result { // Either visibility is set explicitly or we default to super. Ok(if let Some(visibility) = &model.config.visibility { - syn::parse_str(visibility)? + if visibility.trim().is_empty() { + default.clone() + } else { + syn::parse_str(visibility)? + } } else if let Visibility::Inherited = model.vis { default.clone() } else { diff --git a/src/lib.rs b/src/lib.rs index e07f61f..2e6e090 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ use crate::parse::Ast; /// Derive a builder from a constructor! /// -/// 1. Import the `builder` macro. +/// 1. Import the `buildstructor` macro. /// 2. Annotate your `impl` containing a `new` function. /// 3. Use your automatically derived builder. /// @@ -58,6 +58,29 @@ pub fn builder(attr: TokenStream, item: TokenStream) -> TokenStream { do_buildstructor(true, attr, item) } +/// Derive a builder AND a constructor! +/// +/// 1. Import the `Builder` macro. +/// 2. Use your automatically derived builder. +/// +/// TLDR: Write your Rust constructors as you would normally, and get a generated builder. +/// +/// # Examples +/// +/// ```rust +/// use buildstructor::Builder; +/// +/// #[derive(Builder)] +/// struct MyStruct { +/// sum: usize, +/// } +/// +/// # #[allow(clippy::needless_doctest_main)] +/// # fn main() { +/// let mine = MyStruct::builder().sum(3).build(); +/// assert_eq!(mine.sum, 3); +/// # } +/// ``` #[proc_macro_derive(Builder)] pub fn derive_builder(item: TokenStream) -> TokenStream { do_derive(item)