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.3"
```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 = "impl Into
// ~~~ 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 ~~~
Also you can use retypes globally.
```rust
struct Identity { // ~~~ sniff ~~~ } ```
Or additionally for String retypes:
```rust
struct Identity { // ~~~ sniff ~~~ } ```
Both codes above will retype all String
fields into impl Into<String>
in public api.
Final result
```rust
struct Identity {
id: usize,
username: String,
firstname: String,
lastname: Option
fn print_identity(ident: impl Into
fn main() { let ident = Identity::new(1, "immmdreza", "Arash").last_name("Tofani");
print_identity(ident);
// Identity {
// id: 1,
// username: "immmdreza",
// first_name: "Arash",
// last_name: Some(
// "Tofani",
// ),
// lang_code: Some(
// "fa",
// ),
// }
} ```
🧀