refactor: clean up some code
This commit is contained in:
parent
a74cf76b2b
commit
2999ca2301
4 changed files with 58 additions and 103 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use super::hh_mm_time_format;
|
||||
use chrono::NaiveTime;
|
||||
use chrono::{Duration, NaiveTime};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize, Debug, Clone, Copy)]
|
||||
|
|
@ -41,3 +41,39 @@ pub struct Resource {
|
|||
pub slots_state: u32,
|
||||
pub hours: Vec<HourBlock>,
|
||||
}
|
||||
|
||||
impl Resource {
|
||||
pub fn condensed_hours(&self) -> Vec<(&HourBlock, Duration)> {
|
||||
let mut start_hour_opt: Option<&HourBlock> = None;
|
||||
let mut duration = Duration::seconds(0);
|
||||
let mut out: Vec<(&HourBlock, Duration)> = Default::default();
|
||||
|
||||
for hour in &self.hours {
|
||||
if let Some(start_hour) = start_hour_opt {
|
||||
if hour.state == start_hour.state {
|
||||
duration = duration + Duration::minutes(hour.granularity.into());
|
||||
} else {
|
||||
out.push((start_hour, duration));
|
||||
start_hour_opt = Some(hour);
|
||||
duration = Duration::minutes(hour.granularity.into());
|
||||
}
|
||||
} else if hour.state == 1 {
|
||||
start_hour_opt = Some(hour);
|
||||
duration = Duration::minutes(hour.granularity.into());
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(start_hour) = start_hour_opt {
|
||||
out.push((start_hour, duration));
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
pub fn condensed_available_hours(&self) -> Vec<(&HourBlock, Duration)> {
|
||||
self.condensed_hours()
|
||||
.into_iter()
|
||||
.filter(|(hour, _)| hour.state == 1)
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue