Skip to main content

urma_io/
error.rs

1use std::fmt::{Display, Formatter};
2
3#[derive(Debug)]
4pub enum Error {
5    Io(std::io::Error),
6    Integer(std::num::TryFromIntError),
7    LimitOverflow,
8    PrivatePermissions,
9    TooLarge { limit: usize },
10    Persist(tempfile::PersistError),
11    Context { message: String, cause: Box<Error> },
12}
13
14impl Display for Error {
15    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
16        match self {
17            Self::Io(cause) => Display::fmt(cause, f),
18            Self::Integer(cause) => Display::fmt(cause, f),
19            Self::PrivatePermissions => f.write_str("file must have private permissions"),
20            Self::LimitOverflow => f.write_str("file read limit overflow"),
21            Self::TooLarge { limit } => write!(f, "file exceeds {limit} byte capacity"),
22            Self::Persist(cause) => Display::fmt(cause, f),
23            Self::Context { message, cause } => write!(f, "{message}: {cause}"),
24        }
25    }
26}
27impl std::error::Error for Error {
28    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
29        match self {
30            Self::Io(cause) => Some(cause),
31            Self::Integer(cause) => Some(cause),
32            Self::Persist(cause) => Some(cause),
33            Self::Context { cause, .. } => Some(cause),
34            Self::PrivatePermissions | Self::LimitOverflow | Self::TooLarge { .. } => None,
35        }
36    }
37}
38impl From<std::io::Error> for Error {
39    fn from(cause: std::io::Error) -> Self {
40        Self::Io(cause)
41    }
42}