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
123use crate::sanitize::has_data;
use flarmnet::Record;
use serde::Serialize;
fn is_empty_or_unknown(s: &str) -> bool {
s.is_empty() || s == "Unknown"
}
#[derive(Serialize)]
pub struct SerializableRecord<'a> {
flarm_id: &'a str,
#[serde(skip_serializing_if = "str::is_empty")]
pilot_name: &'a str,
#[serde(skip_serializing_if = "str::is_empty")]
airfield: &'a str,
#[serde(skip_serializing_if = "is_empty_or_unknown")]
plane_type: &'a str,
#[serde(skip_serializing_if = "str::is_empty")]
registration: &'a str,
#[serde(skip_serializing_if = "str::is_empty")]
call_sign: &'a str,
#[serde(skip_serializing_if = "str::is_empty")]
frequency: &'a str,
}
impl<'a> SerializableRecord<'a> {
pub fn from_record(record: &'a Record) -> Option<Self> {
if record.flarm_id.is_empty() || !has_data(record) {
return None;
}
Some(Self {
flarm_id: &record.flarm_id,
pilot_name: &record.pilot_name,
airfield: &record.airfield,
plane_type: &record.plane_type,
registration: &record.registration,
call_sign: &record.call_sign,
frequency: &record.frequency,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serialize_record() {
let record = Record {
flarm_id: "ABCDEF".to_string(),
pilot_name: "John Dö".to_string(),
airfield: "EDKA".to_string(),
plane_type: "ASW 28".to_string(),
registration: "D-1234".to_string(),
call_sign: "XY".to_string(),
frequency: "123.456".to_string(),
};
insta::assert_json_snapshot!(SerializableRecord::from_record(&record), @r#"
{
"flarm_id": "ABCDEF",
"pilot_name": "John Dö",
"airfield": "EDKA",
"plane_type": "ASW 28",
"registration": "D-1234",
"call_sign": "XY",
"frequency": "123.456"
}
"#);
}
#[test]
fn test_empty_strings_are_skipped() {
let record = Record {
flarm_id: "ABCDEF".to_string(),
pilot_name: "".to_string(),
airfield: "".to_string(),
plane_type: "Unknown".to_string(),
registration: "D-1234".to_string(),
call_sign: "".to_string(),
frequency: "".to_string(),
};
insta::assert_json_snapshot!(SerializableRecord::from_record(&record), @r#"
{
"flarm_id": "ABCDEF",
"registration": "D-1234"
}
"#);
}
#[test]
fn test_empty_flarm_id_returns_none() {
let record = Record {
flarm_id: "".to_string(),
pilot_name: "John Dö".to_string(),
airfield: "EDKA".to_string(),
plane_type: "ASW 28".to_string(),
registration: "D-1234".to_string(),
call_sign: "XY".to_string(),
frequency: "123.456".to_string(),
};
assert!(SerializableRecord::from_record(&record).is_none());
}
#[test]
fn test_all_other_fields_empty_returns_none() {
let record = Record {
flarm_id: "ABCDEF".to_string(),
pilot_name: "".to_string(),
airfield: "".to_string(),
plane_type: "".to_string(),
registration: "".to_string(),
call_sign: "".to_string(),
frequency: "".to_string(),
};
assert!(SerializableRecord::from_record(&record).is_none());
}
}