|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | +use std::str::FromStr; |
| 3 | +use time::Duration; |
| 4 | + |
| 5 | +use crate::schema::utils::NonBlankString; |
| 6 | + |
| 7 | +#[derive(Debug, Default, Clone, Serialize, Deserialize)] |
| 8 | +pub struct PowerRestrictions { |
| 9 | + from: NonBlankString, |
| 10 | + to: NonBlankString, |
| 11 | + value: String, |
| 12 | +} |
| 13 | + |
| 14 | +#[derive(Debug, Default, Clone, Serialize, Deserialize)] |
| 15 | +pub struct TrainScheduleOptions { |
| 16 | + use_electrical_profiles: bool, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug, Default, Clone, Serialize, Deserialize)] |
| 20 | +pub struct ScheduleItem { |
| 21 | + at: NonBlankString, |
| 22 | + departure: Option<Duration>, |
| 23 | + stop_for: Option<Duration>, |
| 24 | + locked: bool, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 28 | +#[serde(untagged)] |
| 29 | +enum PathItemLocation { |
| 30 | + TrackOffset { |
| 31 | + track: NonBlankString, |
| 32 | + offset: f64, |
| 33 | + }, |
| 34 | + OperationalPointId { |
| 35 | + operational_point: NonBlankString, |
| 36 | + }, |
| 37 | + OperationalPointDescription { |
| 38 | + trigram: NonBlankString, |
| 39 | + secondary_code: Option<String>, |
| 40 | + }, |
| 41 | + OperationalPointUic { |
| 42 | + uic: NonBlankString, |
| 43 | + secondary_code: Option<String>, |
| 44 | + }, |
| 45 | +} |
| 46 | + |
| 47 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 48 | +pub struct PathItem { |
| 49 | + id: NonBlankString, |
| 50 | + #[serde(flatten)] |
| 51 | + location: PathItemLocation, |
| 52 | + deleted: bool, |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Debug, Default, Clone, Serialize, Deserialize)] |
| 56 | +pub struct Margins { |
| 57 | + boudaries: Vec<NonBlankString>, |
| 58 | + values: Vec<MarginValue>, |
| 59 | +} |
| 60 | + |
| 61 | +#[derive(Debug, Copy, Clone, Default)] |
| 62 | +enum MarginValue { |
| 63 | + #[default] |
| 64 | + Zero, |
| 65 | + Percentage(f64), |
| 66 | + MinPerKm(f64), |
| 67 | +} |
| 68 | + |
| 69 | +impl<'de> Deserialize<'de> for MarginValue { |
| 70 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 71 | + where |
| 72 | + D: serde::Deserializer<'de>, |
| 73 | + { |
| 74 | + let value = String::deserialize(deserializer)?; |
| 75 | + if value == "0" { |
| 76 | + return Ok(Self::Zero); |
| 77 | + } |
| 78 | + if value.ends_with('%') { |
| 79 | + let float_value: f64 = f64::from_str(&value[0..value.len() - 1]).map_err(|_| { |
| 80 | + serde::de::Error::invalid_value( |
| 81 | + serde::de::Unexpected::Str(&value), |
| 82 | + &"a valid float", |
| 83 | + ) |
| 84 | + })?; |
| 85 | + return Ok(Self::Percentage(float_value)); |
| 86 | + } |
| 87 | + return Err(serde::de::Error::custom("Invalid margin value")); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl Serialize for MarginValue { |
| 92 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 93 | + where |
| 94 | + S: serde::Serializer, |
| 95 | + { |
| 96 | + match self { |
| 97 | + MarginValue::Zero => serializer.serialize_str("0"), |
| 98 | + MarginValue::Percentage(value) => serializer.serialize_str(&format!("{}%", value)), |
| 99 | + MarginValue::MinPerKm(value) => serializer.serialize_str(&value.to_string()), |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments