From 38c7da73fcdad04dc1ed88a96dac4668c26b2e01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristram=20Gr=C3=A4bener?= Date: Thu, 5 Dec 2024 09:18:19 +0100 Subject: [PATCH] editoast: example using model(uom_unit) for a Length MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tristram Gräbener --- editoast/src/models/rolling_stock_model.rs | 8 ++++---- editoast/src/views/path/pathfinding.rs | 3 ++- editoast/src/views/rolling_stock/form.rs | 5 ++--- editoast/src/views/rolling_stock/light.rs | 3 +-- editoast/src/views/timetable/stdcm.rs | 9 ++++++--- editoast/src/views/train_schedule/projection.rs | 3 ++- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/editoast/src/models/rolling_stock_model.rs b/editoast/src/models/rolling_stock_model.rs index a8a0db9c85b..bf070f0eff4 100644 --- a/editoast/src/models/rolling_stock_model.rs +++ b/editoast/src/models/rolling_stock_model.rs @@ -14,7 +14,6 @@ use power_restrictions::PowerRestriction; use serde::Deserialize; use serde::Serialize; use uom::si::acceleration::meter_per_second_squared; -use uom::si::length::meter; use uom::si::velocity::meter_per_second; use uom::si::f64::{Acceleration, Length, Velocity}; @@ -48,7 +47,8 @@ pub struct RollingStockModel { #[model(json)] #[schema(required)] pub metadata: Option, - pub length: f64, + #[model(uom_unit = "uom::si::length::meter")] + pub length: Length, pub max_speed: f64, pub startup_time: f64, pub startup_acceleration: f64, @@ -153,7 +153,7 @@ impl From for RollingStock { name: rolling_stock_model.name, effort_curves: rolling_stock_model.effort_curves, base_power_class: rolling_stock_model.base_power_class, - length: Length::new::(rolling_stock_model.length), + length: rolling_stock_model.length, max_speed: Velocity::new::(rolling_stock_model.max_speed), startup_time: rolling_stock_model.startup_time, startup_acceleration: Acceleration::new::( @@ -185,7 +185,7 @@ impl From for RollingStockModelChangeset { .name(rolling_stock.name) .effort_curves(rolling_stock.effort_curves) .base_power_class(rolling_stock.base_power_class) - .length(rolling_stock.length.get::()) + .length(rolling_stock.length) .max_speed(rolling_stock.max_speed.get::()) .startup_time(rolling_stock.startup_time) .startup_acceleration( diff --git a/editoast/src/views/path/pathfinding.rs b/editoast/src/views/path/pathfinding.rs index 220566072ef..ab6bb8b1194 100644 --- a/editoast/src/views/path/pathfinding.rs +++ b/editoast/src/views/path/pathfinding.rs @@ -16,6 +16,7 @@ use serde::Deserialize; use serde::Serialize; use tracing::debug; use tracing::info; +use uom::si::length::meter; use utoipa::ToSchema; use crate::core::pathfinding::PathfindingCoreResult; @@ -391,7 +392,7 @@ pub async fn pathfinding_from_train_batch( rolling_stock_supported_electrifications: rolling_stock.supported_electrification(), rolling_stock_supported_signaling_systems: rolling_stock.supported_signaling_systems.0, rolling_stock_maximum_speed: OrderedFloat(rolling_stock.max_speed), - rolling_stock_length: OrderedFloat(rolling_stock.length), + rolling_stock_length: OrderedFloat(rolling_stock.length.get::()), path_items: train_schedule .path .clone() diff --git a/editoast/src/views/rolling_stock/form.rs b/editoast/src/views/rolling_stock/form.rs index 8e369c17a8f..4cad866afaf 100644 --- a/editoast/src/views/rolling_stock/form.rs +++ b/editoast/src/views/rolling_stock/form.rs @@ -11,7 +11,6 @@ use serde::Deserialize; use serde::Serialize; use uom::si::acceleration::meter_per_second_squared; use uom::si::f64::{Acceleration, Length, Velocity}; -use uom::si::length::meter; use uom::si::velocity::meter_per_second; use utoipa::ToSchema; use validator::Validate; @@ -65,7 +64,7 @@ impl From for Changeset { .name(rolling_stock.name) .effort_curves(rolling_stock.effort_curves) .base_power_class(rolling_stock.base_power_class) - .length(rolling_stock.length.get::()) + .length(rolling_stock.length) .max_speed(rolling_stock.max_speed.get::()) .startup_time(rolling_stock.startup_time) .startup_acceleration( @@ -109,7 +108,7 @@ impl From for RollingStockForm { name: value.name, effort_curves: value.effort_curves, base_power_class: value.base_power_class, - length: Length::new::(value.length), + length: value.length, max_speed: Velocity::new::(value.max_speed), startup_time: value.startup_time, startup_acceleration: Acceleration::new::( diff --git a/editoast/src/views/rolling_stock/light.rs b/editoast/src/views/rolling_stock/light.rs index 3935d3eb126..5d4907cb3bf 100644 --- a/editoast/src/views/rolling_stock/light.rs +++ b/editoast/src/views/rolling_stock/light.rs @@ -19,7 +19,6 @@ use serde::Serialize; use std::collections::HashMap; use uom::si::acceleration::meter_per_second_squared; use uom::si::f64::{Acceleration, Length, Velocity}; -use uom::si::length::meter; use uom::si::velocity::meter_per_second; use utoipa::ToSchema; @@ -267,7 +266,7 @@ impl From for LightRollingStock { locked, effort_curves: effort_curves.into(), base_power_class, - length: Length::new::(length), + length, max_speed: Velocity::new::(max_speed), startup_time, startup_acceleration: Acceleration::new::( diff --git a/editoast/src/views/timetable/stdcm.rs b/editoast/src/views/timetable/stdcm.rs index 672c8a0eb4f..d7cdd10056e 100644 --- a/editoast/src/views/timetable/stdcm.rs +++ b/editoast/src/views/timetable/stdcm.rs @@ -459,8 +459,8 @@ mod tests { use serde_json::json; use std::str::FromStr; use uom::si::acceleration::meter_per_second_squared; - use uom::si::length::meter; - use uom::si::length::millimeter; + use uom::si::f64::{Acceleration, Length, Velocity}; + use uom::si::length::{meter, millimeter}; use uom::si::velocity::meter_per_second; use uuid::Uuid; @@ -541,7 +541,10 @@ mod tests { let physics_consist: PhysicsConsist = simulation_parameters.into(); assert_eq!(physics_consist.mass, 123_u64); - assert_eq!(physics_consist.length, Length::new::(455000.0)); // It should be converted in mm + assert_eq!( + physics_consist.length, + uom::si::f64::Length::new::(455000.0) + ); // It should be converted in mm assert_eq!( physics_consist.max_speed, Velocity::new::(10_f64) diff --git a/editoast/src/views/train_schedule/projection.rs b/editoast/src/views/train_schedule/projection.rs index 2a833c28941..8a57369a818 100644 --- a/editoast/src/views/train_schedule/projection.rs +++ b/editoast/src/views/train_schedule/projection.rs @@ -16,6 +16,7 @@ use std::hash::Hash; use std::hash::Hasher; use std::sync::Arc; use tracing::info; +use uom::si::length::millimeter; use utoipa::ToSchema; use super::TrainScheduleError; @@ -320,7 +321,7 @@ async fn project_path( id, ProjectPathTrainResult { departure_time: train.start_time, - rolling_stock_length: (length * 1000.) as u64, + rolling_stock_length: length.get::() as u64, cached, }, );