Skip to content

Commit ed8229b

Browse files
committed
fixup! editoast: add schema for train schedule v2
1 parent 24f6074 commit ed8229b

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

editoast/src/schema/v2/trainschedule.rs

+13-23
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ pub struct PathItem {
222222
pub struct Margins {
223223
#[schema(inline)]
224224
pub boundaries: Vec<NonBlankString>,
225-
#[derivative(Default(value = "vec![MarginValue::Zero]"))]
225+
#[derivative(Default(value = "vec![MarginValue::None]"))]
226226
/// The values of the margins. Must contains one more element than the boundaries
227227
/// Can be a percentage `X%`, a time in minutes per kilometer `Xmin/km` or `0`
228228
#[schema(value_type = Vec<String>, example = json!(["0", "5%", "2min/km"]))]
@@ -253,7 +253,7 @@ impl<'de> Deserialize<'de> for Margins {
253253
#[derive(Debug, Copy, Clone, Default, PartialEq)]
254254
pub enum MarginValue {
255255
#[default]
256-
Zero,
256+
None,
257257
Percentage(f64),
258258
MinPerKm(f64),
259259
}
@@ -264,8 +264,8 @@ impl<'de> Deserialize<'de> for MarginValue {
264264
D: serde::Deserializer<'de>,
265265
{
266266
let value = String::deserialize(deserializer)?;
267-
if value == "0" {
268-
return Ok(Self::Zero);
267+
if value.to_lowercase() == "none" {
268+
return Ok(Self::None);
269269
}
270270
if value.ends_with('%') {
271271
let float_value = f64::from_str(value[0..value.len() - 1].trim()).map_err(|_| {
@@ -274,15 +274,12 @@ impl<'de> Deserialize<'de> for MarginValue {
274274
&"a valid float",
275275
)
276276
})?;
277-
if float_value < 0.0 {
277+
if float_value <= 0.0 {
278278
return Err(serde::de::Error::invalid_value(
279279
serde::de::Unexpected::Str(&value),
280-
&"a positive float",
280+
&"a strictly positive number",
281281
));
282282
}
283-
if float_value == 0. {
284-
return Ok(Self::Zero);
285-
}
286283
return Ok(Self::Percentage(float_value));
287284
}
288285
if value.ends_with("min/km") {
@@ -293,15 +290,12 @@ impl<'de> Deserialize<'de> for MarginValue {
293290
&"a valid float",
294291
)
295292
})?;
296-
if float_value < 0.0 {
293+
if float_value <= 0.0 {
297294
return Err(serde::de::Error::invalid_value(
298295
serde::de::Unexpected::Str(&value),
299-
&"a positive float",
296+
&"a strictly positive float",
300297
));
301298
}
302-
if float_value == 0. {
303-
return Ok(Self::Zero);
304-
}
305299
return Ok(Self::MinPerKm(float_value));
306300
}
307301
Err(serde::de::Error::custom("Margin type not recognized"))
@@ -314,7 +308,7 @@ impl Serialize for MarginValue {
314308
S: serde::Serializer,
315309
{
316310
match self {
317-
MarginValue::Zero => serializer.serialize_str("0"),
311+
MarginValue::None => serializer.serialize_str("none"),
318312
MarginValue::Percentage(value) => serializer.serialize_str(&format!("{}%", value)),
319313
MarginValue::MinPerKm(value) => serializer.serialize_str(&format!("{}min/km", value)),
320314
}
@@ -349,12 +343,8 @@ mod tests {
349343
/// Test that the `MarginValue` enum can be deserialized from a string
350344
#[test]
351345
fn deserialize_margin_value() {
352-
let zero: MarginValue = from_str(r#""0""#).unwrap();
353-
assert_eq!(zero, MarginValue::Zero);
354-
let percentage: MarginValue = from_str(r#""0%""#).unwrap();
355-
assert_eq!(percentage, MarginValue::Zero);
356-
let min_per_km: MarginValue = from_str(r#""0min/km""#).unwrap();
357-
assert_eq!(min_per_km, MarginValue::Zero);
346+
let none: MarginValue = from_str(r#""none""#).unwrap();
347+
assert_eq!(none, MarginValue::None);
358348

359349
let percentage: MarginValue = from_str(r#""10%""#).unwrap();
360350
assert_eq!(percentage, MarginValue::Percentage(10.0));
@@ -374,8 +364,8 @@ mod tests {
374364
/// Test that the `MarginValue` enum can be serialized to a string
375365
#[test]
376366
fn serialize_margin_value() {
377-
let zero = to_string(&MarginValue::Zero).unwrap();
378-
assert_eq!(zero, r#""0""#);
367+
let none = to_string(&MarginValue::None).unwrap();
368+
assert_eq!(none, r#""none""#);
379369

380370
let percentage = to_string(&MarginValue::Percentage(10.0)).unwrap();
381371
assert_eq!(percentage, r#""10%""#);

editoast/src/tests/train_schedules/simple.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@
5151
],
5252
"margins": {
5353
"boundaries": [
54-
"b"
54+
"b",
55+
"c"
5556
],
5657
"values": [
5758
"5%",
58-
"3%"
59+
"3min/km",
60+
"none"
5961
]
6062
},
6163
"initial_speed": 2.5,

0 commit comments

Comments
 (0)