Skip to content

Commit 752f52b

Browse files
authored
fix(model): resolve deserialization issue with figment (#15)
* make it compatible with `figment`
1 parent 7e818ff commit 752f52b

3 files changed

Lines changed: 83 additions & 9 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ statrs = { version = "0.17.1", optional = true}
2828

2929
[dev-dependencies]
3030
serde_json = "1.0"
31+
figment = {version = "0.10.19", features = ["json"]}
3132

3233

3334
[features]

src/lib.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,4 +464,76 @@ mod test {
464464
Some((Bandwidth::from_bps(12132938), Duration::from_millis(100)))
465465
);
466466
}
467+
468+
#[test]
469+
#[cfg(feature = "human")]
470+
fn test_compatibility_with_figment() {
471+
use figment::{
472+
providers::{Format, Json},
473+
Figment,
474+
};
475+
476+
let config = r##"
477+
{
478+
"RepeatedBwPatternConfig": {
479+
"pattern": [
480+
{"TraceBwConfig": [["25ms",["10Mbps", "20Mbps"]],["2ms",["11Mbps", "23Mbps"]]]},
481+
{"SawtoothBwConfig": {
482+
"bottom" : "10Mbps",
483+
"top" : "20Mbps",
484+
"step" : "1ms",
485+
"interval" : "10ms",
486+
"duty_ratio" : 0.5
487+
}
488+
}
489+
],
490+
"count": 0
491+
}
492+
}
493+
"##;
494+
495+
let trace: Box<dyn BwTraceConfig> = Figment::new()
496+
.merge(Json::string(config))
497+
.extract()
498+
.unwrap();
499+
500+
let mut model = trace.into_model();
501+
502+
assert_eq!(
503+
model.next_bw(),
504+
Some((Bandwidth::from_mbps(10), Duration::from_millis(25)))
505+
);
506+
assert_eq!(
507+
model.next_bw(),
508+
Some((Bandwidth::from_mbps(20), Duration::from_millis(25)))
509+
);
510+
assert_eq!(
511+
model.next_bw(),
512+
Some((Bandwidth::from_mbps(11), Duration::from_millis(2)))
513+
);
514+
assert_eq!(
515+
model.next_bw(),
516+
Some((Bandwidth::from_mbps(23), Duration::from_millis(2)))
517+
);
518+
assert_eq!(
519+
model.next_bw(),
520+
Some((Bandwidth::from_mbps(10), Duration::from_millis(1)))
521+
);
522+
assert_eq!(
523+
model.next_bw(),
524+
Some((Bandwidth::from_mbps(12), Duration::from_millis(1)))
525+
);
526+
assert_eq!(
527+
model.next_bw(),
528+
Some((Bandwidth::from_mbps(14), Duration::from_millis(1)))
529+
);
530+
assert_eq!(
531+
model.next_bw(),
532+
Some((Bandwidth::from_mbps(16), Duration::from_millis(1)))
533+
);
534+
assert_eq!(
535+
model.next_bw(),
536+
Some((Bandwidth::from_mbps(18), Duration::from_millis(1)))
537+
);
538+
}
467539
}

src/model/bw.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -661,20 +661,21 @@ impl<'de> Deserialize<'de> for TraceBwConfig {
661661
let mut pattern = Vec::new();
662662

663663
while let Some((duration_str, bandwidths_str)) =
664-
seq.next_element::<(&str, Vec<&str>)>()?
664+
seq.next_element::<(String, Vec<String>)>()?
665665
{
666-
let duration = humantime_serde::re::humantime::parse_duration(duration_str)
667-
.map_err(|e| {
668-
serde::de::Error::custom(format!(
669-
"Failed to parse duration '{}': {}",
670-
duration_str, e
671-
))
672-
})?;
666+
let duration =
667+
humantime_serde::re::humantime::parse_duration(duration_str.as_str())
668+
.map_err(|e| {
669+
serde::de::Error::custom(format!(
670+
"Failed to parse duration '{}': {}",
671+
duration_str, e
672+
))
673+
})?;
673674

674675
let bandwidths = bandwidths_str
675676
.into_iter()
676677
.map(|b| {
677-
human_bandwidth::parse_bandwidth(b).map_err(|e| {
678+
human_bandwidth::parse_bandwidth(&b).map_err(|e| {
678679
serde::de::Error::custom(format!(
679680
"Failed to parse bandwidth '{}': {}",
680681
b, e

0 commit comments

Comments
 (0)