sameplace 0.2.0

A SAME/EAS Message Parser
Documentation
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! SAME/EAS Event Codes

use std::fmt;

use strum::{EnumMessage, EnumProperty};

/// SAME message phenomenon
///
/// A Phenomenon code indicates what prompted the message. These include
/// tests, such as the
/// [required weekly test](Phenomenon::RequiredWeeklyTest),
/// and live messages like [floods](Phenomenon::Flood). Some events
/// have multiple significance levels: floods can be reported as both
/// a "Flood Watch" and a "Flood Warning." The `Phenomenon` only encodes
/// `Phenomenon::Flood`—the [significance](crate::SignificanceLevel)
/// is left to other types.
///
/// Phenomenon may be matched individually if the user wishes to take
/// special action…
///
/// ```
/// # use sameplace::Phenomenon;
/// # let phenomenon = Phenomenon::Flood;
/// match phenomenon {
///     Phenomenon::Flood => println!("this message describes a flood"),
///     _ => { /* pass */ }
/// }
/// ```
///
/// … but the programmer must **exercise caution** here. Flooding may also
///  result from a [`Phenomenon::FlashFlood`] or a larger event like a
/// [`Phenomenon::Hurricane`]. An evacuation might be declared with
/// [`Phenomenon::Evacuation`], but many other messages might prompt an
/// evacuation as part of the response. So:
///
/// **⚠️ When in doubt, play the message and let the user decide! ⚠️**
///
/// sameplace *does* separate Phenomenon into broad categories. These include:
///
/// ```
/// # use sameplace::Phenomenon;
/// assert!(Phenomenon::NationalPeriodicTest.is_national());
/// assert!(Phenomenon::NationalPeriodicTest.is_test());
/// assert!(Phenomenon::SevereThunderstorm.is_weather());
/// assert!(Phenomenon::Fire.is_non_weather());
/// ```
///
/// All Phenomenon `Display` a human-readable description of the event,
/// without its significance level.
///
/// ```
/// # use sameplace::Phenomenon;
/// use std::fmt;
///
/// assert_eq!(format!("{}", Phenomenon::HazardousMaterials), "Hazardous Materials");
/// assert_eq!(Phenomenon::HazardousMaterials.as_brief_str(), "Hazardous Materials");
/// ```
///
/// but you probably want to display the full
/// [`EventCode`](crate::EventCode) instead.
///
/// NOTE: the strum traits on this type are **not** considered API.
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    Hash,
    strum_macros::EnumMessage,
    strum_macros::EnumProperty,
    strum_macros::EnumIter,
)]
#[non_exhaustive]
pub enum Phenomenon {
    /// National Emergency Message
    ///
    /// This was previously known as Emergency Action Notification
    #[strum(
        message = "National Emergency",
        detailed_message = "National Emergency Message",
        props(national = "")
    )]
    NationalEmergency,

    /// National Information Center (United States, part of national activation)
    #[strum(message = "National Information Center", props(national = ""))]
    NationalInformationCenter,

    /// National Audible Test (Canada)
    #[strum(message = "National Audible Test", props(national = "", test = ""))]
    NationalAudibleTest,

    /// National Periodic Test (United States)
    #[strum(message = "National Periodic Test", props(national = "", test = ""))]
    NationalPeriodicTest,

    /// National Silent Test (Canada)
    #[strum(message = "National Silent Test", props(national = "", test = ""))]
    NationalSilentTest,

    /// Required Monthly Test
    #[strum(message = "Required Monthly Test", props(test = ""))]
    RequiredMonthlyTest,

    /// Required Weekly Test
    #[strum(message = "Required Weekly Test", props(test = ""))]
    RequiredWeeklyTest,

    /// Administrative Message
    ///
    /// Used as follow-up for non-weather messages, including potentially
    /// to issue an all-clear.
    #[strum(message = "Administrative Message")]
    AdministrativeMessage,

    /// Avalanche
    #[strum(message = "Avalanche", detailed_message = "Avalanche %")]
    Avalanche,

    /// Blizzard
    #[strum(
        message = "Blizzard",
        detailed_message = "Blizzard %",
        props(weather = "")
    )]
    Blizzard,

    /// Blue Alert (state/local)
    #[strum(message = "Blue Alert")]
    BlueAlert,

    /// Child Abduction Emergency (state/local)
    #[strum(
        message = "Child Abduction",
        detailed_message = "Child Abduction Emergency"
    )]
    ChildAbduction,

    /// Civil Danger Warning (state/local)
    #[strum(message = "Civil Danger", detailed_message = "Civil Danger Warning")]
    CivilDanger,

    /// Civil Emergency Message (state/local)
    #[strum(
        message = "Civil Emergency",
        detailed_message = "Civil Emergency Message"
    )]
    CivilEmergency,

    /// Coastal Flood
    #[strum(
        message = "Coastal Flood",
        detailed_message = "Coastal Flood %",
        props(weather = "")
    )]
    CoastalFlood,

    /// Dust Storm
    #[strum(
        message = "Dust Storm",
        detailed_message = "Dust Storm %",
        props(weather = "")
    )]
    DustStorm,

    /// Earthquake Warning
    ///
    /// **NOTE:** It is unclear if SAME is fast enough to provide timely
    /// notifications of earthquakes.
    #[strum(message = "Earthquake", detailed_message = "Earthquake Warning")]
    Earthquake,

    /// Evacuation Immediate
    #[strum(message = "Evacuation", detailed_message = "Evacuation Immediate")]
    Evacuation,

    /// Extreme Wind
    #[strum(
        message = "Extreme Wind",
        detailed_message = "Extreme Wind %",
        props(weather = "")
    )]
    ExtremeWind,

    /// Fire Warning
    #[strum(message = "Fire", detailed_message = "Fire %")]
    Fire,

    /// Flash Flood
    #[strum(
        message = "Flash Flood",
        detailed_message = "Flash Flood %",
        props(weather = "")
    )]
    FlashFlood,

    /// Flash Freeze (Canada)
    #[strum(
        message = "Flash Freeze",
        detailed_message = "Flash Freeze %",
        props(weather = "")
    )]
    FlashFreeze,

    /// Flood
    #[strum(message = "Flood", detailed_message = "Flood %", props(weather = ""))]
    Flood,

    /// Freeze (Canada)
    #[strum(message = "Freeze", detailed_message = "Freeze %", props(weather = ""))]
    Freeze,

    /// Hazardous Materials (Warning)
    #[strum(
        message = "Hazardous Materials",
        detailed_message = "Hazardous Materials Warning"
    )]
    HazardousMaterials,

    /// High Wind
    #[strum(
        message = "High Wind",
        detailed_message = "High Wind %",
        props(weather = "")
    )]
    HighWind,

    /// Hurricane
    #[strum(
        message = "Hurricane",
        detailed_message = "Hurricane %",
        props(weather = "")
    )]
    Hurricane,

    /// Hurricane Local Statement
    #[strum(message = "Hurricane Local Statement", props(weather = ""))]
    HurricaneLocalStatement,

    /// Law Enforcement Warning
    #[strum(message = "Law Enforcement Warning")]
    LawEnforcementWarning,

    /// Local Area Emergency
    #[strum(message = "Local Area Emergency")]
    LocalAreaEmergency,

    /// Network Message Notification
    #[strum(message = "Network Message Notification")]
    NetworkMessageNotification,

    /// 911 Telephone Outage Emergency
    #[strum(
        message = "911 Telephone Outage",
        detailed_message = "911 Telephone Outage Emergency"
    )]
    TelephoneOutage,

    /// Nuclear Power Plant (Warning)
    #[strum(
        message = "Nuclear Power Plant",
        detailed_message = "Nuclear Power Plant Warning"
    )]
    NuclearPowerPlant,

    /// Practice/Demo Warning
    #[strum(message = "Practice/Demo Warning")]
    PracticeDemoWarning,

    /// Radiological Hazard
    #[strum(
        message = "Radiological Hazard",
        detailed_message = "Radiological Hazard Warning"
    )]
    RadiologicalHazard,

    /// Severe Thunderstorm
    #[strum(
        message = "Severe Thunderstorm",
        detailed_message = "Severe Thunderstorm %",
        props(weather = "")
    )]
    SevereThunderstorm,

    /// Severe Weather Statement
    #[strum(
        message = "Severe Weather",
        detailed_message = "Severe Weather %",
        props(weather = "")
    )]
    SevereWeather,

    /// Shelter In Place
    #[strum(
        message = "Shelter In Place",
        detailed_message = "Shelter In Place Warning"
    )]
    ShelterInPlace,

    /// Snow Squall
    #[strum(
        message = "Snow Squall",
        detailed_message = "Snow Squall %",
        props(weather = "")
    )]
    SnowSquall,

    /// Special Marine
    #[strum(
        message = "Special Marine",
        detailed_message = "Special Marine %",
        props(weather = "")
    )]
    SpecialMarine,

    /// Special Weather Statement
    #[strum(message = "Special Weather Statement", props(weather = ""))]
    SpecialWeatherStatement,

    /// Storm Surge
    #[strum(
        message = "Storm Surge",
        detailed_message = "Storm Surge %",
        props(weather = "")
    )]
    StormSurge,

    /// Tornado Warning
    #[strum(
        message = "Tornado",
        detailed_message = "Tornado %",
        props(weather = "")
    )]
    Tornado,

    /// Tropical Storm
    #[strum(
        message = "Tropical Storm",
        detailed_message = "Tropical Storm %",
        props(weather = "")
    )]
    TropicalStorm,

    /// Tsunami
    #[strum(
        message = "Tsunami",
        detailed_message = "Tsunami %",
        props(weather = "")
    )]
    Tsunami,

    /// Volcano
    #[strum(message = "Volcano", detailed_message = "Volcano Warning")]
    Volcano,

    /// Winter Storm
    #[strum(
        message = "Winter Storm",
        detailed_message = "Winter Storm %",
        props(weather = "")
    )]
    WinterStorm,

    /// Unrecognized phenomenon
    ///
    /// A catch-all for unrecognized event codes which either did not
    /// decode properly or are not known to sameold. If you encounter
    /// an Unrecognized event code in a production message, please
    /// [report it as a bug](https://github.com/cbs228/sameold/issues)
    /// right away.
    #[strum(message = "Unrecognized", detailed_message = "Unrecognized %")]
    Unrecognized,
}

