feat: parse hours

dev
Jef Roosens 2023-05-11 16:55:01 +02:00
parent c799d3d226
commit 0deb67478f
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 35 additions and 32 deletions

View File

@ -1,6 +1,6 @@
mod models; mod models;
use chrono::{NaiveDate, NaiveDateTime}; use chrono::NaiveDate;
use models::{Resource, Data, SiteData, Reservation, ReservationResponse}; use models::{Resource, Data, SiteData, Reservation, ReservationResponse};
const USER_AGENT: &str = "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/112.0"; const USER_AGENT: &str = "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/112.0";

View File

@ -1,9 +1,38 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use chrono::{NaiveTime, NaiveDate}; use chrono::{NaiveTime, NaiveDate};
mod time_format {
use chrono::NaiveTime;
use serde::{self, Deserialize, Serializer, Deserializer};
const FORMAT: &'static str = "%H:%M";
pub fn serialize<S>(
time: &NaiveTime,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let s = format!("{}", time.format(FORMAT));
serializer.serialize_str(&s)
}
pub fn deserialize<'de, D>(
deserializer: D,
) -> Result<NaiveTime, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
NaiveTime::parse_from_str(&s, FORMAT).map_err(serde::de::Error::custom)
}
}
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct Hour { pub struct Hour {
pub hour: String, #[serde(with = "time_format")]
pub hour: NaiveTime,
pub state: u32, pub state: u32,
// reservations // reservations
pub granularity: u32, pub granularity: u32,
@ -87,34 +116,6 @@ pub struct SiteDataNotice {
pub url: Option<String> pub url: Option<String>
} }
#[derive(Deserialize, Debug)]
pub struct SiteDataChild {
pub id: uuid::Uuid,
pub slug: String,
pub parent: Option<String>,
pub primary_name: String,
pub secondary_name: String,
pub concat_name: String,
pub categories: Vec<SiteDataCategory>,
pub time_zone: String,
pub location: SiteDataLocation,
pub phone_number: String,
pub email: String,
pub url: String,
pub notices: Vec<SiteDataNotice>,
// messages
pub estimated_distance: f64,
pub current_forecast: SiteDataForecast,
pub today_forecasts: Vec<SiteDataForecast>,
// events
// children
// actions
// services
// infos
pub poster_image: String,
// images
}
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct SiteDataService { pub struct SiteDataService {
pub id: u64, pub id: u64,
@ -179,8 +180,10 @@ pub struct Reservation {
pub auth_type: Option<String>, pub auth_type: Option<String>,
pub email: String, pub email: String,
pub date: NaiveDate, pub date: NaiveDate,
pub start_time: String, #[serde(with = "time_format")]
pub end_time: String, pub start_time: NaiveTime,
#[serde(with = "time_format")]
pub end_time: NaiveTime,
pub note: String, pub note: String,
pub user_firstname: String, pub user_firstname: String,
pub user_lastname: String, pub user_lastname: String,