WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 24db3e2

Browse files
committed
editoast: ignore disabled occurrences in conflict detection
When an occurrence was modified to be disabled, it was not ignored in iter_occurrences and hence in conflict detection. Signed-off-by: Erashin <[email protected]>
1 parent 4b460da commit 24db3e2

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

editoast/src/models/paced_train.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl PacedTrain {
170170
}
171171

172172
/// Returns an iterator over all train occurrences, including:
173-
/// - base occurrences
173+
/// - base occurrences, minus the ones disabled by a modified exception
174174
/// - occurrences modified by exceptions (which replace a base one)
175175
/// - occurrences created by exceptions (additional trains)
176176
///
@@ -181,27 +181,37 @@ impl PacedTrain {
181181
pub fn iter_occurrences(&self) -> impl Iterator<Item = (TrainId, TrainSchedule)> {
182182
let mut base_occurrences = self.get_base_occurrences();
183183

184-
let enabled_modified_exceptions = self
184+
let modified_exceptions = self
185185
.exceptions
186186
.iter()
187-
.filter(|ex| !ex.disabled)
188187
.filter_map(|e| match e.exception_type {
189188
ExceptionType::Modified { occurrence_index } => Some((occurrence_index, e)),
190189
_ => None,
191190
});
192191

193-
for (occurrence_index, exception) in enabled_modified_exceptions {
192+
let mut to_remove = vec![false; base_occurrences.len()];
193+
// Modify corresponding occurrences.
194+
for (occurrence_index, exception) in modified_exceptions {
194195
if let Some(occurrence) = base_occurrences.get_mut(occurrence_index as usize) {
195-
let train_id = TrainId::PacedTrainModifiedException {
196-
paced_train_id: self.id,
197-
index: occurrence_index as u64,
198-
exception_key: exception.key.clone(),
199-
};
200-
*occurrence = (train_id, self.apply_exception(exception));
196+
if exception.disabled {
197+
to_remove[occurrence_index as usize] = true;
198+
} else {
199+
let train_id = TrainId::PacedTrainModifiedException {
200+
paced_train_id: self.id,
201+
index: occurrence_index as u64,
202+
exception_key: exception.key.clone(),
203+
};
204+
*occurrence = (train_id, self.apply_exception(exception));
205+
}
201206
}
202207
}
208+
// Remove disabled occurrences.
209+
let occurences = base_occurrences
210+
.into_iter()
211+
.zip(to_remove)
212+
.filter_map(|(occ, disabled)| if disabled { None } else { Some(occ) });
203213

204-
base_occurrences
214+
occurences
205215
.into_iter()
206216
.chain(self.get_created_occurrences_exceptions())
207217
.sorted_by_key(|(_, ts)| ts.start_time)
@@ -452,7 +462,7 @@ mod tests {
452462
let occurrences: Vec<(TrainId, schemas::TrainSchedule)> =
453463
paced_train.iter_occurrences().collect();
454464

455-
assert_eq!(occurrences.len(), 5);
465+
assert_eq!(occurrences.len(), 4);
456466

457467
let start_times: Vec<DateTime<Utc>> =
458468
occurrences.iter().map(|(_, o)| o.start_time).collect();
@@ -465,7 +475,6 @@ mod tests {
465475
assert_eq!(
466476
start_times,
467477
vec![
468-
DateTime::<Utc>::from_str("2025-05-15T14:00:00+02:00").unwrap(),
469478
DateTime::<Utc>::from_str("2025-05-15T14:30:00+02:00").unwrap(),
470479
DateTime::<Utc>::from_str("2025-05-15T15:00:00+02:00").unwrap(),
471480
DateTime::<Utc>::from_str("2025-05-15T15:10:00+02:00").unwrap(),
@@ -476,7 +485,6 @@ mod tests {
476485
assert_eq!(
477486
train_names,
478487
vec![
479-
"train_name".to_string(),
480488
"modified_exception_train_name".to_string(),
481489
"train_name".to_string(),
482490
"created_exception_train_name".to_string(),
@@ -487,10 +495,6 @@ mod tests {
487495
assert_eq!(
488496
types,
489497
vec![
490-
TrainId::PacedTrainBaseOccurrence {
491-
paced_train_id: paced_train.id,
492-
index: 0
493-
},
494498
TrainId::PacedTrainModifiedException {
495499
paced_train_id: paced_train.id,
496500
index: 1,

0 commit comments

Comments
 (0)