Engineer
Engineer is a master builder based on Optional.
It just generates an Engineer (Builder) class for a data model.
Installation
Add following as dependencies
[]
= "0.1.3"
Get Started
use Engineer;
Optional fields are not required during the initialization.
// Option fields are set to None.
let identity = engineer.done;
But you can set a value for Option fields as well.
let identity = engineer // IdentityEngineer
.first_name // IdentityEngineer
.last_name // IdentityEngineer
.done; // Identity
That's all for the basics, but you can do a little customizations.
Customizations
Engineer Struct Name
Engineer struct name is {struct}Engineer (IdentityEngineer for Identity) by default, but you can change that.
// ~~~ sniff ~~~
// ~~~ sniff ~~~
let identity = engineer // IdentityBuilder
.first_name // IdentityBuilder
.last_name // IdentityBuilder
.done; // Identity
Builder Function Name
The name of builder function is engineer by default, but guess what?
// ~~~ sniff ~~~
// ~~~ sniff ~~~
let identity = builder
// ~~~ sniff ~~~
You want to use this as new function:
// ~~~ sniff ~~~
// ~~~ sniff ~~~
let identity = new
// ~~~ sniff ~~~
This can be simplified for special new name as builder function:
Default value for Options
You can set a default value for option fields.
This value is used if you don't set any other for them.
// ~~~ sniff ~~~
// ~~~ sniff ~~~
let identity = new;
identity.lang_code // Some("fa")
Alternatively, you can use default to set Some(Default::default) instead of None if any other value is not given.
// ~~~ sniff ~~~
luck_number: , // Some(0)
// ~~~ sniff ~~~
Retype
You can change types requested in builder processes.
// ~~~ sniff ~~~
// ~~~ sniff ~~~
let identity = new; // .to_string() is not needed.
// ~~~ sniff ~~~
Alternatively, for str retypes (like example above), you can use a shorthand str_retype.
// ~~~ sniff ~~~
username: String,
// ~~~ sniff ~~~
Also you can use retypes globally.
Or additionally for String retypes:
Both codes above will retype all String fields into impl Into<String> in public api.
Final result
More about traits
This crate has two main traits: Builder<T> where T: Engineer and Engineer.
Engineer trait has two associated types: Params and Builder.
Paramsis a tuple of your required fields types ( fields that are notOption<_>)Builderis the type of Builder/Engineer class.
Using Engineer macro, will make your data class implements Engineer trait and also
creates a Builder struct ( usually named {struct_name}Engineer - i'm thinking about rename )
that implements Builder<T> where T is your own struct.
This enables you to do some generic stuff around these traits, As instance:
// Or
Note that E::Params is a tuple.
If all of you required fields (E::Param) implement Default, another function build_default
will become available on you struct that creates a default instance with no inputs required.
Additionally, Engineer trait has two const fields:
- const NORMAL_FIELDS: usize;
- const OPTIONAL_FIELDS: usize;
🧀