-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
editoast: timetable and trainschedule CRUD
- Loading branch information
1 parent
9358bbd
commit 06cfde4
Showing
13 changed files
with
463 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,29 @@ | ||
use crate::schema::v2::trainschedulev2::{ | ||
Margins, PathItem, PowerRestrictions, ScheduleItem, TrainScheduleOptions, | ||
}; | ||
use crate::DieselJson; | ||
use chrono::NaiveDateTime; | ||
use diesel_async::RunQueryDsl; | ||
use editoast_derive::ModelV2; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Default, Clone, ModelV2)] | ||
#[derive(Debug, Default, Clone, ModelV2, Serialize, Deserialize)] | ||
#[model(table = crate::tables::trainschedulev2)] | ||
#[model(changeset(public))] | ||
pub struct TrainScheduleV2 { | ||
pub id: i64, | ||
pub train_name: String, | ||
pub labels: DieselJson<Vec<String>>, | ||
pub rolling_stock_id: String, | ||
pub labels: Vec<Option<String>>, | ||
pub rolling_stock_name: String, | ||
pub timetable_id: i64, | ||
pub departure_time: NaiveDateTime, | ||
pub scheduled_points: DieselJson<Vec<String>>, | ||
pub allowances: DieselJson<Vec<String>>, | ||
pub start_time: NaiveDateTime, | ||
pub schedule: DieselJson<ScheduleItem>, | ||
pub margins: DieselJson<Margins>, | ||
pub initial_speed: f64, | ||
pub comfort: String, | ||
pub path: DieselJson<Vec<String>>, | ||
pub tag_constrains: DieselJson<Vec<String>>, | ||
pub speed_limit_tags: Option<String>, | ||
pub power_restriction_ranges: Option<DieselJson<Vec<String>>>, | ||
pub options: Option<DieselJson<Vec<String>>>, | ||
pub path: DieselJson<Vec<PathItem>>, | ||
pub constraint_distribution: String, | ||
pub speed_limit_tags: Vec<Option<String>>, | ||
pub power_restrictions: DieselJson<PowerRestrictions>, | ||
pub train_schedule_options: DieselJson<TrainScheduleOptions>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod trainschedulev2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::str::FromStr; | ||
use time::Duration; | ||
|
||
use crate::schema::utils::NonBlankString; | ||
|
||
#[derive(Debug, Default, Clone, Serialize, Deserialize)] | ||
pub struct PowerRestrictions { | ||
from: NonBlankString, | ||
to: NonBlankString, | ||
value: String, | ||
} | ||
|
||
#[derive(Debug, Default, Clone, Serialize, Deserialize)] | ||
pub struct TrainScheduleOptions { | ||
use_electrical_profiles: bool, | ||
} | ||
|
||
#[derive(Debug, Default, Clone, Serialize, Deserialize)] | ||
pub struct ScheduleItem { | ||
at: NonBlankString, | ||
departure: Option<Duration>, | ||
stop_for: Option<Duration>, | ||
locked: bool, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[serde(untagged)] | ||
enum PathItemLocation { | ||
TrackOffset { | ||
track: NonBlankString, | ||
offset: f64, | ||
}, | ||
OperationalPointId { | ||
operational_point: NonBlankString, | ||
}, | ||
OperationalPointDescription { | ||
trigram: NonBlankString, | ||
secondary_code: Option<String>, | ||
}, | ||
OperationalPointUic { | ||
uic: NonBlankString, | ||
secondary_code: Option<String>, | ||
}, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct PathItem { | ||
id: NonBlankString, | ||
#[serde(flatten)] | ||
location: PathItemLocation, | ||
deleted: bool, | ||
} | ||
|
||
#[derive(Debug, Default, Clone, Serialize, Deserialize)] | ||
pub struct Margins { | ||
boudaries: Vec<NonBlankString>, | ||
values: Vec<MarginValue>, | ||
} | ||
|
||
#[derive(Debug, Copy, Clone, Default)] | ||
enum MarginValue { | ||
#[default] | ||
Zero, | ||
Percentage(f64), | ||
MinPerKm(f64), | ||
} | ||
|
||
impl<'de> Deserialize<'de> for MarginValue { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
let value = String::deserialize(deserializer)?; | ||
if value == "0" { | ||
return Ok(Self::Zero); | ||
} | ||
if value.ends_with('%') { | ||
let float_value: f64 = f64::from_str(&value[0..value.len() - 1]).map_err(|_| { | ||
serde::de::Error::invalid_value( | ||
serde::de::Unexpected::Str(&value), | ||
&"a valid float", | ||
) | ||
})?; | ||
return Ok(Self::Percentage(float_value)); | ||
} | ||
return Err(serde::de::Error::custom("Invalid margin value")); | ||
} | ||
} | ||
|
||
impl Serialize for MarginValue { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
match self { | ||
MarginValue::Zero => serializer.serialize_str("0"), | ||
MarginValue::Percentage(value) => serializer.serialize_str(&format!("{}%", value)), | ||
MarginValue::MinPerKm(value) => serializer.serialize_str(&value.to_string()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod scenariov2; | ||
pub mod timetablev2; | ||
pub mod trainschedulev2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.