From 038f823d985938a37a0aee44baaf4958d356e6ea Mon Sep 17 00:00:00 2001 From: Egor Date: Mon, 8 Apr 2024 17:37:59 +0200 Subject: [PATCH] editoast: remove rolling stock common --- .../editoast_schemas/src/rolling_stock/mod.rs | 33 ++++- .../src/rolling_stock/rolling_stock_common.rs | 45 ------- editoast/src/fixtures.rs | 4 +- editoast/src/models/rolling_stock/mod.rs | 23 ++-- editoast/src/views/rolling_stocks/mod.rs | 52 ++++---- .../rolling_stocks/rolling_stock_form.rs | 118 +++++++++++------- 6 files changed, 137 insertions(+), 138 deletions(-) delete mode 100644 editoast/editoast_schemas/src/rolling_stock/rolling_stock_common.rs diff --git a/editoast/editoast_schemas/src/rolling_stock/mod.rs b/editoast/editoast_schemas/src/rolling_stock/mod.rs index 70190543bbe..3e7ff8ef492 100644 --- a/editoast/editoast_schemas/src/rolling_stock/mod.rs +++ b/editoast/editoast_schemas/src/rolling_stock/mod.rs @@ -27,15 +27,13 @@ pub use rolling_stock_metadata::RollingStockMetadata; mod loading_gauge_type; pub use loading_gauge_type::LoadingGaugeType; -mod rolling_stock_common; -pub use rolling_stock_common::RollingStockCommon; - mod rolling_stock_livery; pub use rolling_stock_livery::RollingStockLivery; pub use rolling_stock_livery::RollingStockLiveryMetadata; use serde::Deserialize; use serde::Serialize; +use std::collections::HashMap; use utoipa::ToSchema; editoast_common::schemas! { @@ -55,7 +53,6 @@ editoast_common::schemas! { RollingStockSupportedSignalingSystems, RollingStockMetadata, LoadingGaugeType, - RollingStockCommon, RollingStockLivery, RollingStockLiveryMetadata, } @@ -66,7 +63,33 @@ pub const ROLLING_STOCK_RAILJSON_VERSION: &str = "3.2"; pub struct RollingStock { pub id: i64, #[serde(flatten)] - pub common: RollingStockCommon, + pub name: String, + pub effort_curves: EffortCurves, + #[schema(example = "5", required)] + pub base_power_class: Option, + pub length: f64, + pub max_speed: f64, + pub startup_time: f64, + pub startup_acceleration: f64, + pub comfort_acceleration: f64, + pub gamma: Gamma, + pub inertia_coefficient: f64, + pub mass: f64, + pub rolling_resistance: RollingResistance, + pub loading_gauge: LoadingGaugeType, + /// Mapping of power restriction code to power class + #[serde(default)] + #[schema(required)] + pub power_restrictions: HashMap, + #[serde(default)] + pub energy_sources: Vec, + /// The time the train takes before actually using electrical power (in seconds). Is null if the train is not electric. + #[schema(example = 5.0)] + pub electrical_power_startup_time: Option, + /// The time it takes to raise this train's pantograph in seconds. Is null if the train is not electric. + #[schema(example = 15.0)] + pub raise_pantograph_time: Option, + pub supported_signaling_systems: RollingStockSupportedSignalingSystems, pub railjson_version: String, /// Whether the rolling stock can be edited/deleted or not. pub locked: bool, diff --git a/editoast/editoast_schemas/src/rolling_stock/rolling_stock_common.rs b/editoast/editoast_schemas/src/rolling_stock/rolling_stock_common.rs deleted file mode 100644 index 28eff2c8f0e..00000000000 --- a/editoast/editoast_schemas/src/rolling_stock/rolling_stock_common.rs +++ /dev/null @@ -1,45 +0,0 @@ -use std::collections::HashMap; - -use derivative::Derivative; -use serde::Deserialize; -use serde::Serialize; -use utoipa::ToSchema; - -use super::EffortCurves; -use super::EnergySource; -use super::Gamma; -use super::LoadingGaugeType; -use super::RollingResistance; -use super::RollingStockSupportedSignalingSystems; - -#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, Derivative)] -#[derivative(PartialEq)] -pub struct RollingStockCommon { - pub name: String, - pub effort_curves: EffortCurves, - #[schema(example = "5", required)] - pub base_power_class: Option, - pub length: f64, - pub max_speed: f64, - pub startup_time: f64, - pub startup_acceleration: f64, - pub comfort_acceleration: f64, - pub gamma: Gamma, - pub inertia_coefficient: f64, - pub mass: f64, - pub rolling_resistance: RollingResistance, - pub loading_gauge: LoadingGaugeType, - /// Mapping of power restriction code to power class - #[serde(default)] - #[schema(required)] - pub power_restrictions: HashMap, - #[serde(default)] - pub energy_sources: Vec, - /// The time the train takes before actually using electrical power (in seconds). Is null if the train is not electric. - #[schema(example = 5.0)] - pub electrical_power_startup_time: Option, - /// The time it takes to raise this train's pantograph in seconds. Is null if the train is not electric. - #[schema(example = 15.0)] - pub raise_pantograph_time: Option, - pub supported_signaling_systems: RollingStockSupportedSignalingSystems, -} diff --git a/editoast/src/fixtures.rs b/editoast/src/fixtures.rs index a49abcfb6f8..60feb17649e 100644 --- a/editoast/src/fixtures.rs +++ b/editoast/src/fixtures.rs @@ -154,7 +154,7 @@ pub mod tests { let mut rolling_stock_form: RollingStockForm = serde_json::from_str(include_str!("./tests/example_rolling_stock_1.json")) .expect("Unable to parse"); - rolling_stock_form.common.name = name.to_string(); + rolling_stock_form.name = name.to_string(); rolling_stock_form } @@ -172,7 +172,7 @@ pub mod tests { "./tests/example_rolling_stock_2_energy_sources.json" )) .expect("Unable to parse"); - rolling_stock_form.common.name = name.to_string(); + rolling_stock_form.name = name.to_string(); rolling_stock_form } diff --git a/editoast/src/models/rolling_stock/mod.rs b/editoast/src/models/rolling_stock/mod.rs index a5706585ad2..f853205e0c4 100644 --- a/editoast/src/models/rolling_stock/mod.rs +++ b/editoast/src/models/rolling_stock/mod.rs @@ -1,10 +1,14 @@ -use editoast_schemas::rolling_stock::{RollingStock, RollingStockCommon}; +use editoast_schemas::rolling_stock::RollingStock; use crate::modelsv2::rolling_stock_model::RollingStockModel; -impl From for RollingStockCommon { +impl From for RollingStock { fn from(rolling_stock_model: RollingStockModel) -> Self { - RollingStockCommon { + RollingStock { + railjson_version: rolling_stock_model.railjson_version, + id: rolling_stock_model.id, + metadata: rolling_stock_model.metadata, + locked: rolling_stock_model.locked, name: rolling_stock_model.name, effort_curves: rolling_stock_model.effort_curves, base_power_class: rolling_stock_model.base_power_class, @@ -27,19 +31,6 @@ impl From for RollingStockCommon { } } -impl From for RollingStock { - fn from(rolling_stock_model: RollingStockModel) -> Self { - let rolling_stock_common: RollingStockCommon = rolling_stock_model.clone().into(); - RollingStock { - id: rolling_stock_model.id, - common: rolling_stock_common, - railjson_version: rolling_stock_model.railjson_version, - locked: rolling_stock_model.locked, - metadata: rolling_stock_model.metadata, - } - } -} - #[cfg(test)] pub mod tests { use actix_web::web::Data; diff --git a/editoast/src/views/rolling_stocks/mod.rs b/editoast/src/views/rolling_stocks/mod.rs index 72d2fe17dc6..cdaba143fc8 100644 --- a/editoast/src/views/rolling_stocks/mod.rs +++ b/editoast/src/views/rolling_stocks/mod.rs @@ -204,7 +204,7 @@ async fn create( ) -> Result> { rolling_stock_form.validate()?; let mut db_conn = db_pool.get().await?; - let rolling_stock_name = rolling_stock_form.common.name.clone(); + let rolling_stock_name = rolling_stock_form.name.clone(); let rolling_stock_changeset: Changeset = rolling_stock_form.into(); let rolling_stock = rolling_stock_changeset @@ -247,7 +247,7 @@ async fn update( let existing_rs_version = existing_rs.version; assert_rolling_stock_unlocked(existing_rs.clone())?; - let rolling_stock_name = rolling_stock_form.common.name.clone(); + let rolling_stock_name = rolling_stock_form.name.clone(); let existing_rs_form: RollingStockForm = existing_rs.into(); @@ -606,7 +606,7 @@ pub mod tests { // THEN let response_body: RollingStock = assert_status_and_read!(response, StatusCode::OK); - assert_eq!(response_body.common.name, name); + assert_eq!(response_body.name, name); } #[rstest] @@ -644,27 +644,27 @@ pub mod tests { let response_body = RollingStockModel::changeset() .railjson_version(response_body.railjson_version) - .name(response_body.common.name) - .effort_curves(response_body.common.effort_curves) + .name(response_body.name) + .effort_curves(response_body.effort_curves) .metadata(response_body.metadata) - .length(response_body.common.length) - .max_speed(response_body.common.max_speed) - .startup_time(response_body.common.startup_time) - .startup_acceleration(response_body.common.startup_acceleration) - .comfort_acceleration(response_body.common.comfort_acceleration) - .gamma(response_body.common.gamma) - .inertia_coefficient(response_body.common.inertia_coefficient) - .base_power_class(response_body.common.base_power_class) - .mass(response_body.common.mass) - .rolling_resistance(response_body.common.rolling_resistance) - .loading_gauge(response_body.common.loading_gauge) - .power_restrictions(response_body.common.power_restrictions) - .energy_sources(response_body.common.energy_sources) - .electrical_power_startup_time(response_body.common.electrical_power_startup_time) - .raise_pantograph_time(response_body.common.raise_pantograph_time) - .supported_signaling_systems(response_body.common.supported_signaling_systems); - - assert_eq!(response_body.name, Some(rolling_stock_form.common.name)); + .length(response_body.length) + .max_speed(response_body.max_speed) + .startup_time(response_body.startup_time) + .startup_acceleration(response_body.startup_acceleration) + .comfort_acceleration(response_body.comfort_acceleration) + .gamma(response_body.gamma) + .inertia_coefficient(response_body.inertia_coefficient) + .base_power_class(response_body.base_power_class) + .mass(response_body.mass) + .rolling_resistance(response_body.rolling_resistance) + .loading_gauge(response_body.loading_gauge) + .power_restrictions(response_body.power_restrictions) + .energy_sources(response_body.energy_sources) + .electrical_power_startup_time(response_body.electrical_power_startup_time) + .raise_pantograph_time(response_body.raise_pantograph_time) + .supported_signaling_systems(response_body.supported_signaling_systems); + + assert_eq!(response_body.name, Some(rolling_stock_form.name)); // Check rolling_stock deletion let delete_request = rolling_stock_delete_request(rolling_stock_id); @@ -704,7 +704,7 @@ pub mod tests { let mut rolling_stock_form = get_fast_rolling_stock_form( "fast_rolling_stock_create_rolling_stock_with_base_power_class_empty", ); - rolling_stock_form.common.base_power_class = Some("".to_string()); + rolling_stock_form.base_power_class = Some("".to_string()); // WHEN let post_response = call_service( @@ -727,7 +727,7 @@ pub mod tests { let fast_rolling_stock = named_fast_rolling_stock(name, db_pool.clone()).await; let app = create_test_service().await; let mut rolling_stock_form = get_fast_rolling_stock_form(name); - rolling_stock_form.common.name = fast_rolling_stock.model.name.clone(); + rolling_stock_form.name = fast_rolling_stock.model.name.clone(); // WHEN let post_response = call_service( @@ -859,7 +859,7 @@ pub mod tests { let rolling_stock_id = fast_rolling_stock.id(); let mut rolling_stock_form: RollingStockForm = fast_rolling_stock.model.clone().into(); - rolling_stock_form.common.name = + rolling_stock_form.name = "other_rolling_stock_update_unlocked_rolling_stock".to_string(); // WHEN diff --git a/editoast/src/views/rolling_stocks/rolling_stock_form.rs b/editoast/src/views/rolling_stocks/rolling_stock_form.rs index e09e545b020..2749b6186c5 100644 --- a/editoast/src/views/rolling_stocks/rolling_stock_form.rs +++ b/editoast/src/views/rolling_stocks/rolling_stock_form.rs @@ -1,6 +1,13 @@ +use std::collections::HashMap; + use derivative::Derivative; -use editoast_schemas::rolling_stock::RollingStockCommon; +use editoast_schemas::rolling_stock::EffortCurves; +use editoast_schemas::rolling_stock::EnergySource; +use editoast_schemas::rolling_stock::Gamma; +use editoast_schemas::rolling_stock::LoadingGaugeType; +use editoast_schemas::rolling_stock::RollingResistance; use editoast_schemas::rolling_stock::RollingStockMetadata; +use editoast_schemas::rolling_stock::RollingStockSupportedSignalingSystems; use editoast_schemas::rolling_stock::ROLLING_STOCK_RAILJSON_VERSION; use serde_derive::Deserialize; use serde_derive::Serialize; @@ -17,8 +24,33 @@ use crate::modelsv2::RollingStockModel; #[derivative(PartialEq)] #[validate(schema(function = "validate_rolling_stock_form"))] pub struct RollingStockForm { - #[serde(flatten)] - pub common: RollingStockCommon, + pub name: String, + pub effort_curves: EffortCurves, + #[schema(example = "5", required)] + pub base_power_class: Option, + pub length: f64, + pub max_speed: f64, + pub startup_time: f64, + pub startup_acceleration: f64, + pub comfort_acceleration: f64, + pub gamma: Gamma, + pub inertia_coefficient: f64, + pub mass: f64, + pub rolling_resistance: RollingResistance, + pub loading_gauge: LoadingGaugeType, + /// Mapping of power restriction code to power class + #[serde(default)] + #[schema(required)] + pub power_restrictions: HashMap, + #[serde(default)] + pub energy_sources: Vec, + /// The time the train takes before actually using electrical power (in seconds). Is null if the train is not electric. + #[schema(example = 5.0)] + pub electrical_power_startup_time: Option, + /// The time it takes to raise this train's pantograph in seconds. Is null if the train is not electric. + #[schema(example = 15.0)] + pub raise_pantograph_time: Option, + pub supported_signaling_systems: RollingStockSupportedSignalingSystems, #[derivative(PartialEq = "ignore")] pub locked: Option, #[derivative(PartialEq = "ignore")] @@ -28,26 +60,24 @@ pub struct RollingStockForm { impl From for RollingStockForm { fn from(value: RollingStockModel) -> Self { RollingStockForm { - common: RollingStockCommon { - name: value.name, - effort_curves: value.effort_curves, - base_power_class: value.base_power_class, - length: value.length, - max_speed: value.max_speed, - startup_time: value.startup_time, - startup_acceleration: value.startup_acceleration, - comfort_acceleration: value.comfort_acceleration, - gamma: value.gamma, - inertia_coefficient: value.inertia_coefficient, - mass: value.mass, - rolling_resistance: value.rolling_resistance, - loading_gauge: value.loading_gauge, - power_restrictions: value.power_restrictions, - energy_sources: value.energy_sources, - electrical_power_startup_time: value.electrical_power_startup_time, - raise_pantograph_time: value.raise_pantograph_time, - supported_signaling_systems: value.supported_signaling_systems, - }, + name: value.name, + effort_curves: value.effort_curves, + base_power_class: value.base_power_class, + length: value.length, + max_speed: value.max_speed, + startup_time: value.startup_time, + startup_acceleration: value.startup_acceleration, + comfort_acceleration: value.comfort_acceleration, + gamma: value.gamma, + inertia_coefficient: value.inertia_coefficient, + mass: value.mass, + rolling_resistance: value.rolling_resistance, + loading_gauge: value.loading_gauge, + power_restrictions: value.power_restrictions, + energy_sources: value.energy_sources, + electrical_power_startup_time: value.electrical_power_startup_time, + raise_pantograph_time: value.raise_pantograph_time, + supported_signaling_systems: value.supported_signaling_systems, locked: Some(value.locked), metadata: value.metadata, } @@ -60,24 +90,24 @@ impl From for Changeset { .railjson_version(ROLLING_STOCK_RAILJSON_VERSION.to_string()) .flat_locked(rolling_stock.locked) .metadata(rolling_stock.metadata) - .name(rolling_stock.common.name) - .effort_curves(rolling_stock.common.effort_curves) - .base_power_class(rolling_stock.common.base_power_class) - .length(rolling_stock.common.length) - .max_speed(rolling_stock.common.max_speed) - .startup_time(rolling_stock.common.startup_time) - .startup_acceleration(rolling_stock.common.startup_acceleration) - .comfort_acceleration(rolling_stock.common.comfort_acceleration) - .gamma(rolling_stock.common.gamma) - .inertia_coefficient(rolling_stock.common.inertia_coefficient) - .mass(rolling_stock.common.mass) - .rolling_resistance(rolling_stock.common.rolling_resistance) - .loading_gauge(rolling_stock.common.loading_gauge) - .power_restrictions(rolling_stock.common.power_restrictions) - .energy_sources(rolling_stock.common.energy_sources) - .electrical_power_startup_time(rolling_stock.common.electrical_power_startup_time) - .raise_pantograph_time(rolling_stock.common.raise_pantograph_time) - .supported_signaling_systems(rolling_stock.common.supported_signaling_systems) + .name(rolling_stock.name) + .effort_curves(rolling_stock.effort_curves) + .base_power_class(rolling_stock.base_power_class) + .length(rolling_stock.length) + .max_speed(rolling_stock.max_speed) + .startup_time(rolling_stock.startup_time) + .startup_acceleration(rolling_stock.startup_acceleration) + .comfort_acceleration(rolling_stock.comfort_acceleration) + .gamma(rolling_stock.gamma) + .inertia_coefficient(rolling_stock.inertia_coefficient) + .mass(rolling_stock.mass) + .rolling_resistance(rolling_stock.rolling_resistance) + .loading_gauge(rolling_stock.loading_gauge) + .power_restrictions(rolling_stock.power_restrictions) + .energy_sources(rolling_stock.energy_sources) + .electrical_power_startup_time(rolling_stock.electrical_power_startup_time) + .raise_pantograph_time(rolling_stock.raise_pantograph_time) + .supported_signaling_systems(rolling_stock.supported_signaling_systems) } } @@ -85,8 +115,8 @@ fn validate_rolling_stock_form( rolling_stock_form: &RollingStockForm, ) -> std::result::Result<(), ValidationError> { validate_rolling_stock( - &rolling_stock_form.common.effort_curves, - rolling_stock_form.common.electrical_power_startup_time, - rolling_stock_form.common.raise_pantograph_time, + &rolling_stock_form.effort_curves, + rolling_stock_form.electrical_power_startup_time, + rolling_stock_form.raise_pantograph_time, ) }