nmea 0.7.0

Simple NMEA 0183 parser
Documentation
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use core::str;

use arrayvec::ArrayString;
use chrono::{Duration, NaiveDate, NaiveTime};
use nom::{
    branch::alt,
    bytes::complete::{tag, take, take_until, take_while},
    character::complete::{char, digit1, one_of},
    combinator::{all_consuming, eof, map, map_parser, map_res},
    number::complete::{double, float},
    sequence::{terminated, tuple},
    IResult,
};

#[cfg(not(feature = "std"))]
#[allow(unused_imports)]
use num_traits::float::FloatCore;

use crate::Error;

pub fn parse_hms(i: &str) -> IResult<&str, NaiveTime> {
    map_res(
        tuple((
            map_res(take(2usize), parse_num::<u32>),
            map_res(take(2usize), parse_num::<u32>),
            map_parser(take_until(","), double),
        )),
        |(hour, minutes, sec)| -> core::result::Result<NaiveTime, &'static str> {
            if sec.is_sign_negative() {
                return Err("Invalid time: second is negative");
            }
            if hour >= 24 {
                return Err("Invalid time: hour >= 24");
            }
            if minutes >= 60 {
                return Err("Invalid time: min >= 60");
            }
            if sec >= 60. {
                return Err("Invalid time: sec >= 60");
            }
            NaiveTime::from_hms_nano_opt(
                hour,
                minutes,
                sec.trunc() as u32,
                (sec.fract() * 1_000_000_000f64).round() as u32,
            )
            .ok_or("Invalid time")
        },
    )(i)
}

/// The number of milliseconds in a second.
const MILLISECS_PER_SECOND: u32 = 1000;
/// The number of milliseconds in a minute.
const MILLISECS_PER_MINUTE: u32 = 60000;
/// The number of milliseconds in a hour.
const MILLISECS_PER_HOUR: u32 = 3600000;

/// Parses values like `125619,` and `125619.5,` to [`Duration`]
pub fn parse_duration_hms(i: &str) -> IResult<&str, Duration> {
    map_res(
        tuple((
            map_res(take(2usize), parse_num::<u8>),
            map_res(take(2usize), parse_num::<u8>),
            map_parser(take_until(","), float),
        )),
        |(hours, minutes, seconds)| -> core::result::Result<Duration, &'static str> {
            if hours >= 24 {
                return Err("Invalid time: hours >= 24");
            }
            if minutes >= 60 {
                return Err("Invalid time: minutes >= 60");
            }
            if !seconds.is_finite() {
                return Err("Invalid time: seconds is not finite");
            }
            if seconds < 0.0 {
                return Err("Invalid time: seconds is negative");
            }
            if seconds >= 60. {
                return Err("Invalid time: seconds >= 60");
            }

            // We don't have to use checked operations as above checks limits number of milliseconds
            // to value within i64 bounds.
            Ok(Duration::milliseconds(
                i64::from(hours) * i64::from(MILLISECS_PER_HOUR)
                    + i64::from(minutes) * i64::from(MILLISECS_PER_MINUTE)
                    + (seconds.trunc() as i64) * i64::from(MILLISECS_PER_SECOND)
                    + (seconds.fract() * 1_000f32).round() as i64,
            ))
        },
    )(i)
}

pub fn do_parse_lat_lon(i: &str) -> IResult<&str, (f64, f64)> {
    let (i, lat_deg) = map_res(take(2usize), parse_num::<u8>)(i)?;
    let (i, lat_min) = double(i)?;
    let (i, _) = char(',')(i)?;
    let (i, lat_dir) = one_of("NS")(i)?;
    let (i, _) = char(',')(i)?;
    let (i, lon_deg) = map_res(take(3usize), parse_num::<u8>)(i)?;
    let (i, lon_min) = double(i)?;
    let (i, _) = char(',')(i)?;
    let (i, lon_dir) = one_of("EW")(i)?;

    let mut lat = f64::from(lat_deg) + lat_min / 60.;
    if lat_dir == 'S' {
        lat = -lat;
    }
    let mut lon = f64::from(lon_deg) + lon_min / 60.;
    if lon_dir == 'W' {
        lon = -lon;
    }

    Ok((i, (lat, lon)))
}

/// Parses the variation between magnetic north and true north.
///
/// The angle returned will be positive or negative depending on
/// the East or West direction.<br>
/// E.g:<br>
/// "14.2,E" => 14.2 <br>
/// "14.2,W" => -14.2 <br>
pub fn do_parse_magnetic_variation(i: &str) -> IResult<&str, f32> {
    let (i, variation_deg) = float(i)?;
    let (i, _) = char(',')(i)?;
    let (i, direction) = one_of("EW")(i)?;
    let variation_deg = match direction {
        'E' => variation_deg,
        'W' => -variation_deg,
        _ => unreachable!(),
    };
    Ok((i, variation_deg))
}

pub(crate) fn parse_lat_lon(i: &str) -> IResult<&str, Option<(f64, f64)>> {
    alt((map(tag(",,,"), |_| None), map(do_parse_lat_lon, Some)))(i)
}

