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
use std::fmt;

use crate::util::ParseError;

/// Enumeration of the different sources an H record can come from.
#[derive(Debug, PartialEq, Eq)]
pub enum DataSource {
    FVU,
    OfficialObserver,
    Pilot,
    Unrecognized(u8),
}

/// A header information record.
#[derive(Debug, PartialEq, Eq)]
pub struct HRecord<'a> {
    pub data_source: DataSource,
    pub mnemonic: &'a str,
    pub friendly_name: Option<&'a str>,
    pub data: &'a str,
}

impl DataSource {
    fn from_byte(byte: u8) -> Self {
        match byte {
            b'F' => DataSource::FVU,
            b'O' => DataSource::OfficialObserver,
            b'P' => DataSource::Pilot,
            _ => DataSource::Unrecognized(byte),
        }
    }

    fn to_byte(&self) -> u8 {
        match self {
            DataSource::FVU => b'F',
            DataSource::OfficialObserver => b'O',
            DataSource::Pilot => b'P',
            DataSource::Unrecognized(byte) => *byte,
        }
    }
}

impl<'a> HRecord<'a> {
    pub fn parse(line: &'a str) -> Result<Self, ParseError> {
        let bytes = line.as_bytes();
        assert_eq!(bytes[0], b'H');

        if bytes.len() < 6 {
            return Err(ParseError::SyntaxError);
        }
        if !line.bytes().take(5).all(|b| b.is_ascii()) {
            return Err(ParseError::NonASCIICharacters);
        }

        let data_source = DataSource::from_byte(bytes[1]);
        let mnemonic = &line[2..5];

        let friendly_name;
        let data;
        if let Some(colon_idx) = &line[5..].find(':') {
            let colon_idx = *colon_idx + 5;
            friendly_name = Some(&line[5..colon_idx]);
            data = &line[colon_idx + 1..];
        } else {
            friendly_name = None;
            data = &line[5..];
        }

        Ok(Self {
            data_source,
            mnemonic,
            friendly_name,
            data,
        })
    }
}

impl<'a> fmt::Display for HRecord<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // NB: not using DisplayOption, as the colon also disappears when friendly_name is None
        match self.friendly_name {
            Some(friendly_name) => write!(
                f,
                "H{source}{mnemonic}{friendly_name}:{data}",
                source = self.data_source.to_byte() as char,
                mnemonic = self.mnemonic,
                friendly_name = friendly_name,
                data = self.data
            ),
            None => write!(
                f,
                "H{source}{mnemonic}{data}",
                source = self.data_source.to_byte() as char,
                mnemonic = self.mnemonic,
                data = self.data
            ),
        }
    }
}

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

    #[test]
    fn hrecord_parse() {
        let sample_string = "HFGIDGLIDERID:D-KOOL";
        let parsed_record = HRecord::parse(sample_string).unwrap();
        let expected = HRecord {
            data_source: DataSource::FVU,
            mnemonic: "GID",
            friendly_name: Some("GLIDERID"),
            data: "D-KOOL",
        };

        assert_eq!(parsed_record, expected);
    }

    #[test]
    fn parse_with_missing_content() {
        assert!(HRecord::parse("H").is_err());
        assert!(HRecord::parse("HXXX").is_err());
    }

    #[test]
    fn parse_with_early_colon() {
        assert_eq!(
            HRecord::parse("H:00 a ").unwrap(),
            HRecord {
                data_source: DataSource::Unrecognized(b':'),
                mnemonic: "00 ",
                friendly_name: None,
                data: "a ",
            }
        );

        assert_eq!(
            HRecord::parse("HAaA :a").unwrap(),
            HRecord {
                data_source: DataSource::Unrecognized(b'A'),
                mnemonic: "aA ",
                friendly_name: Some(""),
                data: "a",
            }
        );
    }

    #[test]
    fn parse_with_invalid_char_boundary() {
        assert!(HRecord::parse("H\u{1107f}").is_err());
    }

    #[test]
    fn hrecord_format() {
        let expected_string = "HFGIDGLIDERID:D-KOOL";
        let record = HRecord {
            data_source: DataSource::FVU,
            mnemonic: "GID",
            friendly_name: Some("GLIDERID"),
            data: "D-KOOL",
        };

        assert_eq!(format!("{}", record), expected_string);
    }

    proptest! {
        #[test]
        #[allow(unused_must_use)]
        fn parse_doesnt_crash(s in "H\\PC*") {
            HRecord::parse(&s);
        }
    }
}