Skip to main content

erigon_seg/
error.rs

1//! Error and result types for the crate.
2
3use std::fmt;
4use std::path::Path;
5
6/// The crate result type.
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// Anything that can go wrong while opening or reading a seg file set.
10#[derive(Debug)]
11#[non_exhaustive]
12pub enum Error {
13    /// An underlying I/O failure (open, stat, mmap), annotated with the path.
14    Io {
15        /// The file the operation was acting on.
16        path: Box<Path>,
17        /// The originating I/O error.
18        source: std::io::Error,
19    },
20    /// The file exists and was mapped, but its contents are not a valid / supported
21    /// encoding of the format we were trying to read.
22    Format(String),
23}
24
25impl Error {
26    /// Build a [`Error::Format`] from anything string-like.
27    pub(crate) fn format(msg: impl Into<String>) -> Error {
28        Error::Format(msg.into())
29    }
30
31    pub(crate) fn io(path: &Path, source: std::io::Error) -> Error {
32        Error::Io {
33            path: path.into(),
34            source,
35        }
36    }
37}
38
39impl fmt::Display for Error {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            Error::Io { path, source } => write!(f, "{}: {source}", path.display()),
43            Error::Format(msg) => f.write_str(msg),
44        }
45    }
46}
47
48impl std::error::Error for Error {
49    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
50        match self {
51            Error::Io { source, .. } => Some(source),
52            Error::Format(_) => None,
53        }
54    }
55}