-
Notifications
You must be signed in to change notification settings - Fork 2
/
builder.rs
59 lines (48 loc) · 1.76 KB
/
builder.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
///Data type that we want to build
#[derive(Debug)]
pub struct Human {
age: u32,
name: String,
sex: String,
}
///Builder data type, that we will implement the [Builder pattern]
pub struct HumanBuilder {
with_age: Option<u32>,
with_name: Option<String>,
with_sex: Option<String>,
}
///Implementation of [Builder pattern]. We use the [HumanBuilder] type to keep all the temporal data with [Option] types,
/// so then we can control filled and unfilled fields. Then once we use [build] function, we transform the Builder object
/// into the final Data type.
impl HumanBuilder {
pub fn new() -> Self {
return HumanBuilder { with_age: None, with_name: None, with_sex: None };
}
pub fn with_age(self, age: u32) -> Self {
return HumanBuilder { with_age: Some(age), with_name: self.with_name, with_sex: self.with_sex };
}
pub fn with_name(self, name: String) -> Self {
return HumanBuilder { with_age: self.with_age, with_name: Some(name), with_sex: self.with_sex };
}
pub fn with_sex(self, sex: String) -> Self {
return HumanBuilder { with_age: self.with_age, with_name: self.with_name, with_sex: Some(sex) };
}
pub fn build(self) -> Human {
let s = self.with_age;
return Human { age: self.with_age.unwrap_or_default(), name: self.with_name.unwrap_or_default(), sex: self.with_sex.unwrap_or_default() };
}
}
#[cfg(test)]
mod tests {
use crate::creational::builder::HumanBuilder;
#[test]
fn builder_pattern() {
let human = HumanBuilder::new()
.with_name("Politrons".to_string())
.with_age(42)
.with_sex("Male".to_string())
.build();
println!("${:?}", human);
assert_eq!(human.name, "Politrons");
}
}