| Crates.io | typed_builder_rules |
| lib.rs | typed_builder_rules |
| version | 0.1.0 |
| created_at | 2025-04-15 13:05:30.264701+00 |
| updated_at | 2025-04-15 13:05:30.264701+00 |
| description | Opinionated and very limited macro_rules reimplementation of the typed_builder macro |
| homepage | |
| repository | https://github.com/nrabulinski/typed_builder_rules |
| max_upload_size | |
| id | 1634398 |
| size | 12,555 |
Opinionated and very limited macro_rules reimplementation of the typed_builder derive macro.
Currently only supports required fields and "private" (or "computed") fields and does not support Option stripping. You can hack your way to "optional" fields though.
The struct you declare will be re-emitted as-is with all the custom syntax stripped
and with an impl block containing ::builder() method, which returns an empty instance of Builder.
The builder implements ::build method which is only implemented for builders with all fields filled in.
Fields with a default value don't have to be filled in before ::build can be called.
(They're considered filled in, even if the user doesn't provide a value)
The syntax is pretty much "stock" Rust struct declaration with a few notable exceptions:
@ sign before a field declaration to make it "private" (i.e. it will not be part of the builder)= expr syntax after a field declaration to set the default value of a field! after the field name to force this field's setter to only accept specifically the type of that field.
The default is impl Into<T>.Private fields have to also specify the computed value.
Important implementation details:
use typed_builder_rules::typed_builder;
typed_builder!(
#[derive(Debug, PartialEq)]
struct Foo {
foo: String, // regular, required field. setter accepts `impl Into<String>`
bar!: String, // reqular, required field. must use `String` in the setter
baz: String = format!("{foo} {bar}"), // regular, required field. if not provided will take value `format!("{foo} {bar}")`
@qux: u64 = {
use std::hash::{DefaultHasher, Hash, Hasher};
let mut s = DefaultHasher::new();
foo.hash(&mut s);
bar.hash(&mut s);
baz.hash(&mut s);
s.finish()
}, // private field. only gets initialized inside of `::build()`
}
);
fn main() {
let expected = Foo {
foo: "foo".to_string(),
bar: "bar".to_string(),
baz: "foo bar".to_string(),
qux: 10576444654000555064,
};
let actual = Foo::builder()
.foo("foo")
.bar("bar".to_string())
.build();
assert_eq!(expected, actual);
}