diff --git a/editoast/src/core/stdcm.rs b/editoast/src/core/stdcm.rs index 869aba5de56..22085148973 100644 --- a/editoast/src/core/stdcm.rs +++ b/editoast/src/core/stdcm.rs @@ -11,15 +11,12 @@ use serde::Deserialize; use serde::Serialize; use utoipa::ToSchema; -use super::conflict_detection::Conflict; use super::conflict_detection::TrainRequirements; -use super::pathfinding::PathfindingResult; use super::pathfinding::PathfindingResultSuccess; use super::pathfinding::TrackRange; use super::simulation::PhysicsRollingStock; use super::simulation::SimulationResponse; use crate::core::{AsCoreRequest, Json}; -use crate::views::path::pathfinding::PathfindingResult; #[derive(Debug, Serialize)] pub struct STDCMRequest { @@ -116,28 +113,24 @@ pub struct UndirectedTrackRange { pub end: u64, } -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, ToSchema)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] #[serde(tag = "status", rename_all = "snake_case")] // We accepted the difference of memory size taken by variants // Since there is only on success and others are error cases #[allow(clippy::large_enum_variant)] -pub enum STDCMResponse { +pub enum STDCMCoreResponse { Success { simulation: SimulationResponse, path: PathfindingResultSuccess, departure_time: DateTime, }, PathNotFound, - Conflicts { - pathfinding_result: PathfindingResult, - conflicts: Vec, - }, PreprocessingSimulationError { error: SimulationResponse, }, } -impl AsCoreRequest> for STDCMRequest { +impl AsCoreRequest> for STDCMRequest { const METHOD: reqwest::Method = reqwest::Method::POST; const URL_PATH: &'static str = "/v2/stdcm"; diff --git a/editoast/src/views/timetable/path_not_found_handler.rs b/editoast/src/views/timetable/path_not_found_handler.rs index f3e2654b0b0..6c3a9535ff8 100644 --- a/editoast/src/views/timetable/path_not_found_handler.rs +++ b/editoast/src/views/timetable/path_not_found_handler.rs @@ -5,14 +5,14 @@ use chrono::Utc; use crate::core::conflict_detection::ConflictDetectionRequest; use crate::core::conflict_detection::WorkSchedulesRequest; -use crate::core::pathfinding::PathfindingResult; use crate::core::simulation::SimulationResponse; -use crate::core::stdcm::STDCMResponse; use crate::core::AsCoreRequest; use crate::core::CoreClient; use crate::error::Result; use crate::models::train_schedule::TrainSchedule; use crate::models::work_schedules::WorkSchedule; +use crate::views::path::pathfinding::PathfindingResult; +use crate::views::timetable::stdcm::STDCMResponse; use super::filter_core_work_schedule; use super::stdcm::build_train_requirements; diff --git a/editoast/src/views/timetable/stdcm.rs b/editoast/src/views/timetable/stdcm.rs index f67bd4a0b30..97a8cce2c10 100644 --- a/editoast/src/views/timetable/stdcm.rs +++ b/editoast/src/views/timetable/stdcm.rs @@ -28,14 +28,16 @@ use super::path_not_found_handler::PathNotFoundHandler; use super::stdcm_request_payload::convert_steps; use super::stdcm_request_payload::STDCMRequestPayload; use super::SelectionSettings; +use crate::core::conflict_detection::Conflict; use crate::core::conflict_detection::TrainRequirements; use crate::core::pathfinding::InvalidPathItem; use crate::core::pathfinding::PathfindingInputError; +use crate::core::pathfinding::PathfindingResultSuccess; use crate::core::simulation::PhysicsRollingStock; use crate::core::simulation::{RoutingRequirement, SimulationResponse, SpacingRequirement}; +use crate::core::stdcm::STDCMCoreResponse; use crate::core::stdcm::STDCMPathItem; use crate::core::stdcm::STDCMRequest; -use crate::core::stdcm::STDCMResponse; use crate::core::stdcm::STDCMStepTimingData; use crate::core::AsCoreRequest; use crate::core::CoreClient; @@ -62,6 +64,26 @@ crate::routes! { "/stdcm" => stdcm, } +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, ToSchema)] +#[serde(tag = "status", rename_all = "snake_case")] +// We accepted the difference of memory size taken by variants +// Since there is only on success and others are error cases +#[allow(clippy::large_enum_variant)] +pub enum STDCMResponse { + Success { + simulation: SimulationResponse, + path: PathfindingResultSuccess, + departure_time: DateTime, + }, + Conflicts { + pathfinding_result: PathfindingResult, + conflicts: Vec, + }, + PreprocessingSimulationError { + error: SimulationResponse, + }, +} + #[derive(Debug, Error, EditoastError, Serialize)] #[editoast_error(base_id = "stdcm_v2")] enum STDCMError { @@ -247,28 +269,39 @@ async fn stdcm( let stdcm_response = stdcm_request.fetch(core_client.as_ref()).await?; - // 8. Handle PathNotFound response of STDCM - if let STDCMResponse::PathNotFound = stdcm_response { - let path_not_found = PathNotFoundHandler { - core_client, - infra_id, - infra_version: infra.version, - train_schedules, - simulations, - work_schedules, - virtual_train_schedule, - virtual_train_sim_result, - virtual_train_pathfinding_result, - earliest_departure_time, - maximum_run_time, - latest_simulation_end, - }; - let stdcm_response = path_not_found.handle().await?; - - return Ok(Json(stdcm_response)); + // 8. Handle STDCM Core Response + match stdcm_response { + STDCMCoreResponse::Success { + simulation, + path, + departure_time, + } => Ok(Json(STDCMResponse::Success { + simulation, + path, + departure_time, + })), + STDCMCoreResponse::PreprocessingSimulationError { error } => { + Ok(Json(STDCMResponse::PreprocessingSimulationError { error })) + } + STDCMCoreResponse::PathNotFound => { + let path_not_found = PathNotFoundHandler { + core_client, + infra_id, + infra_version: infra.version, + train_schedules, + simulations, + work_schedules, + virtual_train_schedule, + virtual_train_sim_result, + virtual_train_pathfinding_result, + earliest_departure_time, + maximum_run_time, + latest_simulation_end, + }; + let stdcm_response = path_not_found.handle().await?; + Ok(Json(stdcm_response)) + } } - - Ok(Json(stdcm_response)) } /// Build the list of scheduled train requirements, only including requirements @@ -520,7 +553,6 @@ mod tests { use crate::core::simulation::SimulationParameters; use crate::core::simulation::SimulationResponse; use crate::core::simulation::SpeedLimitProperties; - use crate::core::stdcm::STDCMResponse; use crate::models::fixtures::create_fast_rolling_stock; use crate::models::fixtures::create_small_infra; use crate::models::fixtures::create_timetable;