-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathspeed_section.rs
150 lines (129 loc) · 3.77 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
use std::collections::HashMap;
use derivative::Derivative;
use serde::Deserialize;
use serde::Serialize;
use utoipa::ToSchema;
use super::ApplicableDirectionsTrackRange;
use super::Sign;
use crate::primitives::Identifier;
use crate::primitives::NonBlankString;
use crate::primitives::OSRDIdentified;
use crate::primitives::OSRDTyped;
use crate::primitives::ObjectType;
editoast_common::schemas! {
SpeedSection,
}
#[derive(Debug, Derivative, Clone, Serialize, PartialEq, Copy, ToSchema)]
pub struct Speed(pub f64);
#[derive(Debug, Derivative, Clone, Deserialize, Serialize, PartialEq, ToSchema)]
#[derivative(Default)]
#[serde(deny_unknown_fields)]
pub struct SpeedSection {
#[schema(inline)]
pub id: Identifier,
#[derivative(Default(value = "Some(Speed(80.))"))]
#[schema(inline)]
pub speed_limit: Option<Speed>,
#[schema(inline)]
pub speed_limit_by_tag: HashMap<NonBlankString, Speed>,
pub track_ranges: Vec<ApplicableDirectionsTrackRange>,
#[serde(skip_serializing_if = "Option::is_none")]
#[schema(inline)]
pub on_routes: Option<Vec<Identifier>>,
#[serde(default)]
#[schema(inline)]
pub extensions: SpeedSectionExtensions,
}
impl<'de> Deserialize<'de> for Speed {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = f64::deserialize(deserializer)?;
if value <= 0.0 {
return Err(serde::de::Error::custom(
"expected speed to be greater than 0",
));
}
Ok(Speed(value))
}
}
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct SpeedSectionExtensions {
#[schema(inline)]
pub psl_sncf: Option<SpeedSectionPslSncfExtension>,
}
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct SpeedSectionPslSncfExtension {
announcement: Vec<Sign>,
z: Sign,
r: Vec<Sign>,
}
impl SpeedSectionPslSncfExtension {
pub fn announcement(&self) -> &Vec<Sign> {
&self.announcement
}
pub fn z(&self) -> &Sign {
&self.z
}
pub fn r(&self) -> &Vec<Sign> {
&self.r
}
}
impl OSRDTyped for SpeedSection {
fn get_type() -> ObjectType {
ObjectType::SpeedSection
}
}
impl OSRDIdentified for SpeedSection {
fn get_id(&self) -> &String {
&self.id
}
}
#[cfg(test)]
mod tests {
use serde_json::from_str;
use serde_json::from_value;
use serde_json::json;
use super::SpeedSection;
use super::SpeedSectionExtensions;
#[test]
fn test_speed_section_extensions_deserialization() {
from_str::<SpeedSectionExtensions>(r#"{}"#).unwrap();
}
// SpeedSection validation succeed
#[test]
fn test_valid_speed_section() {
let section = json!({
"id": "section_id",
"speed_limit": 50.0,
"speed_limit_by_tag": {},
"track_ranges": [],
});
assert!(from_value::<SpeedSection>(section).is_ok());
}
// SpeedSection validation failed caused by speed_limit
#[test]
fn test_invalid_speed_limit() {
let section = json!({
"id": "section_id",
"speed_limit": -10.0,
"speed_limit_by_tag": {},
"track_ranges": [],
});
assert!(from_value::<SpeedSection>(section).is_err());
}
// SpeedSection validation failed caused by speed_limit_by_tag
#[test]
fn test_invalid_speed_limit_by_tag() {
let section = json!({
"id": "section_id",
"speed_limit": 50.0,
"speed_limit_by_tag": {"tag": 0},
"track_ranges": [],
});
assert!(from_value::<SpeedSection>(section).is_err());
}
}