1use std::fmt;
4use std::io;
5use std::path::PathBuf;
6
7pub type Result<T, E = Error> = std::result::Result<T, E>;
9
10#[derive(Debug)]
12#[non_exhaustive]
13pub enum Error {
14 IdOrder {
16 previous: u64,
18 id: u64,
20 },
21 IdPresence {
23 set_has_ids: bool,
25 },
26 Columns {
29 words: usize,
31 },
32 TooManyRecords,
34 ChunkCount {
37 chunks: u32,
39 bits: u32,
41 },
42 IsaUnavailable(&'static str),
44 InvalidHex {
46 expected: usize,
48 },
49 NoTables,
51 Snapshot {
54 path: PathBuf,
56 reason: String,
58 },
59 Checksum {
61 path: PathBuf,
63 expected: String,
65 actual: String,
67 },
68 OutOfMemory {
71 what: &'static str,
73 },
74 Io {
76 path: PathBuf,
78 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}