Skip to main content

legit_hamming/
error.rs

1//! The crate's error type.
2
3use std::fmt;
4use std::io;
5use std::path::PathBuf;
6
7/// The result of a fallible operation in this crate.
8pub type Result<T, E = Error> = std::result::Result<T, E>;
9
10/// What went wrong. Every failure in this crate, bad input included, is one of these.
11#[derive(Debug)]
12#[non_exhaustive]
13pub enum Error {
14    /// A record whose id isn't above the id before it: records are kept in rising id order.
15    IdOrder {
16        /// The id before.
17        previous: u64,
18        /// The id given.
19        id: u64,
20    },
21    /// A record with an id pushed to a values-only set, or a bare value to a set of records.
22    IdPresence {
23        /// Whether the set holds ids.
24        set_has_ids: bool,
25    },
26    /// Columns for a set that aren't one per word of the value, or that differ in length from
27    /// each other or from the ids.
28    Columns {
29        /// The number of columns the value width needs.
30        words: usize,
31    },
32    /// A set grown past the 2³² − 1 records a position can address.
33    TooManyRecords,
34    /// A chunk count for multi-index hashing that doesn't split the value into equal chunks of 1,
35    /// 2, 4, 8 or 16 bits.
36    ChunkCount {
37        /// The count asked for.
38        chunks: u32,
39        /// The width of the values in bits.
40        bits: u32,
41    },
42    /// An instruction set this CPU or build doesn't have.
43    IsaUnavailable(&'static str),
44    /// A hex value of the wrong length or with a character that isn't a hex digit.
45    InvalidHex {
46        /// The number of hex digits the width needs.
47        expected: usize,
48    },
49    /// A multi-index plan forced on a [`Planned`](crate::Planned) set that has no tables.
50    NoTables,
51    /// A snapshot whose manifest, files or contents don't agree with each other or with the value
52    /// width asked for.
53    Snapshot {
54        /// The file at fault.
55        path: PathBuf,
56        /// What's wrong with it.
57        reason: String,
58    },
59    /// A snapshot part whose SHA-256 isn't the one its manifest records.
60    Checksum {
61        /// The part.
62        path: PathBuf,
63        /// The checksum the manifest records.
64        expected: String,
65        /// The checksum of the file.
66        actual: String,
67    },
68    /// Memory the allocator wouldn't give: room for a snapshot's records, or for multi-index
69    /// tables.
70    OutOfMemory {
71        /// What the memory was for.
72        what: &'static str,
73    },
74    /// A file that couldn't be read or written.
75    Io {
76        /// The file.
77        path: PathBuf,
78        /// The error from the operating system.
79        source: io::Error,
80    },
81}
82
83impl Error {
84    pub(crate) fn io(path: impl Into<PathBuf>, source: io::Error) -> Self {
85        Self::Io {
86            path: path.into(),
87            source,
88        }
89    }
90
91    pub(crate) fn snapshot(path: impl Into<PathBuf>, reason: impl Into<String>) -> Self {
92        Self::Snapshot {
93            path: path.into(),
94            reason: reason.into(),
95        }
96    }
97}
98
99impl fmt::Display for Error {
100    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101        match self {
102            Self::IdOrder { previous, id } => {
103                write!(
104                    f,
105                    "record id `{id}` isn't above the id before it, `{previous}`; records go in rising id order"
106                )
107            }
108            Self::IdPresence { set_has_ids: true } => f.write_str("this set holds records with ids; give each an id"),
109            Self::IdPresence { set_has_ids: false } => f.write_str("this set holds values only; give no ids"),
110            Self::Columns { words } => {
111                write!(
112                    f,
113                    "a set needs {words} value columns of one length, and ids of that length if any"
114                )
115            }
116            Self::TooManyRecords => f.write_str("a set holds at most 4,294,967,295 records"),
117            Self::ChunkCount { chunks, bits } => write!(
118                f,
119                "`{chunks}` chunks don't split {bits} bits into equal chunks of 1, 2, 4, 8 or 16 bits"
120            ),
121            Self::IsaUnavailable(name) => write!(f, "the `{name}` instructions aren't available on this CPU or build"),
122            Self::InvalidHex { expected } => write!(f, "a value needs exactly {expected} hex digits"),
123            Self::NoTables => f.write_str("there are no multi-index tables to probe; give them a larger memory budget"),
124            Self::Snapshot { path, reason } => write!(f, "snapshot file `{}`: {reason}", path.display()),
125            Self::Checksum { path, expected, actual } => write!(
126                f,
127                "snapshot part `{}` has SHA-256 `{actual}`, but the manifest records `{expected}`",
128                path.display()
129            ),
130            Self::OutOfMemory { what } => write!(f, "couldn't set aside the memory for {what}"),
131            Self::Io { path, source } => write!(f, "couldn't access `{}`: {source}", path.display()),
132        }
133    }
134}
135
136impl std::error::Error for Error {
137    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
138        match self {
139            Self::Io { source, .. } => Some(source),
140            _ => None,
141        }
142    }
143}