diff --git a/editoast/src/views/v2/path/path_item_cache.rs b/editoast/src/views/v2/path/path_item_cache.rs index 354f89048bd..95001ff9f2f 100644 --- a/editoast/src/views/v2/path/path_item_cache.rs +++ b/editoast/src/views/v2/path/path_item_cache.rs @@ -11,7 +11,7 @@ use editoast_schemas::train_schedule::PathItemLocation; use crate::modelsv2::OperationalPointModel; -type TrackOffsetResult = std::result::Result>, TrackOffsetExtractionError>; +type TrackOffsetResult = std::result::Result>, PathfindingResult>; /// Gather information about several path items, factorizing db calls. pub struct PathItemCache { @@ -96,7 +96,7 @@ impl PathItemCache { match self.get_from_id(&operational_point.0) { Some(op) => op.track_offset(), None => { - return Err(TrackOffsetExtractionError { + return Err(PathfindingResult::InvalidPathItem { index, path_item: path_item.clone(), }); @@ -113,7 +113,7 @@ impl PathItemCache { .unwrap_or_default(); let ops = secondary_code_filter(secondary_code, ops); if ops.is_empty() { - return Err(TrackOffsetExtractionError { + return Err(PathfindingResult::InvalidPathItem { index, path_item: path_item.clone(), }); @@ -130,7 +130,7 @@ impl PathItemCache { .unwrap_or_default(); let ops = secondary_code_filter(secondary_code, ops); if ops.is_empty() { - return Err(TrackOffsetExtractionError { + return Err(PathfindingResult::InvalidPathItem { index, path_item: path_item.clone(), }); @@ -138,6 +138,17 @@ impl PathItemCache { track_offsets_from_ops(&ops) } }; + + // Check if tracks exist + for track_offset in &track_offsets { + if !self.track_exists(&track_offset.track.0) { + return Err(PathfindingResult::InvalidPathItem { + index, + path_item: path_item.clone(), + }); + } + } + result.push(track_offsets); } Ok(result) @@ -243,20 +254,3 @@ fn secondary_code_filter( ops } } - -/// Error when extracting track offsets from path items -pub struct TrackOffsetExtractionError { - /// The index of the path item that caused the error - pub index: usize, - /// The path item that caused the error - pub path_item: PathItemLocation, -} - -impl From for PathfindingResult { - fn from(error: TrackOffsetExtractionError) -> Self { - PathfindingResult::InvalidPathItem { - index: error.index, - path_item: error.path_item, - } - } -} diff --git a/editoast/src/views/v2/path/pathfinding.rs b/editoast/src/views/v2/path/pathfinding.rs index d0ba54e839e..e3ee78fd1ee 100644 --- a/editoast/src/views/v2/path/pathfinding.rs +++ b/editoast/src/views/v2/path/pathfinding.rs @@ -198,15 +198,6 @@ fn build_pathfinding_request( } let track_offsets = path_item_cache.extract_location_from_path_items(&path_items)?; - // Check if tracks exist - for tracks in &track_offsets { - for track_offset in tracks { - if !path_item_cache.track_exists(&track_offset.track.0) { - return Err(PathfindingResult::NotFoundInTracks); - } - } - } - // Create the pathfinding request Ok(PathfindingRequest { infra: infra.id, diff --git a/editoast/src/views/v2/timetable/stdcm.rs b/editoast/src/views/v2/timetable/stdcm.rs index 770ac11c5ee..cfbe6b5b4e1 100644 --- a/editoast/src/views/v2/timetable/stdcm.rs +++ b/editoast/src/views/v2/timetable/stdcm.rs @@ -19,6 +19,7 @@ use thiserror::Error; use utoipa::IntoParams; use utoipa::ToSchema; +use crate::core::v2::pathfinding::PathfindingResult; use crate::core::v2::simulation::SimulationResponse; use crate::core::v2::stdcm::STDCMResponse; use crate::core::v2::stdcm::TrainRequirement; @@ -33,7 +34,6 @@ use crate::modelsv2::work_schedules::WorkSchedule; use crate::modelsv2::RollingStockModel; use crate::modelsv2::{Infra, List}; use crate::views::v2::path::path_item_cache::PathItemCache; -use crate::views::v2::path::path_item_cache::TrackOffsetExtractionError; use crate::views::v2::train_schedule::train_simulation; use crate::views::v2::train_schedule::train_simulation_batch; use crate::RedisClient; @@ -437,8 +437,14 @@ async fn parse_stdcm_steps( let locations: Vec<_> = data.steps.iter().map(|item| &item.location).collect(); let path_item_cache = PathItemCache::load(conn, infra.id, &locations).await?; - let track_offsets = path_item_cache.extract_location_from_path_items(&locations); - let track_offsets = track_offsets.map_err::(|err| err.into())?; + let track_offsets = path_item_cache + .extract_location_from_path_items(&locations) + .map_err(|path_res| match path_res { + PathfindingResult::InvalidPathItem { index, path_item } => { + STDCMError::InvalidPathItem { index, path_item } + } + _ => panic!("Unexpected pathfinding result"), + })?; Ok(track_offsets .iter() @@ -457,15 +463,6 @@ async fn parse_stdcm_steps( .collect()) } -impl From for STDCMError { - fn from(error: TrackOffsetExtractionError) -> Self { - STDCMError::InvalidPathItem { - index: error.index, - path_item: error.path_item, - } - } -} - enum MaxRunningTimeResult { MaxRunningTime { value: u64 }, Error { error: Box },