Skip to content

Commit

Permalink
editoast: change Model traits CreateBatch,WithKey to use editoast_mod…
Browse files Browse the repository at this point in the history
…els::Error

Signed-off-by: Leo Valais <[email protected]>
  • Loading branch information
leovalais committed Dec 26, 2024
1 parent 4591a26 commit f4c7501
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl ToTokens for CreateBatchImpl {
>(
conn: &mut editoast_models::DbConnection,
values: I,
) -> crate::error::Result<C> {
) -> std::result::Result<C, editoast_models::model::Error> {
use crate::models::Model;
use #table_mod::dsl;
use std::ops::DerefMut;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl ToTokens for CreateBatchWithKeyImpl {
>(
conn: &mut editoast_models::DbConnection,
values: I,
) -> crate::error::Result<C> {
) -> std::result::Result<C, editoast_models::model::Error> {
use crate::models::Identifiable;
use crate::models::Model;
use std::ops::DerefMut;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,10 @@ impl crate::models::CreateBatch<DocumentChangeset> for Document {
async fn create_batch<
I: std::iter::IntoIterator<Item = DocumentChangeset> + Send + 'async_trait,
C: Default + std::iter::Extend<Self> + Send + std::fmt::Debug,
>(conn: &mut editoast_models::DbConnection, values: I) -> crate::error::Result<C> {
>(
conn: &mut editoast_models::DbConnection,
values: I,
) -> std::result::Result<C, editoast_models::model::Error> {
use crate::models::Model;
use editoast_models::tables::osrd_infra_document::dsl;
use std::ops::DerefMut;
Expand Down Expand Up @@ -653,7 +656,10 @@ impl crate::models::CreateBatchWithKey<DocumentChangeset, (String)> for Document
async fn create_batch_with_key<
I: std::iter::IntoIterator<Item = DocumentChangeset> + Send + 'async_trait,
C: Default + std::iter::Extend<((String), Self)> + Send + std::fmt::Debug,
>(conn: &mut editoast_models::DbConnection, values: I) -> crate::error::Result<C> {
>(
conn: &mut editoast_models::DbConnection,
values: I,
) -> std::result::Result<C, editoast_models::model::Error> {
use crate::models::Identifiable;
use crate::models::Model;
use std::ops::DerefMut;
Expand Down Expand Up @@ -697,7 +703,10 @@ impl crate::models::CreateBatchWithKey<DocumentChangeset, (i64)> for Document {
async fn create_batch_with_key<
I: std::iter::IntoIterator<Item = DocumentChangeset> + Send + 'async_trait,
C: Default + std::iter::Extend<((i64), Self)> + Send + std::fmt::Debug,
>(conn: &mut editoast_models::DbConnection, values: I) -> crate::error::Result<C> {
>(
conn: &mut editoast_models::DbConnection,
values: I,
) -> std::result::Result<C, editoast_models::model::Error> {
use crate::models::Identifiable;
use crate::models::Model;
use std::ops::DerefMut;
Expand Down
10 changes: 5 additions & 5 deletions editoast/src/models/prelude/create.rs
Original file line number Diff line number Diff line change
@@ -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
///
Expand All @@ -14,14 +14,14 @@ use crate::error::Result;
pub trait Create<Row: Send>: 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<Row, model::Error>;
async fn create(self, conn: &mut DbConnection) -> Result<Row, model::Error>;

/// Just like [Create::create] but discards the error if any and returns `Err(fail())` instead
async fn create_or_fail<E: From<model::Error>, F: FnOnce() -> E + Send>(
self,
conn: &'async_trait mut DbConnection,
fail: F,
) -> std::result::Result<Row, E> {
) -> Result<Row, E> {
match self.create(conn).await {
Ok(obj) => Ok(obj),
Err(_) => Err(fail()),
Expand Down Expand Up @@ -56,7 +56,7 @@ where
>(
conn: &mut DbConnection,
values: I,
) -> Result<C>;
) -> Result<C, model::Error>;
}

/// Describes how a [Model](super::Model) can be created in the database given a batch of its changesets
Expand All @@ -80,5 +80,5 @@ where
>(
conn: &mut DbConnection,
values: I,
) -> Result<C>;
) -> Result<C, model::Error>;
}

0 comments on commit f4c7501

Please sign in to comment.