diff options
author | Matthias Beyer <matthias.beyer@ifm.com> | 2022-08-18 13:53:26 +0200 |
---|---|---|
committer | Matthias Beyer <matthias.beyer@ifm.com> | 2022-08-30 13:54:49 +0200 |
commit | 021db86e5c42570ea8dd63b5ac94485a093888a2 (patch) | |
tree | ec3a7a9c3fbbce4c4e531d5c6bd458b01597c04e | |
parent | 08a32f3294d65b4011e54838fc67c792cf8f3ff0 (diff) |
Add bytesize helper for implementing AsConfig on ByteSize typepost-merge/tedge_lib-bytesize-helper
Signed-off-by: Matthias Beyer <matthias.beyer@ifm.com>
-rw-r--r-- | Cargo.lock | 10 | ||||
-rw-r--r-- | crates/core/tedge_lib/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/core/tedge_lib/src/config/bytesize.rs | 47 | ||||
-rw-r--r-- | crates/core/tedge_lib/src/config/mod.rs | 3 |
4 files changed, 61 insertions, 0 deletions
@@ -439,6 +439,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec8a7b6a70fde80372154c65702f00a0f56f3e1c36abbc6c440484be248856db" [[package]] +name = "bytesize" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c58ec36aac5066d5ca17df51b3e70279f5670a72102f5752cb7e7c856adfc70" +dependencies = [ + "serde", +] + +[[package]] name = "c8y_api" version = "0.7.4" dependencies = [ @@ -3708,6 +3717,7 @@ version = "0.1.0" dependencies = [ "async-trait", "bevy_reflect", + "bytesize", "futures", "humantime-serde", "indoc", diff --git a/crates/core/tedge_lib/Cargo.toml b/crates/core/tedge_lib/Cargo.toml index c181439e..293bdf7e 100644 --- a/crates/core/tedge_lib/Cargo.toml +++ b/crates/core/tedge_lib/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] async-trait = "0.1.52" +bytesize = { version = "1.1", features = [ "serde" ] } futures = "0.3" log = "0.4" serde = { version = "1.0.136", features = ["derive"] } diff --git a/crates/core/tedge_lib/src/config/bytesize.rs b/crates/core/tedge_lib/src/config/bytesize.rs new file mode 100644 index 00000000..e4362b88 --- /dev/null +++ b/crates/core/tedge_lib/src/config/bytesize.rs @@ -0,0 +1,47 @@ +#[derive(Copy, Clone, Debug, serde::Deserialize)] +#[serde(transparent)] +pub struct ByteSize(bytesize::ByteSize); + +impl ByteSize { + pub fn into_bytesize(self) -> bytesize::ByteSize { + self.0 + } +} + +impl tedge_api::AsConfig for ByteSize { + fn as_config() -> tedge_api::ConfigDescription { + tedge_api::ConfigDescription::new( + "Byte-size-representing String".to_string(), + tedge_api::ConfigKind::String, + Some(indoc::indoc! {r#" + A String that represents a size of bytes + + ## Examples + + One Megabyte: `"1 MB"` + 512 kB: `"512 kB"` + + ## More information + + For more information have a look at the README of + [the bytesize crate](https://github.com/hyunsik/bytesize/#example). + "#}), + ) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_bytesize_deser() { + let ts = r#"t = "1 MB""#; + + #[derive(serde::Deserialize)] + struct T { + t: super::ByteSize, + } + + let ht: T = toml::from_str(ts).unwrap(); + assert_eq!(ht.t.0, bytesize::ByteSize::mb(1)); + } +} diff --git a/crates/core/tedge_lib/src/config/mod.rs b/crates/core/tedge_lib/src/config/mod.rs index a97d352f..8e05163b 100644 --- a/crates/core/tedge_lib/src/config/mod.rs +++ b/crates/core/tedge_lib/src/config/mod.rs @@ -46,6 +46,9 @@ mod address; pub use crate::config::address::Address; +mod bytesize; +pub use crate::config::bytesize::ByteSize; + mod humantime; pub use crate::config::humantime::Humantime; |