correctly handle updated alerts

This commit is contained in:
Sebastian Hugentobler 2024-05-11 18:37:41 +02:00
parent a8c0be2c05
commit ac0ac8a10d
Signed by: shu
GPG key ID: BB32CF3CA052C2F0
34 changed files with 775 additions and 114 deletions

40
app/src/json/alert.rs Normal file
View file

@ -0,0 +1,40 @@
use serde::{Deserialize, Serialize};
use time::PrimitiveDateTime;
use super::{
area::Area, contact::Contact, description::Description, image::Image,
image_description::ImageDescription, image_title::ImageTitle, link::Link, text::Text,
title::Title,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Alert {
pub identifier: String,
pub gtrans_origin: Option<String>,
#[serde(deserialize_with = "super::datetime::deserialize_alert")]
pub sent: PrimitiveDateTime,
pub title: Title,
pub description: Description,
pub nation_wide: bool,
pub banner: bool,
pub instructions: Vec<Text>,
pub sender: String,
pub publisher_name: Option<String>,
pub link: Option<String>,
pub links: Vec<Link>,
pub contact: Contact,
pub event: String,
pub all_clear: bool,
pub severity: String,
pub test_alert: bool,
pub technical_test_alert: bool,
#[serde(deserialize_with = "super::datetime::deserialize_alert")]
pub publish_date: PrimitiveDateTime,
pub areas: Vec<Area>,
pub images: Vec<Image>,
pub image_titles: Vec<ImageTitle>,
pub image_descriptions: Vec<ImageDescription>,
pub event_icon_path: String,
pub reference: String,
}

13
app/src/json/alerts.rs Normal file
View file

@ -0,0 +1,13 @@
use super::alert::Alert;
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Alerts {
pub heartbeat_age_in_millis: u64,
#[serde(deserialize_with = "super::datetime::deserialize_alerts")]
pub render_time: OffsetDateTime,
#[serde(rename = "alerts")]
pub list: Vec<Alert>,
}

13
app/src/json/area.rs Normal file
View file

@ -0,0 +1,13 @@
use serde::{Deserialize, Serialize};
use super::{circle::Circle, description::Description, polygon::Polygon, region::Region};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Area {
pub description: Description,
pub region_type: String,
pub polygons: Vec<Polygon>,
pub circles: Vec<Circle>,
pub regions: Vec<Region>,
}

10
app/src/json/circle.rs Normal file
View file

@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};
use super::coordinate::Coordinate;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Circle {
pub center_position: Coordinate,
pub radius: String,
}

8
app/src/json/contact.rs Normal file
View file

@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Contact {
pub contact: String,
pub contact_origin: Option<String>,
}

View file

@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Coordinate {
#[serde(rename = "0")]
pub zero: String,
#[serde(rename = "1")]
pub one: String,
}

26
app/src/json/datetime.rs Normal file
View file

@ -0,0 +1,26 @@
use serde::{Deserialize, Deserializer};
use time::format_description::FormatItem;
use time::macros::format_description;
use time::{OffsetDateTime, PrimitiveDateTime};
const ALERT_FORMAT: &[FormatItem<'_>] =
format_description!("[weekday repr:short], [day].[month].[year], [hour]:[minute]");
const ALERTS_FORMAT: &[FormatItem<'_>] = format_description!(
"[day].[month].[year] [hour]:[minute]:[second].[subsecond] [offset_hour sign:mandatory][offset_minute]"
);
pub fn deserialize_alert<'de, D>(deserializer: D) -> Result<PrimitiveDateTime, D::Error>
where
D: Deserializer<'de>,
{
let s: &str = Deserialize::deserialize(deserializer)?;
PrimitiveDateTime::parse(s, ALERT_FORMAT).map_err(serde::de::Error::custom)
}
pub fn deserialize_alerts<'de, D>(deserializer: D) -> Result<OffsetDateTime, D::Error>
where
D: Deserializer<'de>,
{
let s: &str = Deserialize::deserialize(deserializer)?;
OffsetDateTime::parse(s, ALERTS_FORMAT).map_err(serde::de::Error::custom)
}

View file

@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Description {
pub description: String,
pub description_gtrans_origin: Option<String>,
}

10
app/src/json/image.rs Normal file
View file

@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Image {
pub uri: String,
pub digest: String,
pub index: u64,
pub size: String,
}

View file

@ -0,0 +1,11 @@
use serde::{Deserialize, Serialize};
use super::description::Description;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ImageDescription {
pub index: u64,
#[serde(flatten)]
pub description: Description,
}

View file

@ -0,0 +1,12 @@
use serde::{Deserialize, Serialize};
use super::title::Title;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ImageTitle {
pub index: u64,
#[serde(flatten)]
pub title: Title,
pub alt_text: String,
}

8
app/src/json/link.rs Normal file
View file

@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Link {
pub href: String,
pub text: String,
}

9
app/src/json/polygon.rs Normal file
View file

@ -0,0 +1,9 @@
use serde::{Deserialize, Serialize};
use super::coordinate::Coordinate;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Polygon {
pub coordinates: Vec<Coordinate>,
}

7
app/src/json/region.rs Normal file
View file

@ -0,0 +1,7 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Region {
pub region: String,
}

8
app/src/json/text.rs Normal file
View file

@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Text {
pub text: String,
pub text_origin: Option<String>,
}

8
app/src/json/title.rs Normal file
View file

@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Title {
pub title: String,
pub title_gtrans_origin: Option<String>,
}