1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#![doc(html_root_url = "https://calypso-lang.github.io/rustdoc/calypso_error/index.html")]
#![warn(clippy::pedantic)]
use std::fmt::{Debug, Display};
use thiserror::Error;
pub use eyre;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum CalError {
#[error("i/o error")]
Io(#[from] std::io::Error),
#[error("utf-8 decoding error")]
FromUtf8(#[from] std::string::FromUtf8Error),
#[error("formatting error")]
Fmt(#[from] std::fmt::Error),
#[error(transparent)]
Other(#[from] eyre::Report),
}
impl CalError {
pub fn try_downcast<E>(self) -> Result<E, Self>
where
E: Display + Debug + Send + Sync + 'static,
{
if let CalError::Other(err) = self {
let x = err.downcast()?;
Ok(x)
} else {
Err(self)
}
}
#[must_use]
pub fn try_downcast_ref<E>(&self) -> Option<&E>
where
E: Display + Debug + Send + Sync + 'static,
{
if let CalError::Other(err) = self {
err.downcast_ref()
} else {
None
}
}
pub fn try_downcast_mut<E>(&mut self) -> Option<&mut E>
where
E: Display + Debug + Send + Sync + 'static,
{
if let CalError::Other(err) = self {
err.downcast_mut()
} else {
None
}
}
}
pub type CalResult<T> = Result<T, CalError>;