Engineer is a master builder based on Optional
.
It just generates an Engineer (Builder) class for a data model.
Add following as dependencies
toml
[dependencies]
engineer = "0.1.2"
```rust use engineer::Engineer;
struct Identity {
id: usize,
username: String,
firstname: Option
Optional fields are not required during the initialization.
rust
// Option fields are set to None.
let identity = Identity::engineer(0, "immmdreza".to_string()).done();
But you can set a value for Option
fields as well.
rust
let identity = Identity::engineer(0, "immmdreza".to_string()) // IdentityEngineer
.first_name("Arash".to_string()) // IdentityEngineer
.last_name("Tofani".to_string()) // IdentityEngineer
.done(); // Identity
That's all for the basics, but you can do a little customizations.
Engineer struct name is {struct}Engineer (IdentityEngineer
for Identity
) by default, but you can change that.
```rust // ~~~ sniff ~~~
struct Identity { // ~~~ sniff ~~~ }
// ~~~ sniff ~~~
let identity = Identity::engineer(0, "immmdreza".to_string()) // IdentityBuilder
.first_name("Arash".to_string()) // IdentityBuilder
.last_name("Tofani".to_string()) // IdentityBuilder
.done(); // Identity
```
The name of builder function is engineer
by default, but guess what?
```rust // ~~~ sniff ~~~
struct Identity { // ~~~ sniff ~~~ }
// ~~~ sniff ~~~
let identity = Identity::builder(0, "immmdreza".to_string())
// ~~~ sniff ~~~
```
You want to use this as new
function:
```rust // ~~~ sniff ~~~
struct Identity { // ~~~ sniff ~~~ }
// ~~~ sniff ~~~
let identity = Identity::new(0, "immmdreza".to_string())
// ~~~ sniff ~~~
```
You can set a default value for option fields.
This value is used if you don't set any other for them.
```rust // ~~~ sniff ~~~
struct Identity {
// ~~~ sniff ~~~
#[engineer(defaultvalue = "\"fa\".tostring()")]
lang_code: Option
// ~~~ sniff ~~~
let identity = Identity::new(0, "immmdreza".to_string());
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.
rust
// ~~~ sniff ~~~
#[engineer(default)]
luck_number: Option<i32>, // Some(0)
// ~~~ sniff ~~~
You can change types requested in builder processes.
```rust // ~~~ sniff ~~~
struct Identity { // ~~~ sniff ~~~ #[engineer(retype(to = "&str", re = ".to_string()"))] // ^ ^ // | Requested type in public. // | // | How we recover to original type. username: String, // ~~~ sniff ~~~ }
// ~~~ sniff ~~~
let identity = Identity::new(0, "immmdreza"); // .to_string() is not needed.
// ~~~ sniff ~~~
```
Alternatively, for str retypes (like example above), you can use a shorthand str_retype
.
rust
// ~~~ sniff ~~~
#[engineer(str_retype)]
username: String,
// ~~~ sniff ~~~
Final result
```rust
struct Identity { id: usize,
#[engineer(str_retype)]
username: String,
#[engineer(str_retype)]
first_name: Option<String>,
#[engineer(str_retype)]
last_name: Option<String>,
#[engineer(str_retype, default_value = "\"fa\".to_string()")]
lang_code: Option<String>,
} ```
🧀