1e1ae56feat: identity domain model1mo | 1 | use std::fmt; |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 8 | pub enum DomainError { |
| 9 | |
| 10 | Validation { field: String, reason: String }, |
| 11 | |
| 12 | NotFound { entity: &'static str }, |
| 13 | |
| 14 | AlreadyExists { entity: &'static str }, |
| 15 | |
| 16 | InvalidCredentials, |
| 17 | } |
| 18 | |
| 19 | impl DomainError { |
| 20 | pub fn validation(field: impl Into<String>, reason: impl Into<String>) -> Self { |
| 21 | Self::Validation { |
| 22 | field: field.into(), |
| 23 | reason: reason.into(), |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl fmt::Display for DomainError { |
| 29 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 30 | match self { |
| 31 | Self::Validation { field, reason } => write!(f, "invalid {field}: {reason}"), |
| 32 | Self::NotFound { entity } => write!(f, "{entity} not found"), |
| 33 | Self::AlreadyExists { entity } => write!(f, "{entity} already exists"), |
| 34 | Self::InvalidCredentials => f.write_str("invalid credentials"), |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl std::error::Error for DomainError {} |