Skip to content

Commit ec9f691

Browse files
committed
samedec: update for breaking EventCode API
The new API is simpler and doesn't require matches for errors. The integration tests are also updated.
1 parent 03ff745 commit ec9f691

File tree

4 files changed

+14
-35
lines changed

4 files changed

+14
-35
lines changed

crates/samedec/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,15 @@ The child process receives the following additional environment variables:
273273
274274
* `SAMEDEC_EVT`: the three-character SAME event code, like "`RWT`"
275275
276-
* `SAMEDEC_EVENT`: human-readable event name: "`Required Weekly Test`." If the
277-
event code is not known, and it its significance level is also unknown, then
278-
this string will be "`Unrecognized`."
276+
* `SAMEDEC_EVENT`: human-readable event description, including its significance
277+
level: "`Required Weekly Test`." If the event code is not known, and it its
278+
significance level is also unknown, then this string will be
279+
"`Unrecognized Warning`."
279280
280281
* `SAMEDEC_SIGNIFICANCE`: one-character significance level. This variable will
281282
be empty if the significance level could not be determined (i.e., because
282283
the event code is unknown).
284+
283285
* `T`: Test
284286
* `M`: Message
285287
* `S`: Statement

crates/samedec/src/app.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ mod tests {
260260
use super::*;
261261

262262
use chrono::{Duration, TimeZone, Utc};
263-
use sameold::EventCode;
263+
use sameold::Phenomenon;
264264

265265
#[test]
266266
fn test_make_demo_message() {
@@ -270,7 +270,7 @@ mod tests {
270270
Message::StartOfMessage(hdr) => hdr,
271271
_ => unreachable!(),
272272
};
273-
assert_eq!(msg.event().unwrap(), EventCode::PracticeDemoWarning);
273+
assert_eq!(msg.event().phenomenon(), Phenomenon::PracticeDemoWarning);
274274
assert_eq!(msg.issue_datetime(&tm).unwrap(), tm);
275275
assert_eq!(msg.valid_duration(), Duration::minutes(15));
276276
}

crates/samedec/src/spawner.rs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io;
77
use std::process::{Child, Command, Stdio};
88

99
use chrono::{DateTime, Utc};
10-
use sameold::{MessageHeader, UnrecognizedEventCode};
10+
use sameold::MessageHeader;
1111

1212
/// Spawn a child process to handle the given message
1313
///
@@ -54,8 +54,11 @@ where
5454
header.originator().as_display_str(),
5555
)
5656
.env(childenv::SAMEDEC_EVT, header.event_str())
57-
.env(childenv::SAMEDEC_EVENT, msg_to_event(&header))
58-
.env(childenv::SAMEDEC_SIGNIFICANCE, msg_to_significance(header))
57+
.env(childenv::SAMEDEC_EVENT, header.event().to_string())
58+
.env(
59+
childenv::SAMEDEC_SIGNIFICANCE,
60+
header.event().significance().as_code_str(),
61+
)
5962
.env(childenv::SAMEDEC_LOCATIONS, locations.join(" "))
6063
.env(childenv::SAMEDEC_ISSUETIME, issue_ts)
6164
.env(childenv::SAMEDEC_PURGETIME, purge_ts)
@@ -143,23 +146,6 @@ mod childenv {
143146
pub const SAMEDEC_PURGETIME: &str = "SAMEDEC_PURGETIME";
144147
}
145148

146-
// convert message event code to string
147-
fn msg_to_event(msg: &MessageHeader) -> String {
148-
match msg.event() {
149-
Ok(evt) => evt.as_display_str().to_owned(),
150-
Err(err) => format!("{}", err),
151-
}
152-
}
153-
154-
// convert message event code to significance
155-
fn msg_to_significance(msg: &MessageHeader) -> &'static str {
156-
match msg.event() {
157-
Ok(evt) => evt.to_significance_level().as_str(),
158-
Err(UnrecognizedEventCode::WithSignificance(sl)) => sl.as_str(),
159-
Err(UnrecognizedEventCode::Unrecognized) => "",
160-
}
161-
}
162-
163149
// convert DateTime to UTC unix timestamp in seconds, as string
164150
fn time_to_unix_str(tm: DateTime<Utc>) -> String {
165151
format!("{}", tm.format("%s"))
@@ -169,15 +155,6 @@ fn time_to_unix_str(tm: DateTime<Utc>) -> String {
169155
mod tests {
170156
use super::*;
171157

172-
use sameold::MessageHeader;
173-
174-
#[test]
175-
fn test_msg_to_significance() {
176-
const MSG: &str = "ZCZC-WXR-RWT-012345-567890-888990+0351-3662322-NOCALL-";
177-
let msg = MessageHeader::new(MSG).unwrap();
178-
assert_eq!("T", msg_to_significance(&msg));
179-
}
180-
181158
#[test]
182159
fn test_time_to_unix_str() {
183160
let dt: DateTime<Utc> = DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 GMT")

sample/two_and_two.22050.s16le.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set -e
66
exec 0>/dev/null
77

88
[ "$SAMEDEC_EVENT" = "Severe Thunderstorm Warning" ]
9-
[ "$SAMEDEC_ORIGINATOR" = "Weather Service" ]
9+
[ "$SAMEDEC_ORIGINATOR" = "National Weather Service" ]
1010
[ "$SAMEDEC_SIGNIFICANCE" = "W" ]
1111

1212
lifetime=$(( SAMEDEC_PURGETIME - SAMEDEC_ISSUETIME))

0 commit comments

Comments
 (0)