pub(crate) fn parse_magnetic_variation(i: &str) -> IResult<&str, Option<f32>> {
    alt((
        map(tag(","), |_| None),
        map(do_parse_magnetic_variation, Some),
    ))(i)
}

pub(crate) fn parse_date(i: &str) -> IResult<&str, NaiveDate> {
    map_res(
        tuple((
            map_res(take(2usize), parse_num::<u8>),
            map_res(take(2usize), parse_num::<u8>),
            map_res(take(2usize), parse_num::<u8>),
        )),
        |data| -> Result<NaiveDate, &'static str> {
            let (day, month, year) = (u32::from(data.0), u32::from(data.1), i32::from(data.2));

            // We only receive a 2digit year code in this message, this has the potential
            // to be ambiguous regarding the year. We assume that anything above 83 is 1900's, and
            // anything above 0 is 2000's.
            //
            // The reason for 83 is that NMEA0183 was released in 1983.
            // Parsing dates from ZDA messages is preferred, since it includes a 4 digit year.
            let year = match year {
                83..=99 => year + 1900,
                _ => year + 2000,
            };

            if !(1..=12).contains(&month) {
                return Err("Invalid month < 1 or > 12");
            }
            if !(1..=31).contains(&day) {
                return Err("Invalid day < 1 or > 31");
            }
            NaiveDate::from_ymd_opt(year, month, day).ok_or("Invalid date")
        },
    )(i)
}

pub(crate) fn parse_num<I: str::FromStr>(data: &str) -> Result<I, &'static str> {
    data.parse::<I>().map_err(|_| "parse of number failed")
}

pub(crate) fn parse_float_num<T: str::FromStr>(input: &str) -> Result<T, &'static str> {
    str::parse::<T>(input).map_err(|_| "parse of float number failed")
}

pub(crate) fn number<T: str::FromStr>(i: &str) -> IResult<&str, T> {
    map_res(digit1, parse_num)(i)
}

pub(crate) fn parse_number_in_range<T>(
    i: &str,
    lower_bound: T,
    upper_bound_inclusive: T,
) -> IResult<&str, T>
where
    T: PartialOrd + str::FromStr,
{
    map_res(number::<T>, |parsed_num| {
        if parsed_num < lower_bound || parsed_num > upper_bound_inclusive {
            return Err("Parsed number is outside of the expected range");
        }
        Ok(parsed_num)
    })(i)
}

/// Parses a given `&str` slice to an owned `ArrayString` with a given `MAX_LEN`.
///
/// # Errors
///
/// If `&str` length > `MAX_LEN` it returns a [`Error::ParameterLength`] error.
pub(crate) fn array_string<const MAX_LEN: usize>(
    string: &str,
) -> Result<ArrayString<MAX_LEN>, Error> {
    ArrayString::from(string).map_err(|_| Error::ParameterLength {
        max_length: MAX_LEN,
        parameter_length: string.len(),
    })
}

pub(crate) fn parse_until_end(input: &str) -> IResult<&str, &str> {
    all_consuming(terminated(take_while(|_| true), eof))(input)
}

#[cfg(test)]
mod tests {

    use approx::assert_relative_eq;

    use super::*;

    #[test]
    fn test_do_parse_lat_lon() {
        let (_, lat_lon) = do_parse_lat_lon("4807.038,N,01131.324,E").unwrap();
        assert_relative_eq!(lat_lon.0, 48. + 7.038 / 60.);
        assert_relative_eq!(lat_lon.1, 11. + 31.324 / 60.);
    }

    #[test]
    fn test_parse_hms() {
        use chrono::Timelike;
        let (_, time) = parse_hms("125619,").unwrap();
        assert_eq!(time.hour(), 12);
        assert_eq!(time.minute(), 56);
        assert_eq!(time.second(), 19);
        assert_eq!(time.nanosecond(), 0);
        let (_, time) = parse_hms("125619.5,").unwrap();
        assert_eq!(time.hour(), 12);
        assert_eq!(time.minute(), 56);
        assert_eq!(time.second(), 19);
        assert_eq!(time.nanosecond(), 500_000_000);
    }

    #[test]
    fn test_parse_duration_hms() {
        let (_, time) = parse_duration_hms("125619,").unwrap();
        assert_eq!(time.num_hours(), 12);
        assert_eq!(time.num_minutes(), 12 * 60 + 56);
        assert_eq!(time.num_seconds(), 12 * 60 * 60 + 56 * 60 + 19);
        assert_eq!(
            time.num_nanoseconds().unwrap(),
            (12 * 60 * 60 + 56 * 60 + 19) * 1_000_000_000
        );
        let (_, time) = parse_duration_hms("125619.5,").unwrap();
        assert_eq!(time.num_hours(), 12);
        assert_eq!(time.num_minutes(), 12 * 60 + 56);
        assert_eq!(time.num_seconds(), 12 * 60 * 60 + 56 * 60 + 19);
        assert_eq!(
            time.num_nanoseconds().unwrap(),
            (12 * 60 * 60 + 56 * 60 + 19) * 1_000_000_000 + 500_000_000
        );
    }

