protoflow_blocks/types/
delay_type.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// This is free and unencumbered software released into the public domain.

use crate::prelude::{Duration, FromStr, Range, String};

/// The type of delay (fixed or random) to apply to message relay.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum DelayType {
    #[cfg_attr(
        feature = "serde",
        serde(deserialize_with = "duration_str::deserialize_duration")
    )]
    Fixed(Duration),

    Random(Range<Duration>),
}

impl Default for DelayType {
    fn default() -> Self {
        Self::Fixed(Duration::from_secs(1))
    }
}

impl FromStr for DelayType {
    type Err = InvalidDelayType;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        // TODO: parse random range parameters as well
        Ok(match input.trim() {
            "" => Self::default(),
            "random" | "rand" => Self::Random(Range {
                start: Duration::from_secs_f64(0.),
                end: Duration::from_secs_f64(1.),
            }),
            input => duration_str::parse_std(input).map(Self::Fixed)?,
        })
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum InvalidDelayType {
    InvalidDuration(String),
}

impl From<String> for InvalidDelayType {
    fn from(input: String) -> Self {
        InvalidDelayType::InvalidDuration(input)
    }
}