rinex/observation/
flag.rs

1use crate::prelude::ParsingError;
2use std::str::FromStr;
3
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6
7/// `EpochFlag` validates an epoch,
8/// or describes possible events that occurred
9#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11pub enum EpochFlag {
12    /// Epoch is sane
13    Ok,
14    /// Power failure since previous epoch
15    PowerFailure,
16    /// Antenna is being moved at current epoch
17    AntennaBeingMoved,
18    /// Site has changed, received has moved since last epoch
19    NewSiteOccupation,
20    /// New information to come after this epoch
21    HeaderInformationFollows,
22    /// External event - significant event in this epoch
23    ExternalEvent,
24    /// Cycle slip at this epoch
25    CycleSlip,
26}
27
28impl Default for EpochFlag {
29    fn default() -> Self {
30        Self::Ok
31    }
32}
33
34impl EpochFlag {
35    /// Returns true if [`Epoch`] attached to this flag is valid
36    pub fn is_ok(self) -> bool {
37        self == Self::Ok
38    }
39}
40
41impl FromStr for EpochFlag {
42    type Err = ParsingError;
43    fn from_str(s: &str) -> Result<Self, Self::Err> {
44        match s {
45            "0" => Ok(EpochFlag::Ok),
46            "1" => Ok(EpochFlag::PowerFailure),
47            "2" => Ok(EpochFlag::AntennaBeingMoved),
48            "3" => Ok(EpochFlag::NewSiteOccupation),
49            "4" => Ok(EpochFlag::HeaderInformationFollows),
50            "5" => Ok(EpochFlag::ExternalEvent),
51            "6" => Ok(EpochFlag::CycleSlip),
52            _ => Err(ParsingError::EpochFlag),
53        }
54    }
55}
56
57impl std::fmt::Display for EpochFlag {
58    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
59        match self {
60            EpochFlag::Ok => "0".fmt(f),
61            EpochFlag::PowerFailure => "1".fmt(f),
62            EpochFlag::AntennaBeingMoved => "2".fmt(f),
63            EpochFlag::NewSiteOccupation => "3".fmt(f),
64            EpochFlag::HeaderInformationFollows => "4".fmt(f),
65            EpochFlag::ExternalEvent => "5".fmt(f),
66            EpochFlag::CycleSlip => "6".fmt(f),
67        }
68    }
69}
70
71#[cfg(test)]
72mod test {
73    use super::*;
74    #[test]
75    fn test_default() {
76        assert_eq!(EpochFlag::default(), EpochFlag::Ok);
77    }
78    #[test]
79    fn from_str() {
80        assert_eq!(EpochFlag::from_str("0").unwrap(), EpochFlag::Ok);
81        assert_eq!(EpochFlag::from_str("1").unwrap(), EpochFlag::PowerFailure);
82        assert_eq!(
83            EpochFlag::from_str("2").unwrap(),
84            EpochFlag::AntennaBeingMoved
85        );
86        assert_eq!(
87            EpochFlag::from_str("3").unwrap(),
88            EpochFlag::NewSiteOccupation
89        );
90        assert_eq!(
91            EpochFlag::from_str("4").unwrap(),
92            EpochFlag::HeaderInformationFollows
93        );
94        assert_eq!(EpochFlag::from_str("5").unwrap(), EpochFlag::ExternalEvent);
95        assert_eq!(EpochFlag::from_str("6").unwrap(), EpochFlag::CycleSlip);
96        assert!(EpochFlag::from_str("7").is_err());
97    }
98    #[test]
99    fn to_str() {
100        assert_eq!(format!("{}", EpochFlag::Ok), "0");
101        assert_eq!(format!("{}", EpochFlag::PowerFailure), "1");
102        assert_eq!(format!("{}", EpochFlag::AntennaBeingMoved), "2");
103        assert_eq!(format!("{}", EpochFlag::NewSiteOccupation), "3");
104        assert_eq!(format!("{}", EpochFlag::HeaderInformationFollows), "4");
105        assert_eq!(format!("{}", EpochFlag::ExternalEvent), "5");
106        assert_eq!(format!("{}", EpochFlag::CycleSlip), "6");
107    }
108}