use core::{error::Error, fmt};
use crate::ResourceId;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FetchError {
Unavailable,
BorrowConflict,
InvalidPointer,
Custom(u16),
}
impl fmt::Display for FetchError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unavailable => formatter.write_str("resource is unavailable"),
Self::BorrowConflict => formatter.write_str("resource borrow conflicts"),
Self::InvalidPointer => formatter.write_str("resource pointer is invalid"),
Self::Custom(code) => write!(formatter, "resource backend error {code}"),
}
}
}
impl Error for FetchError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParamError {
AccessConflict {
first: u8,
second: u8,
resource: ResourceId,
},
Fetch {
index: u8,
error: FetchError,
},
}
impl fmt::Display for ParamError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::AccessConflict {
first,
second,
resource,
} => write!(
formatter,
"parameters {first} and {second} conflict on resource {}",
resource.get()
),
Self::Fetch { index, error } => {
write!(formatter, "failed to fetch parameter {index}: {error}")
}
}
}
}
impl Error for ParamError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::AccessConflict { .. } => None,
Self::Fetch { error, .. } => Some(error),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DispatchStage {
Before,
Command,
After,
Response,
}
impl fmt::Display for DispatchStage {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Before => formatter.write_str("before"),
Self::Command => formatter.write_str("command"),
Self::After => formatter.write_str("after"),
Self::Response => formatter.write_str("response"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StageError<E> {
Param(ParamError),
User(E),
}
impl<E> fmt::Display for StageError<E>
where
E: fmt::Display,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Param(error) => write!(formatter, "parameter error: {error}"),
Self::User(error) => write!(formatter, "function failed: {error}"),
}
}
}
impl<E> Error for StageError<E>
where
E: Error + 'static,
{
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Param(error) => Some(error),
Self::User(error) => Some(error),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DispatchError<E> {
NotFound,
Param {
stage: DispatchStage,
error: ParamError,
},
Before(E),
Command(E),
After(E),
Response(E),
}
impl<E> DispatchError<E> {
pub(crate) fn at(stage: DispatchStage, error: StageError<E>) -> Self {
match error {
StageError::Param(error) => Self::Param { stage, error },
StageError::User(error) => match stage {
DispatchStage::Before => Self::Before(error),
DispatchStage::Command => Self::Command(error),
DispatchStage::After => Self::After(error),
DispatchStage::Response => Self::Response(error),
},
}
}
}
impl<E> fmt::Display for DispatchError<E>
where
E: fmt::Display,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotFound => formatter.write_str("no matching command"),
Self::Param { stage, error } => write!(formatter, "{stage} parameter error: {error}"),
Self::Before(error) => write!(formatter, "before hook failed: {error}"),
Self::Command(error) => write!(formatter, "command execution failed: {error}"),
Self::After(error) => write!(formatter, "after hook failed: {error}"),
Self::Response(error) => write!(formatter, "response handling failed: {error}"),
}
}
}
impl<E> Error for DispatchError<E>
where
E: Error + 'static,
{
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::NotFound => None,
Self::Param { error, .. } => Some(error),
Self::Before(error)
| Self::Command(error)
| Self::After(error)
| Self::Response(error) => Some(error),
}
}
}