@jpgilldev / steid

2.7 KBRaw
1use crate::domain::{DomainError, repository::RepositoryError};
2
3use super::port::{GitProtocolError, GitQueryError, GitStorageError, PasswordError};
4
5/// What a use case can fail with.
6///
7/// Keeps the failure sources distinct: a broken rule, broken storage, a broken hash,
8/// and a filesystem that would not cooperate are different problems with different
9/// responses, and flattening them makes a storage outage indistinguishable from a
10/// validation error.
11#[derive(Debug)]
12pub enum Error {
13 /// A domain rule was violated.
14 Domain(DomainError),
15 /// Persistence failed.
16 Repository(RepositoryError),
17 /// Hashing or verification failed for a reason other than a wrong password.
18 Password(PasswordError),
19 /// A bare repository could not be created or removed on disk.
20 GitStorage(GitStorageError),
21 /// The git protocol could not be served.
22 GitProtocol(GitProtocolError),
23 /// A repository's contents could not be read.
24 GitQuery(GitQueryError),
25}
26
27impl From<DomainError> for Error {
28 fn from(error: DomainError) -> Self {
29 Self::Domain(error)
30 }
31}
32
33impl From<RepositoryError> for Error {
34 fn from(error: RepositoryError) -> Self {
35 Self::Repository(error)
36 }
37}
38
39impl From<PasswordError> for Error {
40 fn from(error: PasswordError) -> Self {
41 Self::Password(error)
42 }
43}
44
45impl From<GitStorageError> for Error {
46 fn from(error: GitStorageError) -> Self {
47 Self::GitStorage(error)
48 }
49}
50
51impl From<GitProtocolError> for Error {
52 fn from(error: GitProtocolError) -> Self {
53 Self::GitProtocol(error)
54 }
55}
56
57impl From<GitQueryError> for Error {
58 fn from(error: GitQueryError) -> Self {
59 Self::GitQuery(error)
60 }
61}
62
63impl std::fmt::Display for Error {
64 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65 match self {
66 Self::Domain(error) => write!(f, "{error}"),
67 Self::Repository(error) => write!(f, "{error}"),
68 Self::Password(error) => write!(f, "{error}"),
69 Self::GitStorage(error) => write!(f, "{error}"),
70 Self::GitProtocol(error) => write!(f, "{error}"),
71 Self::GitQuery(error) => write!(f, "{error}"),
72 }
73 }
74}
75
76impl std::error::Error for Error {
77 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
78 match self {
79 Self::Domain(error) => Some(error),
80 Self::Repository(error) => Some(error),
81 Self::Password(error) => Some(error),
82 Self::GitStorage(error) => Some(error),
83 Self::GitProtocol(error) => Some(error),
84 Self::GitQuery(error) => Some(error),
85 }
86 }
87}
88
89pub type Result<T> = std::result::Result<T, Error>;