Skip to main content

gix_error/concrete/
classify.rs

1use std::borrow::Cow;
2use std::fmt::{Display, Formatter};
3
4use crate::Message;
5
6/// An error caused by malformed or internally inconsistent data.
7#[derive(Debug)]
8pub struct CorruptionError {
9    /// The error message.
10    pub message: Cow<'static, str>,
11}
12
13impl CorruptionError {
14    /// Create a new instance that displays the given `message`.
15    pub fn new(message: impl Into<Cow<'static, str>>) -> Self {
16        CorruptionError {
17            message: message.into(),
18        }
19    }
20}
21
22impl Display for CorruptionError {
23    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
24        f.write_str(self.message.as_ref())
25    }
26}
27
28impl std::error::Error for CorruptionError {}
29
30impl From<Message> for CorruptionError {
31    fn from(Message(msg): Message) -> Self {
32        CorruptionError::new(msg)
33    }
34}
35
36impl From<String> for CorruptionError {
37    fn from(msg: String) -> Self {
38        CorruptionError::new(msg)
39    }
40}
41
42impl From<&'static str> for CorruptionError {
43    fn from(msg: &'static str) -> Self {
44        CorruptionError::new(msg)
45    }
46}
47
48/// An error indicating that a requested resource does not exist.
49#[derive(Debug)]
50pub struct NotFoundError {
51    /// The error message.
52    pub message: Cow<'static, str>,
53}
54
55impl NotFoundError {
56    /// Create a new instance that displays the given `message`.
57    pub fn new(message: impl Into<Cow<'static, str>>) -> Self {
58        NotFoundError {
59            message: message.into(),
60        }
61    }
62}
63
64impl Display for NotFoundError {
65    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
66        f.write_str(self.message.as_ref())
67    }
68}
69
70impl std::error::Error for NotFoundError {}
71
72impl From<Message> for NotFoundError {
73    fn from(Message(msg): Message) -> Self {
74        NotFoundError::new(msg)
75    }
76}
77
78impl From<String> for NotFoundError {
79    fn from(msg: String) -> Self {
80        NotFoundError::new(msg)
81    }
82}
83
84impl From<&'static str> for NotFoundError {
85    fn from(msg: &'static str) -> Self {
86        NotFoundError::new(msg)
87    }
88}
89
90/// A transparent wrapper for dependency-specific errors known to be retryable.
91#[derive(Debug)]
92pub struct RetryableError(Box<dyn std::error::Error + Send + Sync + 'static>);
93
94impl RetryableError {
95    /// Mark `source` as retryable.
96    pub fn new(source: impl std::error::Error + Send + Sync + 'static) -> Self {
97        RetryableError(Box::new(source))
98    }
99}
100
101impl Display for RetryableError {
102    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
103        Display::fmt(&self.0, f)
104    }
105}
106
107impl std::error::Error for RetryableError {
108    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
109        Some(self.0.as_ref())
110    }
111}