fix: update serialization for new state string type

This commit is contained in:
Jef Roosens 2024-02-15 14:00:09 +01:00
parent 83e9d3e8cf
commit 7fd6d55e71
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
6 changed files with 47 additions and 16 deletions

View file

@ -1,12 +1,19 @@
use super::hh_mm_time_format;
use chrono::{Duration, NaiveTime};
use serde::Deserialize;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Debug, Clone, Copy)]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ResourceState {
Available,
Full,
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct HourBlock {
#[serde(with = "hh_mm_time_format")]
pub hour: NaiveTime,
pub state: u32,
pub state: ResourceState,
// reservations
pub granularity: u32,
pub person_count: u32,
@ -14,7 +21,7 @@ pub struct HourBlock {
pub places_bookable: u32,
}
#[derive(Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug)]
pub struct Resource {
pub resource_id: u32,
pub resource_name: String,
@ -38,7 +45,7 @@ pub struct Resource {
// max_places_per_reservation
pub image_url: Option<String>,
// services
pub slots_state: u32,
pub slots_state: ResourceState,
pub hours: Vec<HourBlock>,
}
@ -70,13 +77,18 @@ impl Resource {
pub fn condensed_available_hours(&self) -> Vec<(&HourBlock, Duration)> {
self.condensed_hours()
.into_iter()
.filter(|(hour, _)| hour.state == 1)
.filter(|(hour, _)| hour.state == ResourceState::Available)
.collect()
}
/// Returns whether a slot with the given state and time bounds is present in the list of
/// hours.
pub fn has_slot(&self, start_time: NaiveTime, end_time: NaiveTime, state: u32) -> bool {
pub fn has_slot(
&self,
start_time: NaiveTime,
end_time: NaiveTime,
state: ResourceState,
) -> bool {
self.condensed_hours()
.into_iter()
.filter(|(block, _)| block.state == state)