use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::consumer::common::SyncServerToClientResponse;
use super::{chunk::ChunkId, submission::SubmissionId};
#[cfg_attr(feature = "server-logic", derive(Error, Debug))]
#[cfg_attr(feature = "server-logic", error("Low-level database error: {0:?}"))]
#[cfg(feature = "server-logic")]
pub struct DatabaseError(#[from] pub sqlx::Error);
#[cfg(feature = "server-logic")]
impl<T> From<DatabaseError> for E<DatabaseError, T> {
fn from(e: DatabaseError) -> Self {
E::L(e)
}
}
#[derive(Error, Debug)]
#[error("Chunk not found for ID {0:?}")]
pub struct ChunkNotFound(pub ChunkId);
#[derive(Error, Debug)]
#[error("Submission not found for ID {0:?}")]
pub struct SubmissionNotFound(pub SubmissionId);
#[derive(Error, Debug)]
#[error("Unexpected opsqueue consumer server response. This indicates an error inside Opsqueue itself: {0:?}")]
pub struct UnexpectedOpsqueueConsumerServerResponse(pub SyncServerToClientResponse);
#[derive(Error, Debug, Clone, Serialize, Deserialize)]
pub enum E<L, R> {
#[error(transparent)]
L(L),
#[error(transparent)]
R(R),
}
#[macro_export]
macro_rules! E {
($tl: ty, $tr: ty) => ($crate::common::errors::E<$tl, $tr>);
($h:ty, $($t:ty),+ $(,)?) => ($crate::common::errors::E<$h, $crate::E!($($t),+)>);
}
#[macro_export]
macro_rules! map_both {
($value:expr, $pattern:pat => $result:expr) => {
match $value {
$crate::common::errors::E::L($pattern) => $crate::common::errors::E::L($result),
$crate::common::errors::E::R($pattern) => $crate::common::errors::E::R($result),
}
};
}
#[macro_export]
macro_rules! fold_both {
($value:expr, $pattern:pat => $result:expr) => {
match $value {
$crate::common::errors::E::L($pattern) => $result,
$crate::common::errors::E::R($pattern) => $result,
}
};
}
#[cfg(feature = "server-logic")]
impl<R> From<sqlx::Error> for E<DatabaseError, R> {
fn from(value: sqlx::Error) -> Self {
E::L(DatabaseError::from(value))
}
}
impl<L, R1, R2> From<E<R1, R2>> for E<L, E<R1, R2>> {
fn from(value: E<R1, R2>) -> Self {
E::R(value)
}
}
#[derive(Error, Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[error("You are using Opsqueue incorrectly. Details: {0}")]
pub struct IncorrectUsage<E>(#[from] pub E);
#[derive(Error, Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[error("You passed a 0 as reservation maximum limit. Please provide a positive integer")]
pub struct LimitIsZero();
#[derive(thiserror::Error, Debug, Copy, Clone, PartialEq, Eq)]
#[error("out of range integral type conversion attempted")]
pub struct TryFromIntError(pub(crate) ());