-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathspeed_section.rs
227 lines (213 loc) · 8.62 KB
/
speed_section.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use std::collections::HashMap;
use itertools::Itertools as _;
use json_patch::Patch;
use json_patch::PatchOperation;
use json_patch::RemoveOperation;
use tracing::debug;
use tracing::error;
use super::Fix;
use super::OrderedOperation;
use crate::infra_cache::operation::CacheOperation;
use crate::infra_cache::operation::DeleteOperation;
use crate::infra_cache::operation::Operation;
use crate::infra_cache::operation::RailjsonObject;
use crate::infra_cache::operation::UpdateOperation;
use crate::schema::InfraError;
use crate::schema::InfraErrorType;
use crate::schema::SpeedSection;
use editoast_schemas::primitives::OSRDIdentified as _;
use editoast_schemas::primitives::OSRDObject as _;
use editoast_schemas::primitives::ObjectRef;
fn invalid_reference_to_ordered_operation(
speed_section: &SpeedSection,
object_ref: &ObjectRef,
) -> Option<OrderedOperation> {
let (track_range_idx, _) = speed_section
.track_ranges
.iter()
.enumerate()
.find(|(_idx, track_range)| track_range.track.as_str() == object_ref.obj_id)?;
Some(OrderedOperation::RemoveTrackRange { track_range_idx })
}
pub fn fix_speed_section(
speed_section: &SpeedSection,
errors: impl Iterator<Item = InfraError>,
) -> HashMap<ObjectRef, Fix> {
let operation = errors
.filter_map(|infra_error| match infra_error.get_sub_type() {
InfraErrorType::EmptyObject => Some(OrderedOperation::Delete),
InfraErrorType::InvalidReference { reference } => {
invalid_reference_to_ordered_operation(speed_section, reference)
}
_ => {
debug!("error not (yet) fixable for '{}'", infra_error.get_type());
None
}
})
// Need to invert the ordering because removing from the front would invalidate other indexes
.sorted_by_key(|ordered_operation| std::cmp::Reverse(ordered_operation.clone()))
.map(|ordered_operation| match ordered_operation {
OrderedOperation::RemoveTrackRange { track_range_idx } => {
Operation::Update(UpdateOperation {
obj_id: speed_section.get_id().clone(),
obj_type: speed_section.get_type(),
railjson_patch: Patch(vec![PatchOperation::Remove(RemoveOperation {
path: format!("/track_ranges/{track_range_idx}"),
})]),
})
}
OrderedOperation::Delete => {
Operation::Delete(DeleteOperation::from(speed_section.get_ref()))
}
})
.map(Some)
.reduce(super::reduce_operation)
.flatten();
operation
.and_then(|operation| {
let cache_operation = match CacheOperation::try_from_operation(
&operation,
RailjsonObject::SpeedSection {
railjson: speed_section.clone(),
},
) {
Ok(cache_operation) => cache_operation,
Err(e) => {
error!("failed to convert `Operation` on speed section into a `CacheOperation`: {e}");
return None;
}
};
Some((speed_section.get_ref(), (operation, cache_operation)))
})
.into_iter()
.collect()
}
#[cfg(test)]
mod tests {
use json_patch::Patch;
use crate::infra_cache::operation::CacheOperation;
use crate::infra_cache::operation::Operation;
use crate::infra_cache::ObjectCache;
use crate::schema::ApplicableDirectionsTrackRange;
use crate::schema::InfraError;
use crate::schema::SpeedSection;
use editoast_common::Identifier;
use editoast_schemas::infra::ApplicableDirections;
use editoast_schemas::primitives::OSRDObject as _;
use editoast_schemas::primitives::ObjectRef;
use editoast_schemas::primitives::ObjectType;
#[test]
fn invalid_refs_ordered_speed_section() {
let speed_section_cache = SpeedSection {
id: Identifier::from("speed_section_id"),
track_ranges: vec![
ApplicableDirectionsTrackRange {
track: Identifier::from("unknown_track_section_1"),
begin: 0.,
end: 100.,
applicable_directions: ApplicableDirections::StartToStop,
},
ApplicableDirectionsTrackRange {
track: Identifier::from("track_section_id"),
begin: 0.,
end: 100.,
applicable_directions: ApplicableDirections::StartToStop,
},
ApplicableDirectionsTrackRange {
track: Identifier::from("unknown_track_section_2"),
begin: 0.,
end: 100.,
applicable_directions: ApplicableDirections::StartToStop,
},
],
..Default::default()
};
let error_speed_section_1 = InfraError::new_invalid_reference(
&speed_section_cache,
"speed_section",
ObjectRef::new(ObjectType::TrackSection, "unknown_track_section_1"),
);
let error_speed_section_2 = InfraError::new_invalid_reference(
&speed_section_cache,
"speed_section",
ObjectRef::new(ObjectType::TrackSection, "unknown_track_section_2"),
);
let operations = super::fix_speed_section(
&speed_section_cache,
vec![error_speed_section_1, error_speed_section_2].into_iter(),
);
assert_eq!(operations.len(), 1);
let (operation, cache_operation) = operations.get(&speed_section_cache.get_ref()).unwrap();
let Operation::Update(update_operation) = operation else {
panic!("not an `Operation::Update`");
};
assert_eq!(update_operation.obj_id, "speed_section_id");
assert!(matches!(
update_operation.obj_type,
ObjectType::SpeedSection
));
assert_eq!(
update_operation.railjson_patch,
serde_json::from_str::<Patch>(
r#"[
{"op":"remove","path":"/track_ranges/2"},
{"op":"remove","path":"/track_ranges/0"}
]"#
)
.unwrap()
);
let CacheOperation::Update(ObjectCache::SpeedSection(speed_section)) = cache_operation
else {
panic!("not a `CacheOperation::Update(ObjectCache::SpeedSection())`");
};
assert_eq!(speed_section.track_ranges.len(), 1);
assert_eq!(speed_section.track_ranges[0].track.0, "track_section_id");
}
#[test]
fn empty_object_and_invalid_ref_speed_section() {
let speed_section_cache = SpeedSection {
id: Identifier::from("speed_section_id"),
track_ranges: vec![
ApplicableDirectionsTrackRange {
track: Identifier::from("unknown_track_section_1"),
begin: 0.,
end: 100.,
applicable_directions: ApplicableDirections::StartToStop,
},
ApplicableDirectionsTrackRange {
track: Identifier::from("track_section_id"),
begin: 0.,
end: 100.,
applicable_directions: ApplicableDirections::StartToStop,
},
],
..Default::default()
};
let error_speed_section_1 = InfraError::new_invalid_reference(
&speed_section_cache,
"speed_section",
ObjectRef::new(ObjectType::TrackSection, "unknown_track_section_1"),
);
let error_speed_section_2 =
InfraError::new_empty_object(&speed_section_cache, "track_ranges");
let operations = super::fix_speed_section(
&speed_section_cache,
vec![error_speed_section_1, error_speed_section_2].into_iter(),
);
assert_eq!(operations.len(), 1);
let (operation, cache_operation) = operations.get(&speed_section_cache.get_ref()).unwrap();
let Operation::Delete(delete_operation) = operation else {
panic!("not an `Operation::Delete`");
};
assert_eq!(delete_operation.obj_id, "speed_section_id");
assert!(matches!(
delete_operation.obj_type,
ObjectType::SpeedSection
));
let CacheOperation::Delete(object_ref) = cache_operation else {
panic!("not a `CacheOperation::Delete()`");
};
assert_eq!(object_ref.obj_id, "speed_section_id");
assert_eq!(object_ref.obj_type, ObjectType::SpeedSection);
}
}