diff --git a/Cargo.lock b/Cargo.lock index b80e97a..2604f19 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anyhow" version = "1.0.86" @@ -228,6 +243,26 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-targets 0.52.6", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" + [[package]] name = "crossbeam-channel" version = "0.5.13" @@ -243,6 +278,41 @@ version = "0.8.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" +[[package]] +name = "darling" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83b2eb4d90d12bdda5ed17de686c2acb4c57914f8f921b8da7e112b5a36f3fe1" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622687fe0bac72a04e5599029151f5796111b90f1baaa9b544d807a5e31cd120" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "733cabb43482b1a1b53eee8583c2b9e8684d592215ea83efd305dd31bc2f0178" +dependencies = [ + "darling_core", + "quote", + "syn", +] + [[package]] name = "deranged" version = "0.3.11" @@ -252,12 +322,45 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "derive_builder" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0350b5cb0331628a5916d6c5c0b72e97393b8b6b03b47a9284f4e7f5a405ffd7" +dependencies = [ + "derive_builder_macro", +] + +[[package]] +name = "derive_builder_core" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d48cda787f839151732d396ac69e3473923d54312c070ee21e9effcaa8ca0b1d" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "derive_builder_macro" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "206868b8242f27cecce124c19fd88157fbd0dd334df2587f36417bafbc85097b" +dependencies = [ + "derive_builder_core", + "syn", +] + [[package]] name = "ecosystem" version = "0.1.0" dependencies = [ "anyhow", "axum 0.7.5", + "chrono", + "derive_builder", "once_cell", "opentelemetry", "opentelemetry-otlp", @@ -590,6 +693,35 @@ dependencies = [ "tokio", ] +[[package]] +name = "iana-time-zone" +version = "0.1.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "indexmap" version = "1.9.3" @@ -1127,6 +1259,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "syn" version = "2.0.68" @@ -1543,6 +1681,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index fc7abc0..54453f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,8 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } [dev-dependencies] axum = { version = "0.7.5", features = ["http2", "query", "tracing"] } +chrono = "0.4.38" +derive_builder = "0.20.0" rand = "0.8.5" serde = { version = "1.0.203", features = ["derive"] } serde_json = "1.0.120" diff --git a/examples/builder.rs b/examples/builder.rs new file mode 100644 index 0000000..bd0f795 --- /dev/null +++ b/examples/builder.rs @@ -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, + + #[builder(setter(skip), default = "self.running_time_default()")] + running_time: u64, + + #[builder(default, setter(into, strip_option))] + location: Option, + + #[builder(default = "true")] + is_active: bool, + + #[builder(default = "vec![]", setter(each(name = "data_value")))] + data: Vec, +} + +fn main() -> Result<()> { + let device = Device::builder() + .id("29388844402912".parse::()?) + .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::>().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, + } + } +}