Skip to content

Commit

Permalink
Update docs (#79)
Browse files Browse the repository at this point in the history
Fix derive builder when used with module level visibility.
  • Loading branch information
BrynCooke authored Aug 4, 2022
1 parent ed8c7e5 commit ac38502
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.****

Expand Down
6 changes: 5 additions & 1 deletion src/buildstructor/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ fn builder_visibility(
) -> Result<Visibility> {
// 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 {
Expand Down
25 changes: 24 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ac38502

Please sign in to comment.