-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoi.rs
More file actions
431 lines (391 loc) · 13.5 KB
/
Copy pathoi.rs
File metadata and controls
431 lines (391 loc) · 13.5 KB
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use quick_xml::Reader;
use quick_xml::escape::unescape;
use quick_xml::events::{BytesCData, BytesStart, BytesText, Event};
use time::OffsetDateTime;
use time::format_description::well_known::Rfc3339;
use crate::error::{ErrorKind, ParseError, Result};
use crate::wmo::WmoMessage;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NwwsOiMessage {
pub stanza_type: Option<String>,
pub from: Option<String>,
pub to: Option<String>,
pub summary: Option<String>,
pub xhtml_summary: Option<String>,
pub payload: Option<NwwsOiPayload>,
}
impl NwwsOiMessage {
pub fn parse(input: &str) -> Result<Self> {
parse_message(input)
}
pub fn validate(&self) -> Result<()> {
if let Some(payload) = &self.payload {
payload.validate()?;
}
Ok(())
}
/// Re-serialize this message into canonical NWWS-OI archive XML so live
/// captures round-trip through the same ingest path as archived stanzas.
///
/// Fails with `MissingField` when the message carries no `<x xmlns='nwws-oi'>`
/// payload (presence updates, plain chat).
pub fn to_archive_xml(&self) -> Result<String> {
let payload = self
.payload
.as_ref()
.ok_or_else(|| ParseError::new(ErrorKind::MissingField("nwws-oi payload")))?;
let issue = payload
.issue
.format(&Rfc3339)
.map_err(|_| ParseError::new(ErrorKind::InvalidField("nwws-oi issue time")))?;
let mut xml = String::new();
xml.push_str("<message");
push_xml_attr(
&mut xml,
"type",
self.stanza_type.as_deref().unwrap_or("groupchat"),
);
if let Some(from) = self.from.as_deref() {
push_xml_attr(&mut xml, "from", from);
}
if let Some(to) = self.to.as_deref() {
push_xml_attr(&mut xml, "to", to);
}
xml.push('>');
if let Some(summary) = self.summary.as_deref() {
xml.push_str("<body>");
xml.push_str(&escape_xml_text(summary));
xml.push_str("</body>");
}
if let Some(summary) = self.xhtml_summary.as_deref() {
xml.push_str("<html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://www.w3.org/1999/xhtml'>");
xml.push_str(&escape_xml_text(summary));
xml.push_str("</body></html>");
}
xml.push_str("<x xmlns='nwws-oi'");
push_xml_attr(&mut xml, "cccc", &payload.cccc);
push_xml_attr(&mut xml, "ttaaii", &payload.ttaaii);
push_xml_attr(&mut xml, "issue", &issue);
push_xml_attr(&mut xml, "awipsid", &payload.awips_id);
push_xml_attr(
&mut xml,
"id",
&format!("{}.{}", payload.id.process_id, payload.id.sequence),
);
xml.push('>');
xml.push_str(&escape_xml_text(&payload.raw_bulletin));
xml.push_str("</x></message>");
Ok(xml)
}
}
fn push_xml_attr(xml: &mut String, key: &str, value: &str) {
xml.push(' ');
xml.push_str(key);
xml.push_str("='");
xml.push_str(&escape_xml_attr(value));
xml.push('\'');
}
fn escape_xml_attr(value: &str) -> String {
let mut escaped = String::with_capacity(value.len());
for ch in value.chars() {
match ch {
'&' => escaped.push_str("&"),
'<' => escaped.push_str("<"),
'>' => escaped.push_str(">"),
'\'' => escaped.push_str("'"),
'"' => escaped.push_str("""),
_ => escaped.push(ch),
}
}
escaped
}
fn escape_xml_text(value: &str) -> String {
let mut escaped = String::with_capacity(value.len());
for ch in value.chars() {
match ch {
'&' => escaped.push_str("&"),
'<' => escaped.push_str("<"),
'>' => escaped.push_str(">"),
_ => escaped.push(ch),
}
}
escaped
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NwwsOiPayload {
pub cccc: String,
pub ttaaii: String,
pub issue: OffsetDateTime,
pub awips_id: String,
pub id: NwwsOiId,
pub raw_bulletin: String,
}
impl NwwsOiPayload {
pub fn parse_bulletin(&self) -> Result<WmoMessage<'_>> {
let bulletin = WmoMessage::parse_str(&self.raw_bulletin)?;
bulletin.verify_metadata(&self.ttaaii, &self.cccc, Some(&self.awips_id))?;
Ok(bulletin)
}
pub fn validate(&self) -> Result<()> {
let _ = self.parse_bulletin()?;
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NwwsOiId {
pub process_id: u32,
pub sequence: u64,
}
impl NwwsOiId {
pub fn parse(input: &str) -> Result<Self> {
let (process_id, sequence) = input
.split_once('.')
.ok_or_else(|| ParseError::new(ErrorKind::InvalidField("nwws id")))?;
let process_id = process_id
.parse()
.map_err(|_| ParseError::new(ErrorKind::InvalidField("nwws id")))?;
let sequence = sequence
.parse()
.map_err(|_| ParseError::new(ErrorKind::InvalidField("nwws id")))?;
Ok(Self {
process_id,
sequence,
})
}
}
fn parse_message(input: &str) -> Result<NwwsOiMessage> {
let mut reader = Reader::from_str(input);
reader.config_mut().trim_text(false);
let mut message = NwwsOiMessage {
stanza_type: None,
from: None,
to: None,
summary: None,
xhtml_summary: None,
payload: None,
};
let mut in_html = false;
let mut target = None;
let mut payload_builder: Option<PayloadBuilder> = None;
loop {
match reader.read_event() {
Ok(Event::Start(element)) => match element.name().as_ref() {
b"message" => {
message.stanza_type = find_attr(&element, b"type")?;
message.from = find_attr(&element, b"from")?;
message.to = find_attr(&element, b"to")?;
}
b"html" => in_html = true,
b"body" => {
target = Some(if in_html {
TextTarget::XhtmlSummary
} else {
TextTarget::Summary
});
}
b"x" if find_attr(&element, b"xmlns")?.as_deref() == Some("nwws-oi") => {
payload_builder = Some(PayloadBuilder::from_start(&element)?);
}
_ => {}
},
Ok(Event::Empty(element)) => {
if element.name().as_ref() == b"x"
&& find_attr(&element, b"xmlns")?.as_deref() == Some("nwws-oi")
{
message.payload = Some(PayloadBuilder::from_start(&element)?.finish());
}
}
Ok(Event::End(element)) => match element.name().as_ref() {
b"body" => target = None,
b"html" => in_html = false,
b"x" => {
if let Some(builder) = payload_builder.take() {
message.payload = Some(builder.finish());
}
}
_ => {}
},
Ok(Event::Text(text)) => {
let chunk = decode_text(&text)?;
if let Some(payload) = &mut payload_builder {
payload.raw_bulletin.push_str(&chunk);
} else {
append_text(
&mut message.summary,
&mut message.xhtml_summary,
target,
&chunk,
);
}
}
Ok(Event::CData(text)) => {
let chunk = decode_cdata(&text)?;
if let Some(payload) = &mut payload_builder {
payload.raw_bulletin.push_str(&chunk);
} else {
append_text(
&mut message.summary,
&mut message.xhtml_summary,
target,
&chunk,
);
}
}
Ok(Event::Eof) => break,
Ok(_) => {}
Err(_) => return Err(ParseError::new(ErrorKind::InvalidXml("reader error"))),
}
}
Ok(message)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TextTarget {
Summary,
XhtmlSummary,
}
#[derive(Debug, Clone)]
struct PayloadBuilder {
cccc: String,
ttaaii: String,
issue: OffsetDateTime,
awips_id: String,
id: NwwsOiId,
raw_bulletin: String,
}
impl PayloadBuilder {
fn from_start(element: &BytesStart<'_>) -> Result<Self> {
let cccc = required_attr(element, b"cccc")?;
let ttaaii = required_attr(element, b"ttaaii")?;
let issue_raw = required_attr(element, b"issue")?;
let issue = OffsetDateTime::parse(&issue_raw, &Rfc3339)
.map_err(|_| ParseError::new(ErrorKind::InvalidField("issue")))?;
let awips_id = required_attr(element, b"awipsid")?;
let id = NwwsOiId::parse(&required_attr(element, b"id")?)?;
Ok(Self {
cccc,
ttaaii,
issue,
awips_id,
id,
raw_bulletin: String::new(),
})
}
fn finish(self) -> NwwsOiPayload {
NwwsOiPayload {
cccc: self.cccc,
ttaaii: self.ttaaii,
issue: self.issue,
awips_id: self.awips_id,
id: self.id,
raw_bulletin: trim_bulletin_edges(&self.raw_bulletin),
}
}
}
fn append_text(
summary: &mut Option<String>,
xhtml_summary: &mut Option<String>,
target: Option<TextTarget>,
chunk: &str,
) {
match target {
Some(TextTarget::Summary) => push_chunk(summary, chunk),
Some(TextTarget::XhtmlSummary) => push_chunk(xhtml_summary, chunk),
None => {}
}
}
fn push_chunk(slot: &mut Option<String>, chunk: &str) {
if chunk.is_empty() {
return;
}
slot.get_or_insert_with(String::new).push_str(chunk);
}
fn find_attr(element: &BytesStart<'_>, key: &[u8]) -> Result<Option<String>> {
for attr in element.attributes().with_checks(false) {
let attr = attr.map_err(|_| ParseError::new(ErrorKind::InvalidXml("invalid attribute")))?;
if attr.key.as_ref() == key {
let raw = std::str::from_utf8(attr.value.as_ref())
.map_err(|_| ParseError::new(ErrorKind::InvalidUtf8))?;
let value = unescape(raw)
.map_err(|_| ParseError::new(ErrorKind::InvalidXml("invalid escape sequence")))?;
return Ok(Some(value.into_owned()));
}
}
Ok(None)
}
fn required_attr(element: &BytesStart<'_>, key: &[u8]) -> Result<String> {
find_attr(element, key)?.ok_or_else(|| {
ParseError::new(ErrorKind::MissingField(match key {
b"cccc" => "cccc",
b"ttaaii" => "ttaaii",
b"issue" => "issue",
b"awipsid" => "awipsid",
b"id" => "id",
_ => "attribute",
}))
})
}
fn decode_text(text: &BytesText<'_>) -> Result<String> {
let raw =
std::str::from_utf8(text.as_ref()).map_err(|_| ParseError::new(ErrorKind::InvalidUtf8))?;
let decoded = unescape(raw)
.map_err(|_| ParseError::new(ErrorKind::InvalidXml("invalid escape sequence")))?;
Ok(decoded.into_owned())
}
fn decode_cdata(text: &BytesCData<'_>) -> Result<String> {
let raw =
std::str::from_utf8(text.as_ref()).map_err(|_| ParseError::new(ErrorKind::InvalidUtf8))?;
Ok(raw.to_owned())
}
fn trim_bulletin_edges(input: &str) -> String {
input.trim_matches(|ch| ch == '\r' || ch == '\n').to_owned()
}
#[cfg(test)]
mod tests {
use super::{NwwsOiId, NwwsOiMessage};
const XML: &str = r#"<message to='enduser@server/laptop' type='groupchat' from='nwws@nwws-oi.weather.gov/nwws-oi'>
<body>KARX issues RR8 valid 2013-05-25T02:20:34Z</body>
<html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://www.w3.org/1999/xhtml'>KARX issues RR8 valid 2013-05-25T02:20:34Z</body></html>
<x xmlns='nwws-oi' cccc='KARX' ttaaii='SRUS83' issue='2013-05-25T02:20:34Z' awipsid='RR8ARX' id='10313.6'>111
SRUS83 KARX 250220
RR8ARX
:
: AUTOMATED GAUGE DATA COLLECTED FROM IOWA FLOOD CENTER
:
.A CDGI4 20130524 C DH2100/HGIRP 2.63 : MORGAN CREEK NEAR CEDAR RAPIDS</x>
</message>"#;
#[test]
fn parses_nwws_oi_message() {
let message = NwwsOiMessage::parse(XML).unwrap();
assert_eq!(
message.summary.as_deref(),
Some("KARX issues RR8 valid 2013-05-25T02:20:34Z")
);
assert_eq!(
message.xhtml_summary.as_deref(),
Some("KARX issues RR8 valid 2013-05-25T02:20:34Z")
);
let payload = message.payload.unwrap();
assert_eq!(payload.cccc, "KARX");
assert_eq!(payload.ttaaii, "SRUS83");
assert_eq!(payload.awips_id, "RR8ARX");
assert_eq!(
payload.id,
NwwsOiId {
process_id: 10313,
sequence: 6
}
);
payload.validate().unwrap();
}
#[test]
fn supports_history_message_without_payload() {
let xml = r#"<message type='groupchat'><body>KARX issues RR8 valid 2013-05-25T02:20:34Z</body></message>"#;
let message = NwwsOiMessage::parse(xml).unwrap();
assert!(message.payload.is_none());
message.validate().unwrap();
}
#[test]
fn rejects_bad_id() {
assert!(NwwsOiId::parse("10313").is_err());
}
}