impl Phenomenon {
    /// Describes the event without its accompanying severity information.
    /// For example,
    ///
    /// ```
    /// # use sameplace::Phenomenon;
    /// assert_eq!(Phenomenon::RadiologicalHazard.as_brief_str(), "Radiological Hazard");
    /// ```
    ///
    /// as opposed to the full human-readable description of the event code,
    /// "Radiological Hazard *Warning*." If you want the full description,
    /// use [`EventCode`](crate::EventCode) instead.
    pub fn as_brief_str(&self) -> &'static str {
        self.get_message().expect("missing phenomenon message")
    }

    /// True if the phenomenon is associated with a national activation
    ///
    /// Returns true if the underlying event code is *typically* used
    /// for national activations. This includes both live
    /// National Emergency Messages and the National Periodic Test.
    ///
    /// Clients should consult the message's location codes to
    /// determine if the message actually has national scope.
    pub fn is_national(&self) -> bool {
        self.get_str("national").is_some()
    }

    /// True if the phenomenon is associated with tests
    ///
    /// Returns true if the underlying event code is used only for
    /// tests. Test messages do not represent actual, real-world conditions.
    /// Test messages should also have a
    /// [`SignificanceLevel::Test`](crate::SignificanceLevel::Test).
    pub fn is_test(&self) -> bool {
        self.get_str("test").is_some()
    }

    /// True if the represented phenomenon is weather
    ///
    /// In the United States, weather phenomenon codes like
    /// "Severe Thunderstorm Warning" (`SVR`) are typically
    /// only issued by the National Weather Service. The list of
    /// weather event codes is taken from:
    ///
    /// * "National Weather Service Instruction 10-1708," 11 Dec 2017,
    /// <https://www.nws.noaa.gov/directives/sym/pd01017008curr.pdf>
    ///
    /// Not all **natural phenomenon** are considered **weather.**
    /// Volcanoes, avalanches, and wildfires are examples of non-weather
    /// phenomenon that are naturally occurring. The National Weather
    /// Service does not itself issue these types of alerts; they are
    /// generally left to state and local authorities.
    pub fn is_weather(&self) -> bool {
        self.get_str("weather").is_some()
    }

    /// True if the represented phenomenon is not weather
    ///
    /// The opposite of [`Phenomenon::is_weather()`]. The list of
    /// non-weather event codes available for national, state, and/or
    /// local use is taken from:
    ///
    /// * "National Weather Service Instruction 10-1708," 11 Dec 2017,
    /// <https://www.nws.noaa.gov/directives/sym/pd01017008curr.pdf>
    pub fn is_non_weather(&self) -> bool {
        !self.is_weather()
    }

    /// True if the phenomenon is not recognized
    ///
    /// ```
    /// # use sameplace::Phenomenon;
    /// assert!(Phenomenon::Unrecognized.is_unrecognized());
    /// ```
    pub fn is_unrecognized(&self) -> bool {
        self == &Self::Unrecognized
    }

    /// True if the phenomenon is recognized
    ///
    /// ```
    /// # use sameplace::Phenomenon;
    /// assert!(Phenomenon::TropicalStorm.is_recognized());
    /// ```
    ///
    /// The opposite of [`is_unrecognized()`](Phenomenon::is_unrecognized).
    pub fn is_recognized(&self) -> bool {
        !self.is_unrecognized()
    }

    /// Pattern string for full representation
    ///
    /// Returns a string like "`Tornado %`" that is the full string
    /// representation of a SAME event code, with significance information.
    /// `%` signs should be replaced with a textual representation of
    /// the event code's significance level.
    pub(crate) fn as_full_pattern_str(&self) -> &'static str {
        self.get_detailed_message()
            .unwrap_or_else(|| self.get_message().expect("missing phenomenon message"))
    }
}

