refactor: this is so fun
This commit is contained in:
parent
c966529a2b
commit
f8ce315d8e
3 changed files with 41 additions and 38 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue