forked from AeroRust/nmea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.rs
251 lines (222 loc) · 7.75 KB
/
utils.rs
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
use core::str;
use arrayvec::ArrayString;
use chrono::{NaiveDate, NaiveTime};
use nom::{
branch::alt,
bytes::complete::{tag, take, take_until},
character::complete::{char, digit1, one_of},
combinator::{map, map_parser, map_res},
number::complete::{double, float},
sequence::tuple,
IResult,
};
#[cfg(not(feature = "std"))]
#[allow(unused_imports)]
use num_traits::float::FloatCore;
use crate::Error;
pub(crate) 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)
}
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::SentenceLength`] error.
pub(crate) fn array_string<const MAX_LEN: usize>(
string: &str,
) -> Result<ArrayString<MAX_LEN>, Error> {
ArrayString::from(string).map_err(|_| Error::SentenceLength(string.len()))
}
#[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_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());
}
}