Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
nix develop .#ci -c cargo run --no-default-features --example time
nix develop .#ci -c cargo run --no-default-features --example clap
nix develop .#ci -c cargo run --no-default-features --features=nesting --example nesting
nix develop .#ci -c cargo run --no-default-features --features=nesting --example nested-linear-map
nix develop .#ci -c cargo run --no-default-features --features=option --example option
nix develop .#ci -c cargo test --no-default-features
Expand Down
1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ serde_with = "3.9.0"
toml = "0.9"
humantime-serde = "1.1.1"
clap = { version = "4.4.7", features = ["derive"] }
linear-map = "1.2.0"

[features]
default = ["status", "op"]
Expand Down
98 changes: 98 additions & 0 deletions lib/examples/nested-linear-map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use struct_patch::Patch;
use linear_map::{linear_map, LinearMap};

#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd)]
#[repr(u8)]
pub enum Key {
TEST1 = 1,
TEST2 = 2

}

#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd)]
#[repr(u8)]
pub enum KeyB {
B1 = 1,
B2 = 2
}

#[derive(Debug, Clone, PartialEq, Eq, Patch)]
#[patch(attribute(derive(Debug, Default)))]
pub struct ExampleB {
pub foo: Option<u8>,
}

#[derive(Debug, Clone, Default, PartialEq, Patch)]
#[patch(attribute(derive(Debug, Default )))]
pub struct InnerConfig {
pub example_a: LinearMap<Key, u8>,
pub example_b: LinearMap<KeyB, ExampleB>,
}

#[cfg(feature = "nesting")]
#[derive(Debug, Clone, Default, PartialEq, Patch)]
#[patch(attribute(derive(Debug, Default)))]
pub struct Config {
#[patch(nesting)]
pub inner: InnerConfig,
}

#[cfg(not(feature = "nesting"))]
fn main() {}

#[cfg(feature = "nesting")]
fn main() {
let config_a = Config::default();
let config_b = Config {
inner: InnerConfig {
example_a: linear_map!{
Key::TEST1 => 1,
Key::TEST2 => 2,
},
..Default::default()
},
};

let patch: ConfigPatch = config_b.clone().into_patch_by_diff(config_a);
assert_eq!(
format!("{patch:?}"),
"ConfigPatch { inner: InnerConfigPatch { example_a: Some({TEST1: 1, TEST2: 2}), example_b: None } }"
);

let mut config = Config::default();
config.apply(patch);

assert_eq!(config, config_b);

// --- test nesting with ExampleB ---

let patch_b: ConfigPatch = ConfigPatch {
inner: InnerConfigPatch {
example_b: Some(
linear_map!{
KeyB::B1 => ExampleB {
foo: Some(7),
},
}
),
..Default::default()
},
};
config.apply(patch_b);
assert_eq!(
format!("{config:?}"),
"Config { inner: InnerConfig { example_a: {TEST1: 1, TEST2: 2}, example_b: {B1: ExampleB { foo: Some(7) }} } }"
);

let _patch_under_example_b = Some(
linear_map!{
KeyB::B2 => ExampleB {
foo: Some(100),
},
}
);

// TODO
// nested map feature
config.apply(_patch_under_example_b);
}
Loading