1use super::*;
18
19#[derive(Clone, Debug, PartialEq, Serialize)]
21pub struct MwvData {
22 pub wind_angle: Option<f64>,
24
25 pub relative: Option<bool>,
27
28 pub wind_speed_knots: Option<f64>,
30
31 pub wind_speed_kmh: Option<f64>,
33}
34
35pub(crate) fn handle(sentence: &str) -> Result<ParsedMessage, ParseError> {
40 let split: Vec<&str> = sentence.split(',').collect();
41
42 Ok(ParsedMessage::Mwv(MwvData {
43 wind_angle: pick_number_field(&split, 1)?,
44 relative: match pick_string_field(&split, 2)
45 .ok_or(ParseError::CorruptedSentence(
46 "pick string for \"relative\" was None".to_string(),
47 ))?
48 .as_str()
49 {
50 "R" => Some(true),
51 "T" => Some(false),
52 _ => None,
53 },
54 wind_speed_knots: match pick_string_field(&split, 4)
55 .ok_or(ParseError::CorruptedSentence(
56 "pick string for \"wind_speed_knots\" was None".to_string(),
57 ))?
58 .as_str()
59 {
60 "N" => pick_number_field(&split, 3)?,
61 "M" => Some(
62 pick_number_field::<f64>(&split, 3)?.ok_or(ParseError::CorruptedSentence(
63 "pick string for \"wind_speed_knots M\" was None".to_string(),
64 ))? * 1.943844,
65 ),
66 "K" => Some(
67 pick_number_field::<f64>(&split, 3)?.ok_or(ParseError::CorruptedSentence(
68 "pick string for \"wind_speed_knots K\" was None".to_string(),
69 ))? * 0.539957,
70 ),
71 _ => None,
72 },
73 wind_speed_kmh: match pick_string_field(&split, 4)
74 .ok_or(ParseError::CorruptedSentence(
75 "pick string for \"wind_speed_kmh\" was None".to_string(),
76 ))?
77 .as_str()
78 {
79 "N" => Some(
80 pick_number_field::<f64>(&split, 3)?.ok_or(ParseError::CorruptedSentence(
81 "pick string for \"wind_speed_kmh N\" was None".to_string(),
82 ))? * 1.852,
83 ),
84 "M" => Some(
85 pick_number_field::<f64>(&split, 3)?.ok_or(ParseError::CorruptedSentence(
86 "pick string for \"wind_speed_kmh M\" was None".to_string(),
87 ))? * 3.6,
88 ),
89 "K" => pick_number_field(&split, 3)?,
90 _ => None,
91 },
92 }))
93}
94
95#[cfg(test)]
98mod test {
99 use super::*;
100
101 #[test]
102 fn test_parse_mwv() {
103 match NmeaParser::new().parse_sentence("$WIMWV,295.4,T,33.3,N,A*1C") {
104 Ok(ps) => match ps {
105 ParsedMessage::Mwv(mwv) => {
106 assert_eq!(mwv.wind_angle, Some(295.4));
107 assert_eq!(mwv.relative, Some(false));
108 assert_eq!(mwv.wind_speed_knots, Some(33.3));
109 assert_eq!(mwv.wind_speed_kmh, Some(33.3 * 1.852));
110 }
111 _ => {
112 assert!(false);
113 }
114 },
115 Err(e) => {
116 assert_eq!(e.to_string(), "OK");
117 }
118 }
119 }
120}