Skip to content

Commit

Permalink
editoast: add categories to rolling stock model
Browse files Browse the repository at this point in the history
Signed-off-by: hamz2a <[email protected]>
  • Loading branch information
hamz2a committed Feb 13, 2025
1 parent e08bd68 commit 519b22f
Show file tree
Hide file tree
Showing 29 changed files with 345 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class RJSRollingStock implements Identified {
public static final JsonAdapter<RJSRollingStock> adapter =
new Moshi.Builder().add(RJSRollingResistance.adapter).build().adapter(RJSRollingStock.class);

public static final transient String CURRENT_VERSION = "3.2";
public static final transient String CURRENT_VERSION = "3.3";

/** The version of the rolling stock format used */
@Json(name = "railjson_version")
Expand Down
5 changes: 5 additions & 0 deletions editoast/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion editoast/diesel.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

[print_schema]
file = "editoast_models/src/tables.rs"
generate_missing_sql_type_definitions = false
filter = { except_tables = ["spatial_ref_sys"] }
import_types = ["diesel::sql_types::*", "postgis_diesel::sql_types::*"]

Expand Down
5 changes: 5 additions & 0 deletions editoast/editoast_models/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ testing = []
[dependencies]
diesel.workspace = true
diesel-async.workspace = true
editoast_common.workspace = true
editoast_derive.workspace = true
editoast_schemas.workspace = true
futures.workspace = true
futures-util.workspace = true
openssl.workspace = true
opentelemetry-semantic-conventions.workspace = true
postgis_diesel.workspace = true
postgres-openssl.workspace = true
regex.workspace = true
serde.workspace = true
strum.workspace = true
thiserror.workspace = true
tokio.workspace = true
tokio-postgres.workspace = true
tracing.workspace = true
url.workspace = true
utoipa.workspace = true

[dev-dependencies]
# The feature 'testing' is needed for all of the doc-tests
Expand Down
1 change: 1 addition & 0 deletions editoast/editoast_models/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod db_connection_pool;
pub mod model;
pub mod rolling_stock;
pub mod tables;

pub use db_connection_pool::DbConnection;
Expand Down
3 changes: 3 additions & 0 deletions editoast/editoast_models/src/rolling_stock.rs
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;
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()
}
}
25 changes: 25 additions & 0 deletions editoast/editoast_models/src/tables.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// @generated automatically by Diesel CLI.

pub mod sql_types {
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "geometry"))]
pub struct Geometry;

#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "rolling_stock_category"))]
pub struct RollingStockCategory;
}

diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
Expand Down Expand Up @@ -102,6 +112,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_buffer_stop (id) {
id -> Int8,
Expand All @@ -115,6 +126,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_detector (id) {
id -> Int8,
Expand All @@ -128,6 +140,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_electrification (id) {
id -> Int8,
Expand All @@ -141,6 +154,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_error (id) {
id -> Int8,
Expand All @@ -155,6 +169,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_neutral_section (id) {
id -> Int8,
Expand All @@ -168,6 +183,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_neutral_sign (id) {
id -> Int8,
Expand All @@ -183,6 +199,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_operational_point (id) {
id -> Int8,
Expand All @@ -198,6 +215,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_psl_sign (id) {
id -> Int8,
Expand All @@ -213,6 +231,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_signal (id) {
id -> Int8,
Expand All @@ -231,6 +250,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_speed_section (id) {
id -> Int8,
Expand All @@ -244,6 +264,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_switch (id) {
id -> Int8,
Expand All @@ -257,6 +278,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::Geometry;

infra_layer_track_section (id) {
id -> Int8,
Expand Down Expand Up @@ -455,6 +477,7 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
use super::sql_types::RollingStockCategory;

rolling_stock (id) {
id -> Int8,
Expand Down Expand Up @@ -484,6 +507,8 @@ diesel::table! {
version -> Int8,
supported_signaling_systems -> Array<Nullable<Text>>,
etcs_brake_params -> Jsonb,
primary_category -> RollingStockCategory,
other_categories -> Array<Nullable<RollingStockCategory>>,
}
}

Expand Down
7 changes: 6 additions & 1 deletion editoast/editoast_schemas/src/rolling_stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub use rolling_stock_livery::RollingStockLiveryMetadata;
mod towed_rolling_stock;
pub use towed_rolling_stock::TowedRollingStock;

mod rolling_stock_category;
pub use rolling_stock_category::RollingStockCategories;
pub use rolling_stock_category::RollingStockCategory;

use editoast_common::units;
use editoast_common::units::quantities::{
Acceleration, Deceleration, Length, Mass, Ratio, Time, Velocity,
Expand All @@ -53,9 +57,10 @@ editoast_common::schemas! {
rolling_resistance::schemas(),
rolling_stock_livery::schemas(),
supported_signaling_systems::schemas(),
rolling_stock_category::schemas(),
}

pub const ROLLING_STOCK_RAILJSON_VERSION: &str = "3.2";
pub const ROLLING_STOCK_RAILJSON_VERSION: &str = "3.3";

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Self")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 = "kebab-case")]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum RollingStockCategory {
Unknown,
HighSpeedTrain,
IntercityTrain,
RegionalTrainMultipleUnit,
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()
}
}
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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE TYPE rolling_stock_category AS ENUM (
'unknown',
'high-speed-train',
'intercity-train',
'regional-train-multiple-unit',
'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 'unknown',
ADD COLUMN other_categories rolling_stock_category[] NOT NULL DEFAULT '{}';

UPDATE rolling_stock SET railjson_version = '3.3';
Loading

0 comments on commit 519b22f

Please sign in to comment.