use std::hash::{Hash, Hasher};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, NoneAsEmptyString};
use time::{format_description::parse, OffsetDateTime};
use crate::{callsign::Callsign, HasSummit};
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 Notice for Alert {
fn callsign(&self) -> &Callsign {
&self.activating_callsign
}
fn epoch(&self) -> &str {
&self.epoch
}
fn timestamp(&self) -> String {
let time = self
.date_activated
.format(&parse("[year]-[month]-[day] [hour]:[minute]").unwrap())
.unwrap();
format!("{}Z", time)
}
}
impl HasSummit for Alert {
fn summit_code(&self) -> String {
format!("{}/{}", self.association_code, self.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
}
}