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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use super::Config;
use std::convert::TryFrom;

/// Message delivery guarantees for the routing (defined per port channel)
#[repr(u8)]
#[derive(Debug, Clone, PartialEq, Copy)]
pub enum DeliveryGuarantee {
    /// best effort: there is no guarantee that a message will be routed, the default
    BestEffort = 0,
    /// At Least Once the message should be received at the destination
    AtLeastOnce = 2,
}

impl DeliveryGuarantee {
    /// Does the selected delivery guarantee requires an acknowledgment?
    pub fn requires_acknowledgment(&self) -> bool {
        match self {
            DeliveryGuarantee::BestEffort => false,
            _ => true,
        }
    }
}

impl Default for DeliveryGuarantee {
    fn default() -> Self {
        DeliveryGuarantee::BestEffort
    }
}

impl TryFrom<Config> for DeliveryGuarantee {
    type Error = anyhow::Error;
    fn try_from(value: Config) -> Result<Self, Self::Error> {
        DeliveryGuarantee::try_from(&value)
    }
}

impl TryFrom<&Config> for DeliveryGuarantee {
    type Error = anyhow::Error;
    fn try_from(value: &Config) -> Result<Self, Self::Error> {
        if let Config::U8(number) = value {
            match number {
                0 => Ok(DeliveryGuarantee::BestEffort),
                2 => Ok(DeliveryGuarantee::AtLeastOnce),
                _ => bail!("number out of range"),
            }
        } else {
            bail!("Config not of type Config::U8")
        }
    }
}

impl From<DeliveryGuarantee> for Config {
    fn from(value: DeliveryGuarantee) -> Self {
        Config::U8(value as u8)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::error::Error;

    #[test]
    fn delivery_guarantee_to_config() {
        let delivery_guarantee = DeliveryGuarantee::AtLeastOnce;
        let config = Config::from(delivery_guarantee);
        assert_eq!(config, Config::U8(DeliveryGuarantee::AtLeastOnce as u8));
    }

    #[test]
    fn config_to_delivery_guarantee() -> Result<(), Box<dyn Error>> {
        let config = Config::U8(DeliveryGuarantee::AtLeastOnce as u8);
        let delivery_guarantee = DeliveryGuarantee::try_from(config)?;
        assert_eq!(delivery_guarantee, DeliveryGuarantee::AtLeastOnce);
        Ok(())
    }

    #[test]
    fn failed_config_to_delivery_guarantee() -> Result<(), Box<dyn Error>> {
        let config = Config::U8(99);
        let delivery_guarantee = DeliveryGuarantee::try_from(config);
        assert!(delivery_guarantee.is_err());
        Ok(())
    }
}