/rust/registry/src/index.crates.io-6f17d22bba15001f/arbitrary-1.4.1/src/error.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use std::{error, fmt}; |
2 | | |
3 | | /// An enumeration of buffer creation errors |
4 | | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
5 | | #[non_exhaustive] |
6 | | pub enum Error { |
7 | | /// No choices were provided to the Unstructured::choose call |
8 | | EmptyChoose, |
9 | | /// There was not enough underlying data to fulfill some request for raw |
10 | | /// bytes. |
11 | | NotEnoughData, |
12 | | /// The input bytes were not of the right format |
13 | | IncorrectFormat, |
14 | | } |
15 | | |
16 | | impl fmt::Display for Error { |
17 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
18 | 0 | match self { |
19 | 0 | Error::EmptyChoose => write!( |
20 | 0 | f, |
21 | 0 | "`arbitrary::Unstructured::choose` must be given a non-empty set of choices" |
22 | 0 | ), |
23 | 0 | Error::NotEnoughData => write!( |
24 | 0 | f, |
25 | 0 | "There is not enough underlying raw data to construct an `Arbitrary` instance" |
26 | 0 | ), |
27 | 0 | Error::IncorrectFormat => write!( |
28 | 0 | f, |
29 | 0 | "The raw data is not of the correct format to construct this type" |
30 | 0 | ), |
31 | | } |
32 | 0 | } |
33 | | } |
34 | | |
35 | | impl error::Error for Error {} |
36 | | |
37 | | /// A `Result` with the error type fixed as `arbitrary::Error`. |
38 | | /// |
39 | | /// Either an `Ok(T)` or `Err(arbitrary::Error)`. |
40 | | pub type Result<T, E = Error> = std::result::Result<T, E>; |
41 | | |
42 | | #[cfg(test)] |
43 | | mod tests { |
44 | | // Often people will import our custom `Result` type because 99.9% of |
45 | | // results in a file will be `arbitrary::Result` but then have that one last |
46 | | // 0.1% that want to have a custom error type. Don't make them prefix that |
47 | | // 0.1% as `std::result::Result`; instead, let `arbitrary::Result` have an |
48 | | // overridable error type. |
49 | | #[test] |
50 | | fn can_use_custom_error_types_with_result() -> super::Result<(), String> { |
51 | | Ok(()) |
52 | | } |
53 | | } |