-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
editoast: add categories to rolling stock model
Signed-off-by: hamz2a <[email protected]>
- Loading branch information
Showing
29 changed files
with
342 additions
and
30 deletions.
There are no files selected for viewing
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
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
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 @@ | ||
mod rolling_stock_category; | ||
pub use rolling_stock_category::RollingStockCategories; | ||
pub use rolling_stock_category::RollingStockCategory; |
48 changes: 48 additions & 0 deletions
48
editoast/editoast_models/src/rolling_stock/rolling_stock_category.rs
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,48 @@ | ||
use std::io::Write; | ||
use std::str::FromStr; | ||
|
||
use diesel::deserialize::FromSql; | ||
use diesel::deserialize::FromSqlRow; | ||
use diesel::expression::AsExpression; | ||
use diesel::pg::Pg; | ||
use diesel::pg::PgValue; | ||
use diesel::serialize::Output; | ||
use diesel::serialize::ToSql; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
|
||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, FromSqlRow, AsExpression)] | ||
#[diesel(sql_type = crate::tables::sql_types::RollingStockCategory)] | ||
pub struct RollingStockCategory(pub editoast_schemas::rolling_stock::RollingStockCategory); | ||
|
||
impl FromSql<crate::tables::sql_types::RollingStockCategory, Pg> for RollingStockCategory { | ||
fn from_sql(value: PgValue) -> diesel::deserialize::Result<Self> { | ||
let s = std::str::from_utf8(value.as_bytes()).map_err(|_| "Invalid UTF-8 data")?; | ||
editoast_schemas::rolling_stock::RollingStockCategory::from_str(s) | ||
.map(RollingStockCategory) | ||
.map_err(|_| "Unrecognized enum variant for RollingStockCategory".into()) | ||
} | ||
} | ||
|
||
impl ToSql<crate::tables::sql_types::RollingStockCategory, Pg> for RollingStockCategory { | ||
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> diesel::serialize::Result { | ||
let variant: &str = &self.0.to_string(); | ||
out.write_all(variant.as_bytes())?; | ||
Ok(diesel::serialize::IsNull::No) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] | ||
pub struct RollingStockCategories(pub Vec<RollingStockCategory>); | ||
|
||
impl From<Vec<Option<RollingStockCategory>>> for RollingStockCategories { | ||
fn from(categories: Vec<Option<RollingStockCategory>>) -> Self { | ||
Self(categories.into_iter().flatten().collect()) | ||
} | ||
} | ||
|
||
impl From<RollingStockCategories> for Vec<Option<RollingStockCategory>> { | ||
fn from(categories: RollingStockCategories) -> Self { | ||
categories.0.into_iter().map(Some).collect() | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
editoast/editoast_schemas/src/rolling_stock/rolling_stock_category.rs
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,47 @@ | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
use strum::Display; | ||
use strum::EnumString; | ||
use strum::IntoStaticStr; | ||
use utoipa::ToSchema; | ||
|
||
editoast_common::schemas! { | ||
RollingStockCategory, | ||
RollingStockCategories, | ||
} | ||
|
||
// This enum maps to a Postgres enum type, specifically `rolling_stock_category`. | ||
// Any changes made to this enum must be reflected in the corresponding Postgres enum, | ||
// and vice versa, to ensure consistency between the application and the database. | ||
#[derive( | ||
Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema, EnumString, IntoStaticStr, Display, | ||
)] | ||
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")] | ||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")] | ||
pub enum RollingStockCategory { | ||
HighSpeedTrain, | ||
IntercityTrain, | ||
RegionalTrain, | ||
NightTrain, | ||
CommuterTrain, | ||
FreightTrain, | ||
FastFreightTrain, | ||
TramTrain, | ||
TouristicTrain, | ||
WorkTrain, | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ToSchema)] | ||
pub struct RollingStockCategories(pub Vec<RollingStockCategory>); | ||
|
||
impl From<Vec<Option<RollingStockCategory>>> for RollingStockCategories { | ||
fn from(categories: Vec<Option<RollingStockCategory>>) -> Self { | ||
Self(categories.into_iter().flatten().collect()) | ||
} | ||
} | ||
|
||
impl From<RollingStockCategories> for Vec<Option<RollingStockCategory>> { | ||
fn from(categories: RollingStockCategories) -> Self { | ||
categories.0.into_iter().map(Some).collect() | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...t/migrations/2025-02-03-094532_add_primary_and_other_categories_to_rolling_stock/down.sql
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,7 @@ | ||
ALTER TABLE rolling_stock | ||
DROP COLUMN primary_category, | ||
DROP COLUMN other_categories; | ||
|
||
UPDATE rolling_stock SET railjson_version = '3.2'; | ||
|
||
DROP TYPE rolling_stock_category; |
18 changes: 18 additions & 0 deletions
18
...ast/migrations/2025-02-03-094532_add_primary_and_other_categories_to_rolling_stock/up.sql
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,18 @@ | ||
CREATE TYPE rolling_stock_category AS ENUM ( | ||
'HIGH_SPEED_TRAIN', | ||
'INTERCITY_TRAIN', | ||
'REGIONAL_TRAIN', | ||
'NIGHT_TRAIN', | ||
'COMMUTER_TRAIN', | ||
'FREIGHT_TRAIN', | ||
'FAST_FREIGHT_TRAIN', | ||
'TRAM_TRAIN', | ||
'TOURISTIC_TRAIN', | ||
'WORK_TRAIN' | ||
); | ||
|
||
ALTER TABLE rolling_stock | ||
ADD COLUMN primary_category rolling_stock_category NOT NULL DEFAULT 'FREIGHT_TRAIN', | ||
ADD COLUMN other_categories rolling_stock_category[] NOT NULL DEFAULT '{}'; | ||
|
||
UPDATE rolling_stock SET railjson_version = '3.3'; |
Oops, something went wrong.