Announcement
Bon 3.0 is now generally available 🚀! If you've been using 2.0, see the changes in the release blog post.
Overview
bon
is a Rust crate for generating compile-time-checked builders for structs and functions. It also provides idiomatic partial application with optional and named parameters for functions and methods.
If you wonder "Why would I use builders?", see the motivational blog post.
Function Builder
You can turn a function with positional parameters into a function with named parameters with #[builder]
.
use builder;
let greeting = greet
.name
.level // <- setting `level` is optional, we could omit it
.call;
assert_eq!;
Any syntax for functions is supported including async
, fallible, generic functions, impl Trait
, etc.
Many things are customizable with additional attributes described in the API reference, but let's see what else bon
has to offer.
Struct Builder
Use #[derive(Builder)]
to generate a builder for a struct.
use Builder;
let user = builder
.name
// `level` is optional, we could omit it here
.level
// call setters in any order
.is_admin
.build;
assert_eq!;
assert_eq!;
assert!;
Method Builder
Associated methods require #[bon]
on top of the impl block additionally.
Method new
The method named new
generates builder()/build()
methods.
use bon;
let user = builder
.id
.name
.build;
assert_eq!;
assert_eq!;
#[derive(Builder)]
on a struct generates builder API that is fully compatible with placing #[builder]
on the new()
method with a signature similar to the struct's fields (more details on the Compatibility page).
Other Methods
All other methods generate {method_name}()/call()
methods.
use bon;
let greeter = Greeter ;
let greeting = greeter
.greet
.target
// `prefix` is optional, omitting it is fine
.call;
assert_eq!;
Methods with or without self
are both supported.
No Panics Possible
Builders generated by bon
's macros use the typestate pattern to ensure all required parameters are filled, and the same setters aren't called repeatedly to prevent unintentional overwrites. If something is wrong, a compile error will be created.
⭐ Don't forget to give our repo a star on Github ⭐! |
---|
What's Next?
What you've seen above is the first page of the 📖 Guide Book. If you want to learn more, jump to the Basics section. And remember: knowledge is power 🐱!
Feel free to jump to code and use the #[builder]
and #[derive(Builder)]
once you've seen enough docs to get started.
The 🔍 API Reference will help you navigate the attributes once you feel comfortable with the basics of bon
. Both #[derive(Builder)]
on structs and #[builder]
on functions/methods have almost identical attributes API, so the documentation for them is common.
Installation
Add bon
to your Cargo.toml
.
[]
= "3.1"
You can opt out of std
and alloc
cargo features with default-features = false
for no_std
environments.
Acknowledgments
This project was heavily inspired by such awesome crates as buildstructor
, typed-builder
and derive_builder
. This crate was designed with many lessons learned from them.
See alternatives for comparison.
Who's Using bon
?
Some notable users:
Getting Help
If you can't figure something out, consult the docs and maybe use the 🔍 Search
bar on our docs website. You may also create an issue or a discussion on the Github repository for help or write us a message on Discord.