Skip to content

Commit

Permalink
fixup! fixup! fixup! editoast, front, core: add ETCS brake params to …
Browse files Browse the repository at this point in the history
…rolling-stock model

editoast: use (remote = "Self") for Deserialize
  • Loading branch information
bougue-pe committed Jan 7, 2025
1 parent a9e4b67 commit e28a6af
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 76 deletions.
73 changes: 15 additions & 58 deletions editoast/editoast_schemas/src/rolling_stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub use etcs_brake_params::EtcsBrakeParams;

mod supported_signaling_systems;
use serde::Deserializer;
use serde::Serializer;
pub use supported_signaling_systems::RollingStockSupportedSignalingSystems;

mod rolling_stock_metadata;
Expand Down Expand Up @@ -52,7 +53,8 @@ editoast_common::schemas! {

pub const ROLLING_STOCK_RAILJSON_VERSION: &str = "3.2";

#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(remote = "Self")]
pub struct RollingStock {
pub name: String,
pub locked: bool,
Expand Down Expand Up @@ -99,72 +101,27 @@ impl<'de> Deserialize<'de> for RollingStock {
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Inner {
pub name: String,
pub locked: bool,
pub effort_curves: EffortCurves,
pub base_power_class: Option<String>,
pub length: f64,
pub max_speed: f64,
pub startup_time: f64,
pub startup_acceleration: f64,
pub comfort_acceleration: f64,
pub const_gamma: f64,
pub etcs_brake_params: Option<EtcsBrakeParams>,
pub inertia_coefficient: f64,
pub mass: f64,
pub rolling_resistance: RollingResistance,
pub loading_gauge: LoadingGaugeType,
#[serde(default)]
pub power_restrictions: HashMap<String, String>,
#[serde(default)]
pub energy_sources: Vec<EnergySource>,
pub electrical_power_startup_time: Option<f64>,
#[serde(default)]
pub raise_pantograph_time: Option<f64>,
pub supported_signaling_systems: RollingStockSupportedSignalingSystems,
pub railjson_version: String,
#[serde(default)]
pub metadata: Option<RollingStockMetadata>,
}

let inner = Inner::deserialize(deserializer)?;
let rolling_stock = RollingStock::deserialize(deserializer)?;

if inner
if rolling_stock
.supported_signaling_systems
.0
.contains(&"ETCS_LEVEL2".to_string())
&& inner.etcs_brake_params.is_none()
&& rolling_stock.etcs_brake_params.is_none()
{
return Err(serde::de::Error::custom(
"invalid rolling-stock, supporting ETCS_LEVEL2 signaling system requires providing ETCS brake parameters.",
));
}
Ok(rolling_stock)
}
}

Ok(RollingStock {
name: inner.name,
locked: inner.locked,
effort_curves: inner.effort_curves,
base_power_class: inner.base_power_class,
length: inner.length,
max_speed: inner.max_speed,
startup_time: inner.startup_time,
startup_acceleration: inner.startup_acceleration,
comfort_acceleration: inner.comfort_acceleration,
const_gamma: inner.const_gamma,
etcs_brake_params: inner.etcs_brake_params,
inertia_coefficient: inner.inertia_coefficient,
mass: inner.mass,
rolling_resistance: inner.rolling_resistance,
loading_gauge: inner.loading_gauge,
power_restrictions: inner.power_restrictions,
energy_sources: inner.energy_sources,
electrical_power_startup_time: inner.electrical_power_startup_time,
raise_pantograph_time: inner.raise_pantograph_time,
supported_signaling_systems: inner.supported_signaling_systems,
railjson_version: inner.railjson_version,
metadata: inner.metadata,
})
impl Serialize for RollingStock {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
RollingStock::serialize(self, serializer)
}
}
37 changes: 19 additions & 18 deletions editoast/editoast_schemas/src/rolling_stock/etcs_brake_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use derivative::Derivative;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;
use utoipa::ToSchema;

editoast_common::schemas! {
Expand Down Expand Up @@ -50,9 +51,9 @@ pub struct EtcsBrakeParams {
pub t_be: f64,
}

#[derive(Clone, Debug, PartialEq, Serialize, ToSchema, Derivative)]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ToSchema, Derivative)]
#[derivative(Hash)]
#[serde(deny_unknown_fields)]
#[serde(deny_unknown_fields, remote = "Self")]
pub struct SpeedIntervalValueCurve {
#[derivative(Hash(hash_with = "editoast_common::hash_float_slice::<3,_>"))]
#[schema(example = json!([8.333333, 19.444444]))]
Expand All @@ -71,35 +72,29 @@ impl<'de> Deserialize<'de> for SpeedIntervalValueCurve {
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct InnerParams {
boundaries: Vec<f64>,
values: Vec<f64>,
}
let curve = SpeedIntervalValueCurve::deserialize(deserializer)?;

let inner = InnerParams::deserialize(deserializer)?;

if inner.boundaries.len() != inner.values.len() - 1 {
if curve.boundaries.len() != curve.values.len() - 1 {
return Err(serde::de::Error::custom(
"curve invalid, expected one more value than boundaries.",
));
}
if inner.values.is_empty() {
if curve.values.is_empty() {
return Err(serde::de::Error::custom(
"curve should have at least 1 value.",
));
}
if inner.values.iter().any(|&x| x < 0.0) {
if curve.values.iter().any(|&x| x < 0.0) {
return Err(serde::de::Error::custom(
"curve values must be equal or greater than 0.",
));
};
if inner.boundaries.iter().any(|&x| x < 0.0) {
if curve.boundaries.iter().any(|&x| x < 0.0) {
return Err(serde::de::Error::custom(
"speed boundaries must be equal or greater than 0.",
));
};
if inner
if curve
.boundaries
.windows(2)
.any(|window| window[0] >= window[1])
Expand All @@ -109,9 +104,15 @@ impl<'de> Deserialize<'de> for SpeedIntervalValueCurve {
));
}

Ok(SpeedIntervalValueCurve {
boundaries: inner.boundaries,
values: inner.values,
})
Ok(curve)
}
}

impl Serialize for SpeedIntervalValueCurve {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
SpeedIntervalValueCurve::serialize(self, serializer)
}
}

0 comments on commit e28a6af

Please sign in to comment.