1#[derive(Clone, Debug)]
2pub struct Error {
3 kind: ErrorKind,
4 context: String,
5 source: Option<std::sync::Arc<dyn std::error::Error + Send + Sync + 'static>>,
6}
7
8impl Error {
9 pub fn new(kind: ErrorKind, context: impl std::fmt::Display) -> Self {
10 Self {
11 kind,
12 context: context.to_string(),
13 source: None,
14 }
15 }
16
17 pub fn set_source(mut self, source: impl std::error::Error + Send + Sync + 'static) -> Self {
18 self.source = Some(std::sync::Arc::new(source));
19 self
20 }
21
22 pub fn kind(&self) -> ErrorKind {
23 self.kind
24 }
25}
26
27impl std::fmt::Display for Error {
28 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29 write!(f, "{}", self.context)
30 }
31}
32
33impl std::error::Error for Error {
34 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
35 self.source
36 .as_ref()
37 .map(|s| s as &(dyn std::error::Error + 'static))
38 }
39}
40
41#[derive(Copy, Clone, Debug, PartialEq, Eq)]
42#[non_exhaustive]
43pub enum ErrorKind {
44 ApiParse,
45 Unknown,
46}