From fa21fb0f35620e256fc178542f95f035f5ba6be5 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 24 Apr 2022 22:47:15 +0100 Subject: [PATCH] fake time: Fix config deserialisation Option works weirdly, empirically. This (via a Vec) is what we want. Signed-off-by: Ian Jackson --- src/fake-time.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/fake-time.rs b/src/fake-time.rs index c28bcaab..c71109c2 100644 --- a/src/fake-time.rs +++ b/src/fake-time.rs @@ -14,9 +14,31 @@ type Micros = u64; pub struct FakeTimeConfig(Option); #[derive(Deserialize,Serialize,Debug,Clone,Default)] -#[serde(transparent)] +#[serde(into="Vec", try_from="Vec")] pub struct FakeTimeSpec(Option); +#[derive(Error,Debug)] +#[error("invalid fake time: must be list of 0 or 1 numbers (ms)")] +pub struct InvalidFakeTime; + +impl TryFrom> for FakeTimeSpec { + type Error = InvalidFakeTime; + #[throws(InvalidFakeTime)] + fn try_from(l: Vec) -> FakeTimeSpec { + FakeTimeSpec(match &*l { + [] => None, + &[ms] => Some(ms), + _ => throw!(InvalidFakeTime), + }) + } +} + +impl Into> for FakeTimeSpec { + fn into(self) -> Vec { + self.0.into_iter().collect() + } +} + #[derive(Debug)] pub struct GlobalClock { fakeable: Option>>, -- 2.30.2