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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! GNSS constellations
use hifitime::TimeScale;
use thiserror::Error;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Constellation parsing & identification related errors
#[derive(Error, Clone, Debug, PartialEq)]
pub enum ParsingError {
    #[error("unknown constellation \"{0}\"")]
    Unknown(String),
}

/// Describes all known `GNSS` constellations
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Constellation {
    /// `GPS` american constellation,
    #[default]
    GPS,
    /// `Glonass` russian constellation
    Glonass,
    /// `BeiDou` chinese constellation
    BeiDou,
    /// `QZSS` japanese constellation
    QZSS,
    /// `Galileo` european constellation
    Galileo,
    /// `IRNSS` constellation, renamed "NavIC"
    IRNSS,
    /// American augmentation system,
    WAAS,
    /// European augmentation system
    EGNOS,
    /// Japanese MTSAT Space Based augmentation system
    MSAS,
    /// Indian augmentation system
    GAGAN,
    /// Chinese augmentation system
    BDSBAS,
    /// South Korean augmentation system
    KASS,
    /// Russian augmentation system
    SDCM,
    /// South African augmentation system
    ASBAS,
    /// Autralia / NZ augmentation system
    SPAN,
    /// SBAS is used to describe SBAS (augmentation)
    /// vehicles without much more information
    SBAS,
    /// Australia-NZ Geoscience system
    AusNZ,
    /// Group Based SBAS
    GBAS,
    /// Nigerian SBAS
    NSAS,
    /// Algerian SBAS
    ASAL,
    /// `Mixed` for Mixed constellations
    /// RINEX files description
    Mixed,
}

impl std::fmt::Display for Constellation {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{:X}", self)
    }
}

impl Constellation {
    /// Returns true if Self is an augmentation system
    pub fn is_sbas(&self) -> bool {
        matches!(
            *self,
            Constellation::WAAS
                | Constellation::KASS
                | Constellation::BDSBAS
                | Constellation::EGNOS
                | Constellation::GAGAN
                | Constellation::SDCM
                | Constellation::ASBAS
                | Constellation::SPAN
                | Constellation::MSAS
                | Constellation::NSAS
                | Constellation::ASAL
                | Constellation::AusNZ
                | Constellation::SBAS
        )
    }
    pub(crate) fn is_mixed(&self) -> bool {
        *self == Constellation::Mixed
    }
    /// Returns associated time scale. Returns None
    /// if related time scale is not supported.
    pub fn timescale(&self) -> Option<TimeScale> {
        match self {
            Self::GPS | Self::QZSS => Some(TimeScale::GPST),
            Self::Galileo => Some(TimeScale::GST),
            Self::BeiDou => Some(TimeScale::BDT),
            Self::Glonass => Some(TimeScale::UTC),
            c => {
                if c.is_sbas() {
                    Some(TimeScale::GPST)
                } else {
                    None
                }
            },
        }
    }
}

impl std::str::FromStr for Constellation {
    type Err = ParsingError;
    fn from_str(string: &str) -> Result<Self, Self::Err> {
        let s = string.trim().to_lowercase();
        match s.as_str() {
            "g" | "gps" => Ok(Self::GPS),
            "c" | "bds" => Ok(Self::BeiDou),
            "e" | "gal" => Ok(Self::Galileo),
            "r" | "glo" => Ok(Self::Glonass),
            "j" | "qzss" => Ok(Self::QZSS),
            "i" | "irnss" => Ok(Self::IRNSS),
            "s" | "sbas" => Ok(Self::SBAS),
            "m" | "mixed" => Ok(Self::Mixed),
            "ausnz" => Ok(Self::AusNZ),
            "egnos" => Ok(Self::EGNOS),
            "waas" => Ok(Self::WAAS),
            "kass" => Ok(Self::KASS),
            "gagan" => Ok(Self::GAGAN),
            "asbas" => Ok(Self::ASBAS),
            "nsas" => Ok(Self::NSAS),
            "asal" => Ok(Self::ASAL),
            "msas" => Ok(Self::MSAS),
            "span" => Ok(Self::SPAN),
            "gbas" => Ok(Self::GBAS),
            "sdcm" => Ok(Self::SDCM),
            "bdsbas" => Ok(Self::BDSBAS),
            _ if s.contains("gps") => Ok(Self::GPS),
            _ if s.contains("glonass") => Ok(Self::Glonass),
            _ if s.contains("beidou") => Ok(Self::BeiDou),
            _ if s.contains("galileo") => Ok(Self::Galileo),
            _ if s.contains("qzss") => Ok(Self::QZSS),
            _ if s.contains("sbas") | s.contains("geo") => Ok(Self::SBAS),
            _ if s.contains("irnss") | s.contains("navic") => Ok(Self::IRNSS),
            _ if s.contains("mix") => Ok(Self::Mixed),
            _ => Err(ParsingError::Unknown(string.to_string())),
        }
    }
}

