refactor: this is so fun
Some checks failed
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/clippy Pipeline failed
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
Jef Roosens 2023-05-15 15:22:43 +02:00
parent c966529a2b
commit f8ce315d8e
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
3 changed files with 41 additions and 38 deletions

View file

@ -44,28 +44,25 @@ pub struct Resource {
impl Resource {
pub fn condensed_hours(&self) -> Vec<(&HourBlock, Duration)> {
let mut start_hour_opt: Option<&HourBlock> = None;
let mut duration = Duration::seconds(0);
if self.hours.is_empty() {
return Default::default();
}
let mut start_hour = self.hours.first().unwrap();
let mut duration = Duration::minutes(start_hour.granularity.into());
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);
for hour in self.hours.iter().skip(1) {
if hour.state == start_hour.state {
duration = duration + Duration::minutes(hour.granularity.into());
} else {
out.push((start_hour, duration));
start_hour = &hour;
duration = Duration::minutes(hour.granularity.into());
}
}
if let Some(start_hour) = start_hour_opt {
out.push((start_hour, duration));
}
out.push((start_hour, duration));
out
}