impl Default for Phenomenon {
    fn default() -> Self {
        Self::Unrecognized
    }
}

impl std::fmt::Display for Phenomenon {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_brief_str().fmt(f)
    }
}

impl AsRef<str> for Phenomenon {
    fn as_ref(&self) -> &str {
        self.as_brief_str()
    }
}

#[cfg(test)]
mod tests {
    use strum::IntoEnumIterator;

    use super::*;

    #[test]
    fn test_national() {
        assert!(Phenomenon::NationalEmergency.is_national());
        assert!(Phenomenon::NationalEmergency.is_non_weather());
        assert!(Phenomenon::NationalPeriodicTest.is_national());
        assert!(Phenomenon::NationalPeriodicTest.is_non_weather());
        assert!(!Phenomenon::Hurricane.is_national());
        assert!(Phenomenon::Hurricane.is_weather());
    }

    // all phenomenon have messages and are either tests,
    // weather, or non-weather
    #[test]
    fn test_property_completeness() {
        for phenom in Phenomenon::iter() {
            // these must not panic
            phenom.as_brief_str();
            phenom.as_full_pattern_str();

            if phenom.is_test() || phenom.is_national() {
                assert!(phenom.is_non_weather());
            }
            if phenom.is_weather() {
                assert!(!phenom.is_test());
            }
        }
    }
}