doris_rs/
frequency.rs

1#[cfg(feature = "serde")]
2use serde::{Deserialize, Serialize};
3
4use crate::error::ParsingError;
5
6#[derive(Debug, Copy, Default, Clone, PartialEq, PartialOrd, Hash, Ord, Eq)]
7#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8pub enum Frequency {
9    /// DORIS #1 frequency
10    #[default]
11    DORIS1,
12
13    /// DORIS #2 frequency
14    DORIS2,
15}
16
17impl From<u8> for Frequency {
18    fn from(val: u8) -> Self {
19        match val {
20            2 => Self::DORIS2,
21            _ => Self::DORIS1,
22        }
23    }
24}
25
26impl std::str::FromStr for Frequency {
27    type Err = ParsingError;
28
29    fn from_str(s: &str) -> Result<Self, Self::Err> {
30        if s.eq("1") {
31            return Ok(Self::DORIS1);
32        } else if s.eq("2") {
33            return Ok(Self::DORIS2);
34        } else {
35            return Err(ParsingError::Frequency);
36        }
37    }
38}
39
40impl std::fmt::Display for Frequency {
41    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
42        match self {
43            Self::DORIS1 => write!(f, "{}", '1'),
44            Self::DORIS2 => write!(f, "{}", '2'),
45        }
46    }
47}
48
49impl Frequency {
50    /// Returns frequency value in Hertz
51    pub fn frequency_hz(&self) -> f64 {
52        match self {
53            Self::DORIS1 => 1.0,
54            Self::DORIS2 => 2.0,
55        }
56    }
57}
58
59#[cfg(test)]
60mod test {
61    use super::Frequency;
62    use std::str::FromStr;
63
64    #[test]
65    fn frequency_parsing() {
66        for (value, expected) in [("1", Frequency::DORIS1), ("2", Frequency::DORIS2)] {
67            let freq = Frequency::from_str(value).unwrap_or_else(|e| {
68                panic!("failed to parse frequency from \"{}\"", value);
69            });
70
71            assert_eq!(freq, expected, "wrong value for {}", value);
72        }
73    }
74}