Skip to main content

rfc9162_tiles/
error.rs

1//! The one error type of the crate.
2
3use std::fmt;
4
5/// Why a store operation, a tile read or a note check did not go through.
6#[derive(Debug)]
7pub enum Error {
8    /// The store could not be read or written.
9    Io(std::io::Error),
10    /// Bytes in the store do not agree with the tree they claim to be part of,
11    /// or are not in the shape the layout gives them.
12    Corrupt {
13        /// The store path the bytes came from.
14        path: String,
15        /// What did not agree.
16        reason: String,
17    },
18    /// Text (a note, a checkpoint, a key, a manifest) is not in its format.
19    Format(String),
20    /// Another writer holds the log; the path is the lock file.
21    Locked(String),
22    /// An entry longer than a bundle can length-prefix (65535 bytes).
23    EntryTooLong(usize),
24    /// A tile, hash or entry index that the tree of this size does not have.
25    OutOfRange(String),
26    /// A signature on a note did not check, or no trusted key signed it.
27    Signature(String),
28}
29
30impl fmt::Display for Error {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::Io(e) => write!(f, "store: {e}"),
34            Self::Corrupt { path, reason } => write!(f, "{path}: {reason}"),
35            Self::Format(what) => write!(f, "format: {what}"),
36            Self::Locked(path) => write!(f, "another writer holds {path}"),
37            Self::EntryTooLong(len) => write!(f, "entry of {len} bytes exceeds 65535"),
38            Self::OutOfRange(what) => write!(f, "out of range: {what}"),
39            Self::Signature(what) => write!(f, "signature: {what}"),
40        }
41    }
42}
43
44impl std::error::Error for Error {
45    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
46        match self {
47            Self::Io(e) => Some(e),
48            _ => None,
49        }
50    }
51}
52
53impl From<std::io::Error> for Error {
54    fn from(e: std::io::Error) -> Self {
55        Self::Io(e)
56    }
57}
58
59/// `Result` with this crate's [`Error`].
60pub type Result<T> = std::result::Result<T, Error>;