Skip to main content

Crate bauer

Crate bauer 

Source
Expand description

Bauer is a crate for automatically generating Builder-patterns for your structs!

Not sure what kind of builder you want? Bauer supports a variety of sub-patterns: Owned, Borrowed, and even Type-State!

§Examples

#[derive(Builder)]
#[builder(kind = "type-state")]
pub struct Foo {
    required_field: u32,
    #[builder(default)]
    default_field: u32,
    #[builder(into)]
    converting_field: String,
    #[builder(repeat)]
    repeating_field: Vec<u32>,
    #[builder(repeat, repeat_n = 1..=3)]
    limited_repeating_field: Vec<u32>,
}

let foo: Foo = Foo::builder()
    .required_field(42)
    // .default_field(69) // defaults to 0
    .converting_field("hello world") // calls `.into()` to convert from &str -> String
    .repeating_field(420)
    .repeating_field(1337)
    .limited_repeating_field(0) // If not called 1..=3 times, this will fail
    .build();

Check out the repository for more examples!

§Configuration

§Kinds

Bauer supports generating 3 kinds of builders:

§Owned (default) / Borrowed

"owned" builders are passed around by value and "borrowed" builders are passed by mutable reference.

§Type-State

"type-state" builders use the type-state pattern and generate builds that are validated at compile-time using the type system.

Builder kinds can be switched between trivially using #[builder(kind = <kind>)] on the struct.

§Builder Attributes

All of the attributes that may be applied to the builder are listed below. These go inside of a #[builder(..)] attribute on the struct itself. For a more detailed description and examples, check out the Builder or click on the attribute.

§Builder Configuration

Attributes that affect the generated struct and other items

AttributeDescriptionUsage
kindSet the sub-pattern to use for this builderkind = "borrowed" or kind = "type-state"
constMake this builder work at compile-time – some limitations are added, but most features continue workingconst
visibilityChange the visibility of the created builder (defaults to the same visibility as the struct)visibility = pub(crate)
crateOverride the name of the crate when expanding macros (defaults to bauer)crate = bauer_renamed
errorSet details about the generated error enum (attributes, doc, rename, force)error(...)
build_fnSet details about the build function (attributes, doc, rename, map)build_fn(...)
builder_fnSet details about the builder function added to the struct (attributes, doc, rename)builder_fn(...)
attribute/attributesSet attribute(s) on the generated builder structattribute(#[foo])
doc/docsSet documentation items on the generated builder structdoc(<doc strings>)

§Field Configuration

Attributes affect all functions generated the fields in this struct

AttributeDescriptionUsage
prefix/suffixAdd a prefix/suffix to all field functions created for this builderprefix = "set_" or suffix = "_field"
onApply field attributes to fields that match a specific type patternon(<type> => <attributes ...>)

§Field Attributes

All of the attributes that may be applied to fields are listed below. These go inside of a #[builder(..)] attribute on any field in the struct. For a more detailed description and examples, check out the Builder or click on the attribute.

§Special Field Treatment

Attributes that affect how a field is treated by the builder

AttributeDescriptionUsage
associatedMark this field as associated data with the builder. Associated fields are specified by the builder_fn.associated
skipSkip this field in the builder and construct its value using the other fields (or default value)skip or skip = <value>

§Field Configuration

Attributes that affect how fields are presented to the user

AttributeDescriptionUsage
renameRename the function that is generated for the fieldrename = <name>
skip_prefix/skip_suffixSkip using the prefix/suffix from the builder attributeskip_prefix or skip_suffix
visibilityChange visibility of the generated function (defaults to the same visibility as the builder)visibility = pub(crate)
attribute/attributesSet attribute(s) on the function generated for this fieldattribute(#[foo])
doc/docsSet documentation items on the function generated for this fielddoc(<doc strings>)

§Type Configuration

Configure how the field’s type is constructed or specified by the user

AttributeDescriptionUsage
adapterFully customise how functions take arguments and convert them into the field valueadapter = |<arg>: <ty>| <expr>
defaultSpecify a default value or use Defaultdefault or default = <value>
requiredMake an Option field required to be specified as Some(value) or Nonerequired
flagMark a boolean field as a flag, meaning that the if the function is called, the field will be marked trueflag
intoMake functions accept impl Into<Field>into
tupleMake functions accept tuple items as separate argumentstuple or tuple(x, y)
repeatAllow repeating call to add items to a structurerepeat or repeat = <type>
repeat_nControl the number times a repeat field is allowed to be set. This controls the length of the final datarepeat_n = 1.. or repeat_n = 4
collectorUse a custom collector for converting into the target data structure (default: FromIterator::from_iter)collector = <function>

Modules§

stateDeprecated

Derive Macros§

Builder
The main macro