use std::{
hash::{Hash, Hasher},
str::FromStr,
};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, NoneAsEmptyString};
use time::OffsetDateTime;
use crate::{
callsign::Callsign,
summit::{HasSummit, SummitCode},
ParseError,
};
use super::Notice;
#[serde_as]
#[derive(Debug, Clone, Deserialize, Serialize, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Alert {
pub activating_callsign: Callsign,
pub activator_name: String,
pub association_code: String,
pub summit_code: String,
pub summit_details: String,
pub frequency: String,
#[serde(with = "time::serde::rfc3339")]
pub time_stamp: OffsetDateTime,
#[serde(with = "time::serde::rfc3339")]
pub date_activated: OffsetDateTime,
pub poster_callsign: Callsign,
#[serde_as(as = "NoneAsEmptyString")]
pub comments: Option<String>,
pub epoch: String,
}
impl Alert {
pub fn full_summit_code(&self) -> String {
format!("{}/{}", self.association_code, self.summit_code)
}
}
impl Notice for Alert {
fn callsign(&self) -> &Callsign {
&self.activating_callsign
}
fn epoch(&self) -> &str {
&self.epoch
}
}
impl HasSummit for Alert {
fn summit_code(&self) -> Result<SummitCode, ParseError> {
SummitCode::from_str(&self.full_summit_code())
}
}
impl Hash for Alert {
fn hash<H: Hasher>(&self, state: &mut H) {
self.activating_callsign.hash(state);
self.association_code.hash(state);
self.summit_code.hash(state);
self.frequency.hash(state);
self.date_activated.hash(state);
}
}
impl PartialEq for Alert {
fn eq(&self, other: &Self) -> bool {
self.activating_callsign == other.activating_callsign
&& self.association_code == other.association_code
&& self.summit_code == other.summit_code
&& self.frequency == other.frequency
&& self.time_stamp == other.time_stamp
}
}