Skip to content

Commit

Permalink
fix(model): resolve deserialization issue with figment (#15)
Browse files Browse the repository at this point in the history
* make it compatible with `figment`
  • Loading branch information
Lethe10137 authored Dec 8, 2024
1 parent 7e818ff commit 752f52b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ statrs = { version = "0.17.1", optional = true}

[dev-dependencies]
serde_json = "1.0"
figment = {version = "0.10.19", features = ["json"]}


[features]
Expand Down
72 changes: 72 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,4 +464,76 @@ mod test {
Some((Bandwidth::from_bps(12132938), Duration::from_millis(100)))
);
}

#[test]
#[cfg(feature = "human")]
fn test_compatibility_with_figment() {
use figment::{
providers::{Format, Json},
Figment,
};

let config = r##"
{
"RepeatedBwPatternConfig": {
"pattern": [
{"TraceBwConfig": [["25ms",["10Mbps", "20Mbps"]],["2ms",["11Mbps", "23Mbps"]]]},
{"SawtoothBwConfig": {
"bottom" : "10Mbps",
"top" : "20Mbps",
"step" : "1ms",
"interval" : "10ms",
"duty_ratio" : 0.5
}
}
],
"count": 0
}
}
"##;

let trace: Box<dyn BwTraceConfig> = Figment::new()
.merge(Json::string(config))
.extract()
.unwrap();

let mut model = trace.into_model();

assert_eq!(
model.next_bw(),
Some((Bandwidth::from_mbps(10), Duration::from_millis(25)))
);
assert_eq!(
model.next_bw(),
Some((Bandwidth::from_mbps(20), Duration::from_millis(25)))
);
assert_eq!(
model.next_bw(),
Some((Bandwidth::from_mbps(11), Duration::from_millis(2)))
);
assert_eq!(
model.next_bw(),
Some((Bandwidth::from_mbps(23), Duration::from_millis(2)))
);
assert_eq!(
model.next_bw(),
Some((Bandwidth::from_mbps(10), Duration::from_millis(1)))
);
assert_eq!(
model.next_bw(),
Some((Bandwidth::from_mbps(12), Duration::from_millis(1)))
);
assert_eq!(
model.next_bw(),
Some((Bandwidth::from_mbps(14), Duration::from_millis(1)))
);
assert_eq!(
model.next_bw(),
Some((Bandwidth::from_mbps(16), Duration::from_millis(1)))
);
assert_eq!(
model.next_bw(),
Some((Bandwidth::from_mbps(18), Duration::from_millis(1)))
);
}
}
19 changes: 10 additions & 9 deletions src/model/bw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,20 +661,21 @@ impl<'de> Deserialize<'de> for TraceBwConfig {
let mut pattern = Vec::new();

while let Some((duration_str, bandwidths_str)) =
seq.next_element::<(&str, Vec<&str>)>()?
seq.next_element::<(String, Vec<String>)>()?
{
let duration = humantime_serde::re::humantime::parse_duration(duration_str)
.map_err(|e| {
serde::de::Error::custom(format!(
"Failed to parse duration '{}': {}",
duration_str, e
))
})?;
let duration =
humantime_serde::re::humantime::parse_duration(duration_str.as_str())
.map_err(|e| {
serde::de::Error::custom(format!(
"Failed to parse duration '{}': {}",
duration_str, e
))
})?;

let bandwidths = bandwidths_str
.into_iter()
.map(|b| {
human_bandwidth::parse_bandwidth(b).map_err(|e| {
human_bandwidth::parse_bandwidth(&b).map_err(|e| {
serde::de::Error::custom(format!(
"Failed to parse bandwidth '{}': {}",
b, e
Expand Down

0 comments on commit 752f52b

Please sign in to comment.