-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add common usage examples of derive_builder
- Loading branch information
Showing
3 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use anyhow::Result; | ||
use chrono::{DateTime, Utc}; | ||
use derive_builder::Builder; | ||
|
||
#[allow(unused)] | ||
#[derive(Debug, Clone)] | ||
enum Sensor { | ||
Temperature, | ||
Pressure, | ||
Humidity, | ||
Lightning, | ||
Vibration, | ||
Smoke, | ||
} | ||
|
||
#[allow(unused)] | ||
#[derive(Builder, Debug)] | ||
#[builder(setter(into))] | ||
struct Device { | ||
id: u64, | ||
|
||
name: String, | ||
|
||
device_type: Sensor, | ||
|
||
#[builder(setter(custom))] | ||
create_date: DateTime<Utc>, | ||
|
||
#[builder(setter(skip), default = "self.running_time_default()")] | ||
running_time: u64, | ||
|
||
#[builder(default, setter(into, strip_option))] | ||
location: Option<String>, | ||
|
||
#[builder(default = "true")] | ||
is_active: bool, | ||
|
||
#[builder(default = "vec![]", setter(each(name = "data_value")))] | ||
data: Vec<String>, | ||
} | ||
|
||
fn main() -> Result<()> { | ||
let device = Device::builder() | ||
.id("29388844402912".parse::<u64>()?) | ||
.name("temperature sensor") | ||
.device_type(Sensor::Temperature) | ||
.create_date("2024-07-01T12:34:56Z") | ||
.data_value("30.3".into()) | ||
.data_value("26.8".into()) | ||
.build()?; | ||
|
||
println!("{:#?}", device); | ||
|
||
Ok(()) | ||
} | ||
|
||
impl Device { | ||
pub fn builder() -> DeviceBuilder { | ||
DeviceBuilder::default() | ||
} | ||
} | ||
|
||
impl DeviceBuilder { | ||
pub fn create_date(&mut self, value: &str) -> &mut Self { | ||
self.create_date = value.parse::<DateTime<Utc>>().ok(); | ||
self | ||
} | ||
|
||
fn running_time_default(&self) -> u64 { | ||
match self.create_date { | ||
Some(date) => { | ||
let time = Utc::now() - date; | ||
time.num_days() as u64 | ||
} | ||
None => 0, | ||
} | ||
} | ||
} |