From f4c7501b8db75a9eda1d9c0598999ff6c2f0a2eb Mon Sep 17 00:00:00 2001 From: Leo Valais Date: Thu, 26 Dec 2024 17:34:57 +0100 Subject: [PATCH] editoast: change Model traits CreateBatch,WithKey to use editoast_models::Error Signed-off-by: Leo Valais --- .../src/model/codegen/create_batch_impl.rs | 2 +- .../model/codegen/create_batch_with_key_impl.rs | 2 +- ...itoast_derive__model__tests__construction.snap | 15 ++++++++++++--- editoast/src/models/prelude/create.rs | 10 +++++----- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/editoast/editoast_derive/src/model/codegen/create_batch_impl.rs b/editoast/editoast_derive/src/model/codegen/create_batch_impl.rs index d1684fb2a56..1fda7c9dcd8 100644 --- a/editoast/editoast_derive/src/model/codegen/create_batch_impl.rs +++ b/editoast/editoast_derive/src/model/codegen/create_batch_impl.rs @@ -58,7 +58,7 @@ impl ToTokens for CreateBatchImpl { >( conn: &mut editoast_models::DbConnection, values: I, - ) -> crate::error::Result { + ) -> std::result::Result { use crate::models::Model; use #table_mod::dsl; use std::ops::DerefMut; diff --git a/editoast/editoast_derive/src/model/codegen/create_batch_with_key_impl.rs b/editoast/editoast_derive/src/model/codegen/create_batch_with_key_impl.rs index 7d5f8141512..0e9f2bca3ef 100644 --- a/editoast/editoast_derive/src/model/codegen/create_batch_with_key_impl.rs +++ b/editoast/editoast_derive/src/model/codegen/create_batch_with_key_impl.rs @@ -69,7 +69,7 @@ impl ToTokens for CreateBatchWithKeyImpl { >( conn: &mut editoast_models::DbConnection, values: I, - ) -> crate::error::Result { + ) -> std::result::Result { use crate::models::Identifiable; use crate::models::Model; use std::ops::DerefMut; diff --git a/editoast/editoast_derive/src/snapshots/editoast_derive__model__tests__construction.snap b/editoast/editoast_derive/src/snapshots/editoast_derive__model__tests__construction.snap index 9ccf615cc7c..7b4eaabaaca 100644 --- a/editoast/editoast_derive/src/snapshots/editoast_derive__model__tests__construction.snap +++ b/editoast/editoast_derive/src/snapshots/editoast_derive__model__tests__construction.snap @@ -612,7 +612,10 @@ impl crate::models::CreateBatch for Document { async fn create_batch< I: std::iter::IntoIterator + Send + 'async_trait, C: Default + std::iter::Extend + Send + std::fmt::Debug, - >(conn: &mut editoast_models::DbConnection, values: I) -> crate::error::Result { + >( + conn: &mut editoast_models::DbConnection, + values: I, + ) -> std::result::Result { use crate::models::Model; use editoast_models::tables::osrd_infra_document::dsl; use std::ops::DerefMut; @@ -653,7 +656,10 @@ impl crate::models::CreateBatchWithKey for Document async fn create_batch_with_key< I: std::iter::IntoIterator + Send + 'async_trait, C: Default + std::iter::Extend<((String), Self)> + Send + std::fmt::Debug, - >(conn: &mut editoast_models::DbConnection, values: I) -> crate::error::Result { + >( + conn: &mut editoast_models::DbConnection, + values: I, + ) -> std::result::Result { use crate::models::Identifiable; use crate::models::Model; use std::ops::DerefMut; @@ -697,7 +703,10 @@ impl crate::models::CreateBatchWithKey for Document { async fn create_batch_with_key< I: std::iter::IntoIterator + Send + 'async_trait, C: Default + std::iter::Extend<((i64), Self)> + Send + std::fmt::Debug, - >(conn: &mut editoast_models::DbConnection, values: I) -> crate::error::Result { + >( + conn: &mut editoast_models::DbConnection, + values: I, + ) -> std::result::Result { use crate::models::Identifiable; use crate::models::Model; use std::ops::DerefMut; diff --git a/editoast/src/models/prelude/create.rs b/editoast/src/models/prelude/create.rs index e4509d4e6ae..0dea1e88750 100644 --- a/editoast/src/models/prelude/create.rs +++ b/editoast/src/models/prelude/create.rs @@ -1,10 +1,10 @@ use std::fmt::Debug; +use std::result::Result; use editoast_models::model; use editoast_models::DbConnection; use crate::error::EditoastError; -use crate::error::Result; /// Describes how a [Model](super::Model) can be created in the database /// @@ -14,14 +14,14 @@ use crate::error::Result; pub trait Create: Sized { /// Creates a new row in the database with the values of the changeset and /// returns the created model instance - async fn create(self, conn: &mut DbConnection) -> std::result::Result; + async fn create(self, conn: &mut DbConnection) -> Result; /// Just like [Create::create] but discards the error if any and returns `Err(fail())` instead async fn create_or_fail, F: FnOnce() -> E + Send>( self, conn: &'async_trait mut DbConnection, fail: F, - ) -> std::result::Result { + ) -> Result { match self.create(conn).await { Ok(obj) => Ok(obj), Err(_) => Err(fail()), @@ -56,7 +56,7 @@ where >( conn: &mut DbConnection, values: I, - ) -> Result; + ) -> Result; } /// Describes how a [Model](super::Model) can be created in the database given a batch of its changesets @@ -80,5 +80,5 @@ where >( conn: &mut DbConnection, values: I, - ) -> Result; + ) -> Result; }