Skip to content

Commit 23c11f4

Browse files
committed
editoast: add STDCMCoreResponse
Signed-off-by: hamz2a <[email protected]>
1 parent d26633e commit 23c11f4

File tree

3 files changed

+60
-35
lines changed

3 files changed

+60
-35
lines changed

editoast/src/core/stdcm.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@ use serde::Deserialize;
1111
use serde::Serialize;
1212
use utoipa::ToSchema;
1313

14-
use super::conflict_detection::Conflict;
1514
use super::conflict_detection::TrainRequirements;
16-
use super::pathfinding::PathfindingResult;
1715
use super::pathfinding::PathfindingResultSuccess;
1816
use super::pathfinding::TrackRange;
1917
use super::simulation::PhysicsRollingStock;
2018
use super::simulation::SimulationResponse;
2119
use crate::core::{AsCoreRequest, Json};
22-
use crate::views::path::pathfinding::PathfindingResult;
2320

2421
#[derive(Debug, Serialize)]
2522
pub struct STDCMRequest {
@@ -116,28 +113,24 @@ pub struct UndirectedTrackRange {
116113
pub end: u64,
117114
}
118115

119-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, ToSchema)]
116+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
120117
#[serde(tag = "status", rename_all = "snake_case")]
121118
// We accepted the difference of memory size taken by variants
122119
// Since there is only on success and others are error cases
123120
#[allow(clippy::large_enum_variant)]
124-
pub enum STDCMResponse {
121+
pub enum STDCMCoreResponse {
125122
Success {
126123
simulation: SimulationResponse,
127124
path: PathfindingResultSuccess,
128125
departure_time: DateTime<Utc>,
129126
},
130127
PathNotFound,
131-
Conflicts {
132-
pathfinding_result: PathfindingResult,
133-
conflicts: Vec<Conflict>,
134-
},
135128
PreprocessingSimulationError {
136129
error: SimulationResponse,
137130
},
138131
}
139132

140-
impl AsCoreRequest<Json<STDCMResponse>> for STDCMRequest {
133+
impl AsCoreRequest<Json<STDCMCoreResponse>> for STDCMRequest {
141134
const METHOD: reqwest::Method = reqwest::Method::POST;
142135
const URL_PATH: &'static str = "/v2/stdcm";
143136

editoast/src/views/timetable/path_not_found_handler.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ use chrono::Utc;
55

66
use crate::core::conflict_detection::ConflictDetectionRequest;
77
use crate::core::conflict_detection::WorkSchedulesRequest;
8-
use crate::core::pathfinding::PathfindingResult;
98
use crate::core::simulation::SimulationResponse;
10-
use crate::core::stdcm::STDCMResponse;
119
use crate::core::AsCoreRequest;
1210
use crate::core::CoreClient;
1311
use crate::error::Result;
1412
use crate::models::train_schedule::TrainSchedule;
1513
use crate::models::work_schedules::WorkSchedule;
14+
use crate::views::path::pathfinding::PathfindingResult;
15+
use crate::views::timetable::stdcm::STDCMResponse;
1616

1717
use super::filter_core_work_schedule;
1818
use super::stdcm::build_train_requirements;

editoast/src/views/timetable/stdcm.rs

+55-23
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ use super::path_not_found_handler::PathNotFoundHandler;
2828
use super::stdcm_request_payload::convert_steps;
2929
use super::stdcm_request_payload::STDCMRequestPayload;
3030
use super::SelectionSettings;
31+
use crate::core::conflict_detection::Conflict;
3132
use crate::core::conflict_detection::TrainRequirements;
3233
use crate::core::pathfinding::InvalidPathItem;
3334
use crate::core::pathfinding::PathfindingInputError;
35+
use crate::core::pathfinding::PathfindingResultSuccess;
3436
use crate::core::simulation::PhysicsRollingStock;
3537
use crate::core::simulation::{RoutingRequirement, SimulationResponse, SpacingRequirement};
38+
use crate::core::stdcm::STDCMCoreResponse;
3639
use crate::core::stdcm::STDCMPathItem;
3740
use crate::core::stdcm::STDCMRequest;
38-
use crate::core::stdcm::STDCMResponse;
3941
use crate::core::stdcm::STDCMStepTimingData;
4042
use crate::core::AsCoreRequest;
4143
use crate::core::CoreClient;
@@ -62,6 +64,26 @@ crate::routes! {
6264
"/stdcm" => stdcm,
6365
}
6466

67+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, ToSchema)]
68+
#[serde(tag = "status", rename_all = "snake_case")]
69+
// We accepted the difference of memory size taken by variants
70+
// Since there is only on success and others are error cases
71+
#[allow(clippy::large_enum_variant)]
72+
pub enum STDCMResponse {
73+
Success {
74+
simulation: SimulationResponse,
75+
path: PathfindingResultSuccess,
76+
departure_time: DateTime<Utc>,
77+
},
78+
Conflicts {
79+
pathfinding_result: PathfindingResult,
80+
conflicts: Vec<Conflict>,
81+
},
82+
PreprocessingSimulationError {
83+
error: SimulationResponse,
84+
},
85+
}
86+
6587
#[derive(Debug, Error, EditoastError, Serialize)]
6688
#[editoast_error(base_id = "stdcm_v2")]
6789
enum STDCMError {
@@ -247,28 +269,39 @@ async fn stdcm(
247269

248270
let stdcm_response = stdcm_request.fetch(core_client.as_ref()).await?;
249271

250-
// 8. Handle PathNotFound response of STDCM
251-
if let STDCMResponse::PathNotFound = stdcm_response {
252-
let path_not_found = PathNotFoundHandler {
253-
core_client,
254-
infra_id,
255-
infra_version: infra.version,
256-
train_schedules,
257-
simulations,
258-
work_schedules,
259-
virtual_train_schedule,
260-
virtual_train_sim_result,
261-
virtual_train_pathfinding_result,
262-
earliest_departure_time,
263-
maximum_run_time,
264-
latest_simulation_end,
265-
};
266-
let stdcm_response = path_not_found.handle().await?;
267-
268-
return Ok(Json(stdcm_response));
272+
// 8. Handle STDCM Core Response
273+
match stdcm_response {
274+
STDCMCoreResponse::Success {
275+
simulation,
276+
path,
277+
departure_time,
278+
} => Ok(Json(STDCMResponse::Success {
279+
simulation,
280+
path,
281+
departure_time,
282+
})),
283+
STDCMCoreResponse::PreprocessingSimulationError { error } => {
284+
Ok(Json(STDCMResponse::PreprocessingSimulationError { error }))
285+
}
286+
STDCMCoreResponse::PathNotFound => {
287+
let path_not_found = PathNotFoundHandler {
288+
core_client,
289+
infra_id,
290+
infra_version: infra.version,
291+
train_schedules,
292+
simulations,
293+
work_schedules,
294+
virtual_train_schedule,
295+
virtual_train_sim_result,
296+
virtual_train_pathfinding_result,
297+
earliest_departure_time,
298+
maximum_run_time,
299+
latest_simulation_end,
300+
};
301+
let stdcm_response = path_not_found.handle().await?;
302+
Ok(Json(stdcm_response))
303+
}
269304
}
270-
271-
Ok(Json(stdcm_response))
272305
}
273306

274307
/// Build the list of scheduled train requirements, only including requirements
@@ -520,7 +553,6 @@ mod tests {
520553
use crate::core::simulation::SimulationParameters;
521554
use crate::core::simulation::SimulationResponse;
522555
use crate::core::simulation::SpeedLimitProperties;
523-
use crate::core::stdcm::STDCMResponse;
524556
use crate::models::fixtures::create_fast_rolling_stock;
525557
use crate::models::fixtures::create_small_infra;
526558
use crate::models::fixtures::create_timetable;

0 commit comments

Comments
 (0)