typed_container/
errors.rs

1use std::{error::Error, fmt::Display, sync::PoisonError};
2
3use smol_str::SmolStr;
4
5#[derive(Debug)]
6pub enum RegisterError {
7    LockPoisoned,
8    Duplicated(SmolStr),
9}
10
11impl Display for RegisterError {
12    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13        match self {
14            Self::Duplicated(type_name) => write!(f, "Item duplicated: {}", type_name),
15            Self::LockPoisoned => write!(f, "Lock poisoned"),
16        }
17    }
18}
19
20impl Error for RegisterError {}
21
22impl<T> From<PoisonError<T>> for RegisterError {
23    fn from(_: PoisonError<T>) -> Self {
24        Self::LockPoisoned
25    }
26}
27
28#[derive(Debug)]
29pub enum GetServiceError {
30    LockPoisoned,
31    NotFound(SmolStr),
32    FailDowncast(SmolStr),
33    FailConstruct(SmolStr),
34    FailRegister(RegisterError),
35}
36
37impl Display for GetServiceError {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        match self {
40            Self::NotFound(type_name) => write!(f, "Service not found: {}", type_name),
41            Self::LockPoisoned => write!(f, "Lock poisoned"),
42            Self::FailDowncast(type_name) => write!(f, "Failed to downcast: {}", type_name),
43            Self::FailConstruct(type_name) => write!(f, "Failed to construct: {}", type_name),
44            Self::FailRegister(e) => e.fmt(f),
45        }
46    }
47}
48
49impl Error for GetServiceError {}
50
51impl<T> From<PoisonError<T>> for GetServiceError {
52    fn from(_: PoisonError<T>) -> Self {
53        Self::LockPoisoned
54    }
55}
56
57impl From<RegisterError> for GetServiceError {
58    fn from(e: RegisterError) -> Self {
59        Self::FailRegister(e)
60    }
61}