impl std::fmt::LowerHex for Constellation {
    /*
     * {:x}: formats Self as single letter standard code
     */
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::GPS => write!(f, "G"),
            Self::Glonass => write!(f, "R"),
            Self::Galileo => write!(f, "E"),
            Self::BeiDou => write!(f, "C"),
            Self::QZSS => write!(f, "J"),
            Self::IRNSS => write!(f, "I"),
            c => {
                if c.is_sbas() {
                    write!(f, "S")
                } else if c.is_mixed() {
                    write!(f, "M")
                } else {
                    Err(std::fmt::Error)
                }
            },
        }
    }
}

impl std::fmt::UpperHex for Constellation {
    /*
     * {:X} formats Self as 3 letter standard code
     */
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::GPS => write!(f, "GPS"),
            Self::Glonass => write!(f, "GLO"),
            Self::Galileo => write!(f, "GAL"),
            Self::BeiDou => write!(f, "BDS"),
            Self::QZSS => write!(f, "QZSS"),
            Self::IRNSS => write!(f, "IRNSS"),
            Self::WAAS => write!(f, "WAAS"),
            Self::AusNZ => write!(f, "AUSNZ"),
            Self::EGNOS => write!(f, "EGNOS"),
            Self::KASS => write!(f, "KASS"),
            Self::GAGAN => write!(f, "GAGAN"),
            Self::GBAS => write!(f, "GBAS"),
            Self::NSAS => write!(f, "NSAS"),
            Self::MSAS => write!(f, "MSAS"),
            Self::SPAN => write!(f, "SPAN"),
            Self::SDCM => write!(f, "SDCM"),
            Self::BDSBAS => write!(f, "BDSBAS"),
            Self::ASBAS => write!(f, "ASBAS"),
            Self::ASAL => write!(f, "ASAL"),
            Self::Mixed => write!(f, "MIXED"),
            _ => Err(std::fmt::Error),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use hifitime::TimeScale;
    use std::str::FromStr;
    #[test]
    fn from_str() {
        for (desc, expected) in vec![
            ("G", Ok(Constellation::GPS)),
            ("GPS", Ok(Constellation::GPS)),
            ("R", Ok(Constellation::Glonass)),
            ("GLO", Ok(Constellation::Glonass)),
            ("J", Ok(Constellation::QZSS)),
            ("M", Ok(Constellation::Mixed)),
            ("WAAS", Ok(Constellation::WAAS)),
            ("KASS", Ok(Constellation::KASS)),
            ("GBAS", Ok(Constellation::GBAS)),
            ("NSAS", Ok(Constellation::NSAS)),
            ("SPAN", Ok(Constellation::SPAN)),
            ("EGNOS", Ok(Constellation::EGNOS)),
            ("ASBAS", Ok(Constellation::ASBAS)),
            ("MSAS", Ok(Constellation::MSAS)),
            ("GAGAN", Ok(Constellation::GAGAN)),
            ("BDSBAS", Ok(Constellation::BDSBAS)),
            ("ASAL", Ok(Constellation::ASAL)),
            ("SDCM", Ok(Constellation::SDCM)),
        ] {
            assert_eq!(
                Constellation::from_str(desc),
                expected,
                "failed to parse constellation from \"{}\"",
                desc
            );
        }

        for desc in ["X", "x", "GPX", "gpx", "unknown", "blah"] {
            assert!(Constellation::from_str(desc).is_err());
        }
    }
    #[test]
    fn test_sbas() {
        for sbas in ["WAAS", "KASS", "EGNOS", "ASBAS", "MSAS", "GAGAN", "ASAL"] {
            assert!(Constellation::from_str(sbas).unwrap().is_sbas());
        }
    }
    #[test]
    fn timescale() {
        for (gnss, expected) in [
            (Constellation::GPS, TimeScale::GPST),
            (Constellation::Galileo, TimeScale::GST),
            (Constellation::BeiDou, TimeScale::BDT),
        ] {
            assert_eq!(gnss.timescale(), Some(expected));
        }
    }
}