    #[test]
    fn test_parse_date() {
        let (_, date) = parse_date("180283").unwrap();
        assert_eq!(
            date,
            NaiveDate::from_ymd_opt(1983, 2, 18).expect("invalid time")
        );

        let (_, date) = parse_date("180299").unwrap();
        assert_eq!(
            date,
            NaiveDate::from_ymd_opt(1999, 2, 18).expect("invalid time")
        );

        let (_, date) = parse_date("311200").unwrap();
        assert_eq!(
            date,
            NaiveDate::from_ymd_opt(2000, 12, 31).expect("invalid time")
        );

        let (_, date) = parse_date("311282").unwrap();
        assert_eq!(
            date,
            NaiveDate::from_ymd_opt(2082, 12, 31).expect("invalid time")
        );
    }

    #[test]
    fn test_parse_magnetic_variation() {
        let (_, res) = parse_magnetic_variation("12,E").unwrap();
        assert_relative_eq!(res.unwrap(), 12.0);
        let (_, res) = parse_magnetic_variation("12,W").unwrap();
        assert_relative_eq!(res.unwrap(), -12.0);

        let (_, res) = parse_magnetic_variation(",").unwrap();
        assert!(res.is_none());
        let (_, res) = parse_magnetic_variation(",,").unwrap();
        assert!(res.is_none());
        let (_, res) = parse_magnetic_variation(",W").unwrap();
        assert!(res.is_none());

        //missing direction
        let result = parse_magnetic_variation("12,");
        assert!(result.is_err());
        //illegal character for direction
        let result = parse_magnetic_variation("12,Q");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_array_string() {
        let result = array_string::<5>("12345");
        assert!(result.is_ok());

        let err_expected = Error::ParameterLength {
            max_length: 5,
            parameter_length: 6,
        };
        let result = array_string::<5>("123456");
        assert_eq!(result, Err(err_expected));
    }

    #[test]
    fn test_parse_number_in_range() {
        let result = parse_number_in_range::<u8>("12", 10, 20);
        assert!(result.is_ok());

        let result = parse_number_in_range::<u8>("9", 10, 20);
        let nom_error_expected = nom::error::Error::new("9", nom::error::ErrorKind::MapRes);
        let err = result.unwrap_err();
        assert_eq!(err, nom::Err::Error(nom_error_expected));

        let result = parse_number_in_range::<u8>("21", 10, 20);
        let nom_error_expected = nom::error::Error::new("21", nom::error::ErrorKind::MapRes);
        let err = result.unwrap_err();
        assert_eq!(err, nom::Err::Error(nom_error_expected));
    }

    #[test]
    fn test_parse_number() {
        let result = parse_num::<u8>("12");
        assert!(result.is_ok());

        let result = parse_num::<u8>("12.5");
        let err_expected = "parse of number failed";
        assert_eq!(result, Err(err_expected));
    }

    #[test]
    fn test_parse_float_num() {
        let result = parse_float_num::<f32>("12.5");
        assert!(result.is_ok());
        let result = parse_float_num::<f32>("12");
        assert!(result.is_ok());

        let result = parse_float_num::<f32>("12.5.5");
        let err_expected = "parse of float number failed";
        assert_eq!(result, Err(err_expected));
    }

    #[test]
    fn test_parse_lat_lon() {
        let (_, lat_lon) = parse_lat_lon("4807.038,N,01131.324,E").unwrap();
        assert!(lat_lon.is_some());
        let (_, lat_lon) = parse_lat_lon(",,,,").unwrap();
        assert!(lat_lon.is_none());

        let lat_lon = parse_lat_lon("51.5074,0.1278");
        let err_expected = nom::error::Error::new("0.1278", nom::error::ErrorKind::OneOf);
        let err = lat_lon.unwrap_err();
        assert_eq!(err, nom::Err::Error(err_expected));

        let lat_lon = parse_lat_lon("1234.567,N,09876.543,W");
        assert!(lat_lon.is_ok());

        let lat_lon = parse_lat_lon("0000.000,S,00000.000,E");
        assert!(lat_lon.is_ok());

        let lat_lon = parse_lat_lon("1234.567,S,09876.543,E");
        assert!(lat_lon.is_ok());

        let lat_lon = parse_lat_lon("1234.567,S,09876.543,E");
        assert!(lat_lon.is_ok());

        let lat_lon = parse_lat_lon("40.7128,");
        assert!(lat_lon.is_err());

        let lat_lon = parse_lat_lon(", -74.0060");
        let err_expected = nom::error::Error::new(", -74.0060", nom::error::ErrorKind::MapRes);
        let err = lat_lon.unwrap_err();
        assert_eq!(err, nom::Err::Error(err_expected));

        let lat_lon = parse_lat_lon("abc,def");
        let err_expected = nom::error::Error::new("abc,def", nom::error::ErrorKind::MapRes);
        let err = lat_lon.unwrap_err();
        assert_eq!(err, nom::Err::Error(err_expected));
    }
}