legit_hashes/error.rs
1//! The crate's error type.
2
3use std::fmt;
4
5use crate::{Algorithm, SignatureAlgorithm};
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; nothing
11/// panics.
12#[derive(Debug, Clone, PartialEq, Eq)]
13#[non_exhaustive]
14pub enum Error {
15 /// An image or resize target with a width or height of 0.
16 ZeroDimension {
17 /// The width given.
18 width: u32,
19 /// The height given.
20 height: u32,
21 },
22 /// An image or resize target wider or taller than [`MAX_DIMENSION`](crate::MAX_DIMENSION), or
23 /// on a 32-bit target, one with more pixels than a buffer can hold.
24 DimensionTooLarge {
25 /// The width given.
26 width: u32,
27 /// The height given.
28 height: u32,
29 },
30 /// A pixel buffer whose length doesn't match its width and height.
31 BufferLength {
32 /// The length the width, height and channel count call for.
33 expected: usize,
34 /// The length given.
35 actual: usize,
36 },
37 /// Image hashing asked of an algorithm that hashes text.
38 NotAnImageAlgorithm(Algorithm),
39 /// Two hashes compared across algorithms or versions.
40 AlgorithmMismatch {
41 /// The algorithm of the hash compared from.
42 left: Algorithm,
43 /// The algorithm of the hash compared to.
44 right: Algorithm,
45 },
46 /// An algorithm id that this version of the crate doesn't know.
47 UnknownAlgorithm(String),
48 /// A hash string without the `:` between the algorithm id and the hex.
49 MissingSeparator,
50 /// Hex or bytes of the wrong length for the algorithm.
51 WrongLength {
52 /// The algorithm the value was read for.
53 algorithm: Algorithm,
54 /// The length the algorithm needs, in hex digits or bytes as the input is.
55 expected: usize,
56 /// The length given.
57 actual: usize,
58 },
59 /// A hex string with a character that isn't a hex digit.
60 InvalidHex,
61 /// An integer form asked of an algorithm whose hashes aren't 64 bits wide.
62 NoIntegerForm {
63 /// The algorithm of the hash.
64 algorithm: Algorithm,
65 },
66 /// A chunk count outside `1..=bits`, or one that leaves a chunk wider than 64 bits.
67 ChunkCount {
68 /// The count asked for.
69 count: u32,
70 /// The width of the hash in bits.
71 bits: u32,
72 },
73 /// A shingle size of 0.
74 ShingleSize,
75 /// The id of a signature algorithm, such as `minhash-v1`, read as a bit-hash algorithm.
76 NotABitHash(SignatureAlgorithm),
77 /// The id of a bit-hash algorithm, such as `simhash-v1`, read as a signature algorithm.
78 NotASignature(Algorithm),
79 /// Two signatures compared or combined across algorithms or versions.
80 SignatureMismatch {
81 /// The algorithm of the signature compared from.
82 left: SignatureAlgorithm,
83 /// The algorithm of the signature compared to.
84 right: SignatureAlgorithm,
85 },
86 /// Hex, bytes or values of the wrong length for the signature algorithm.
87 SignatureLength {
88 /// The algorithm the signature was read for.
89 algorithm: SignatureAlgorithm,
90 /// The length the algorithm needs, in hex digits, bytes or values as the input is.
91 expected: usize,
92 /// The length given.
93 actual: usize,
94 },
95 /// A band count that doesn't divide the signature's values into equal bands.
96 BandCount {
97 /// The count asked for.
98 bands: u32,
99 /// The number of values in the signature.
100 values: u32,
101 },
102 /// An encoded image that couldn't be decoded, or that is over the decoder limits.
103 #[cfg(feature = "image")]
104 Decode(String),
105}
106
107impl fmt::Display for Error {
108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109 match self {
110 Self::ZeroDimension { width, height } => {
111 write!(
112 f,
113 "`{width}x{height}` has a side of 0; width and height must be at least 1"
114 )
115 }
116 Self::DimensionTooLarge { width, height } => write!(
117 f,
118 "`{width}x{height}` is too large; width and height must be at most `{}`",
119 crate::MAX_DIMENSION
120 ),
121 Self::BufferLength { expected, actual } => {
122 write!(
123 f,
124 "the pixel buffer holds `{actual}` bytes; its size calls for `{expected}`"
125 )
126 }
127 Self::NotAnImageAlgorithm(algorithm) => write!(f, "`{algorithm}` hashes text, not images"),
128 Self::AlgorithmMismatch { left, right } => {
129 write!(
130 f,
131 "can't compare hashes of different algorithms, `{left}` and `{right}`"
132 )
133 }
134 Self::UnknownAlgorithm(id) => write!(f, "`{id}` isn't a known hash algorithm"),
135 Self::MissingSeparator => f.write_str("a hash is written `<algorithm>:<hex>`; the `:` is missing"),
136 Self::WrongLength { algorithm, actual, .. } => write!(
137 f,
138 "`{algorithm}` hashes are `{}` hex digits or `{}` bytes long, not `{actual}`",
139 algorithm.bits() / 4,
140 algorithm.bits() / 8
141 ),
142 Self::InvalidHex => f.write_str("the hash holds a character that isn't a hex digit"),
143 Self::NoIntegerForm { algorithm } => {
144 write!(
145 f,
146 "`{algorithm}` hashes aren't 64 bits wide, so they have no integer form"
147 )
148 }
149 Self::ChunkCount { count, bits } => write!(
150 f,
151 "can't split `{bits}` bits into `{count}` chunks; use {} to `{bits}`",
152 bits.div_ceil(64)
153 ),
154 Self::ShingleSize => f.write_str("a shingle must be at least 1 character long"),
155 Self::NotABitHash(algorithm) => {
156 write!(
157 f,
158 "`{algorithm}` makes signatures, not bit hashes; read it as a `Signature`"
159 )
160 }
161 Self::NotASignature(algorithm) => {
162 write!(f, "`{algorithm}` makes bit hashes, not signatures; read it as a `Hash`")
163 }
164 Self::SignatureMismatch { left, right } => write!(
165 f,
166 "can't compare signatures of different algorithms, `{left}` and `{right}`"
167 ),
168 Self::SignatureLength { algorithm, actual, .. } => {
169 let values = algorithm.value_count();
170 write!(
171 f,
172 "`{algorithm}` signatures are `{}` hex digits, `{}` bytes or `{values}` values long, not `{actual}`",
173 values * 8,
174 values * 4
175 )
176 }
177 Self::BandCount { bands, values } => write!(
178 f,
179 "can't split `{values}` values into `{bands}` equal bands; use a divisor of `{values}`"
180 ),
181 #[cfg(feature = "image")]
182 Self::Decode(message) => write!(f, "couldn't decode the image: {message}"),
183 }
184 }
185}
186
187impl std::error::Error for Error {}