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. For a more detailed description and examples, check out the Builder or click on the attribute.

AttributeDescriptionUsage
kindSet the sub-patten 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
prefix/suffixAdd a prefix/suffix to all field functions created for this builderprefix = "set_" or suffix = "_field"
visibilityChange the visibility of the created builder (defaults to the same visibility as the struct)prefix = "set_" or suffix = "_field"
crateOverride the name of the crate when expanding macros (defaults to bauer)prefix = "set_" or suffix = "_field"
attribute/attributesSet attribute(s) on the generated builder structattribute(#[foo])
doc/docsSet documentation items on the generated builder structdoc(<doc strings>)
build_fnSet details about the build function (attributes, doc, rename)build_fn(...)
builder_fnSet details about the builder function added to the struct (attributes, doc, rename)builder_fn(...)
errorSet details about the generated error enum (attributes, doc, rename, force)error(...)
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. For a more detailed description and examples, check out the Builder or click on the attribute.

AttributeDescriptionUsage
skipSkip this field in the builder. No other attributes may be specified when this is used.skip or skip = <value>
defaultSpecify a default value or use Defaultdefault or default = <value>
repeatAllow repating call to add items to a structurerepeat or repeat = <type>
repeat_nContorl 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>
intoMake functions accept impl Into<Field>into
tupleMake functions accept tuple items as separate argumentstuple or tuple(x, y)
adapterFully cusotmise how functions take arguments and convert them into the field valueadapter = |<arg>: <ty>| <expr>
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
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>)

Modules§

state

Derive Macros§

Builder
The main macro