-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtrack_section.rs
100 lines (92 loc) · 3.41 KB
/
track_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
use std::collections::HashMap;
use tracing::debug;
use uuid::Uuid;
use super::new_ref_fix_create_pair;
use super::Fix;
use crate::infra_cache::operation::RailjsonObject;
use crate::schema::BufferStop;
use crate::schema::InfraError;
use crate::schema::InfraErrorType;
use crate::schema::TrackSectionCache;
use editoast_common::Identifier;
use editoast_schemas::infra::Endpoint;
use editoast_schemas::primitives::OSRDIdentified as _;
use editoast_schemas::primitives::OSRDObject as _;
use editoast_schemas::primitives::ObjectRef;
pub fn fix_track_section(
track_section: &TrackSectionCache,
errors: impl Iterator<Item = InfraError>,
) -> HashMap<ObjectRef, Fix> {
errors
.filter_map(|infra_error| match infra_error.get_sub_type() {
InfraErrorType::MissingBufferStop { endpoint } => {
let track_id = infra_error.get_id();
let position = match endpoint {
Endpoint::Begin => 0.0,
Endpoint::End => track_section.length,
};
let buffer_stop = RailjsonObject::BufferStop {
railjson: (BufferStop {
id: Identifier::from(Uuid::new_v4()),
track: track_id.to_string().into(),
position,
..Default::default()
}),
};
Some(new_ref_fix_create_pair(buffer_stop))
}
_ => {
debug!("error not (yet) fixable for '{}'", infra_error.get_type());
None
}
})
.collect()
}
#[cfg(test)]
mod tests {
use std::ops::Deref;
use super::*;
use crate::infra_cache::operation::CacheOperation;
use crate::infra_cache::operation::Operation;
use crate::infra_cache::ObjectCache;
use crate::schema::TrackSection;
#[test]
fn missing_buffer_stop() {
let track_section = TrackSection {
id: Identifier::from("track_section_id"),
length: 42.0,
..Default::default()
};
let errors = vec![InfraError::new_missing_buffer_stop(
&track_section,
Endpoint::End,
)];
let operations = fix_track_section(
&TrackSectionCache::from(track_section.clone()),
errors.into_iter(),
);
assert_eq!(operations.len(), 1);
let (operation, cache_operation) = operations.into_values().next().unwrap();
let Operation::Create(railjson) = operation else {
panic!("expecting an `Operation::Create(_)`");
};
let railjson = railjson.deref().clone();
let RailjsonObject::BufferStop {
railjson: buffer_stop,
} = railjson
else {
panic!("expecting a `RailjsonObject::BufferStop {{ .. }}`")
};
assert_eq!(buffer_stop.track, track_section.id);
assert_eq!(buffer_stop.position, 42.0);
let CacheOperation::Create(object_cache) = cache_operation else {
panic!("expecting an `CacheOperation::Create(_)`");
};
let ObjectCache::BufferStop(buffer_stop_cache) = object_cache else {
panic!("expecting a `ObjectCache::BufferStop(_)`");
};
assert_eq!(buffer_stop_cache.obj_id, buffer_stop.id.as_str());
assert_eq!(buffer_stop_cache.track, track_section.id.as_str());
assert_eq!(buffer_stop_cache.position, 42.0);
}
}