diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2ee1fe8..e80c0ae 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -24,6 +24,7 @@ jobs: nix develop -c cargo run --no-default-features --example json nix develop -c cargo run --no-default-features --example rename-patch-struct nix develop -c cargo run --no-default-features --example patch-attr + nix develop -c cargo run --no-default-features --example time nix develop -c cargo test --no-default-features - name: Test with std features diff --git a/struct-patch/Cargo.toml b/struct-patch/Cargo.toml index eda0d68..44eed57 100644 --- a/struct-patch/Cargo.toml +++ b/struct-patch/Cargo.toml @@ -17,6 +17,8 @@ struct-patch-derive = { version = "=0.8.5", path = "../struct-patch-derive" } serde_json = "1.0" serde = { version = "1", features = ["derive"] } serde_with = "3.9.0" +toml = "0.8.19" +humantime-serde = "1.1.1" [features] default = ["status", "op"] diff --git a/struct-patch/examples/time.rs b/struct-patch/examples/time.rs new file mode 100644 index 0000000..b284eb3 --- /dev/null +++ b/struct-patch/examples/time.rs @@ -0,0 +1,34 @@ +use serde::{Deserialize}; +use std::time::Duration; +use struct_patch::Patch; + +#[derive(Deserialize, Clone, Debug, Patch)] +#[patch(name = "FileConfig", attribute(derive(Deserialize, Debug)))] +struct Config { + #[serde(with = "humantime_serde")] + #[patch(attribute(serde(with = "humantime_serde")))] + time: Duration, +} + +fn main() { + let config = Config { + time: Duration::from_millis(500), + }; + + let patch: FileConfig = toml::from_str("time = \"200ms\"").unwrap(); + + let mut patched = config.clone(); + patched.apply(patch); + assert_eq!(patched.time, Duration::from_millis(200)); + + // NOTE + // Following code does not work, because `humantime_serde` does not allow `Option<>` field + // anymore. + // https://github.com/jean-airoldie/humantime-serde/issues/13 + // + // let empty_patch: FileConfig = toml::from_str("").unwrap(); + + // let mut patched = config.clone(); + // patched.apply(empty_patch); + // assert_eq!(patched.time, Duration::from_millis(500)); +}