diff options
Diffstat (limited to 'crates/core/tedge_lib')
-rw-r--r-- | crates/core/tedge_lib/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/core/tedge_lib/src/config.rs | 56 | ||||
-rw-r--r-- | crates/core/tedge_lib/src/lib.rs | 1 |
3 files changed, 59 insertions, 0 deletions
diff --git a/crates/core/tedge_lib/Cargo.toml b/crates/core/tedge_lib/Cargo.toml index 083779ea..5c95662a 100644 --- a/crates/core/tedge_lib/Cargo.toml +++ b/crates/core/tedge_lib/Cargo.toml @@ -14,5 +14,7 @@ thiserror = "1.0.30" tokio = { version = "1.16.1", features = ["macros", "rt", "sync", "time"] } toml = "0.5.8" tracing = "0.1" +humantime-serde = "1" +indoc = "1.0.6" tedge_api = { path = "../tedge_api" } diff --git a/crates/core/tedge_lib/src/config.rs b/crates/core/tedge_lib/src/config.rs new file mode 100644 index 00000000..b06ef3ff --- /dev/null +++ b/crates/core/tedge_lib/src/config.rs @@ -0,0 +1,56 @@ +#[derive(Copy, Clone, Debug, serde::Deserialize)] +#[serde(transparent)] +pub struct Humantime(#[serde(with = "humantime_serde")] std::time::Duration); + +impl Humantime { + pub fn into_duration(self) -> std::time::Duration { + self.0 + } +} + +impl tedge_api::AsConfig for Humantime { + fn as_config() -> tedge_api::ConfigDescription { + tedge_api::ConfigDescription::new( + "Duration-representing String".to_string(), + tedge_api::ConfigKind::String, + Some(indoc::indoc!{r#" + A String that represents a duration + + ## Examples + + A duration of one minute: + + ```toml + "1min" + ``` + + A duration of 5 minutes and 2 nanoseconds: + + ```toml + "5min 2ns" + ``` + + ## More information + + For more information have a look at the documentation of + [the humantime crate](https://docs.rs/humantime). + "#}), + ) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_humantime_deser() { + let ts = r#"t = "1sec""#; + + #[derive(serde::Deserialize)] + struct T { + t: super::Humantime, + } + + let ht: T = toml::from_str(ts).unwrap(); + assert_eq!(ht.t.0, std::time::Duration::from_secs(1)); + } +} diff --git a/crates/core/tedge_lib/src/lib.rs b/crates/core/tedge_lib/src/lib.rs index 279aad2b..4f52bd8a 100644 --- a/crates/core/tedge_lib/src/lib.rs +++ b/crates/core/tedge_lib/src/lib.rs @@ -1,3 +1,4 @@ +pub mod config; pub mod iter; pub mod mainloop; pub mod measurement; |