arbitrary/error.rs
1use std::{error, fmt};
2
3/// An enumeration of buffer creation errors
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[non_exhaustive]
6pub 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 ///
12 /// Note that outside of [`Unstructured::bytes`][crate::Unstructured::bytes],
13 /// most APIs do *not* return this error when running out of underlying arbitrary bytes
14 /// but silently return some default value instead.
15 NotEnoughData,
16 /// The input bytes were not of the right format
17 IncorrectFormat,
18}
19
20impl fmt::Display for Error {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 match self {
23 Error::EmptyChoose => write!(
24 f,
25 "`arbitrary::Unstructured::choose` must be given a non-empty set of choices"
26 ),
27 Error::NotEnoughData => write!(
28 f,
29 "There is not enough underlying raw data to construct an `Arbitrary` instance"
30 ),
31 Error::IncorrectFormat => write!(
32 f,
33 "The raw data is not of the correct format to construct this type"
34 ),
35 }
36 }
37}
38
39impl error::Error for Error {}
40
41/// A `Result` with the error type fixed as `arbitrary::Error`.
42///
43/// Either an `Ok(T)` or `Err(arbitrary::Error)`.
44pub type Result<T, E = Error> = std::result::Result<T, E>;
45
46#[cfg(test)]
47mod tests {
48 // Often people will import our custom `Result` type because 99.9% of
49 // results in a file will be `arbitrary::Result` but then have that one last
50 // 0.1% that want to have a custom error type. Don't make them prefix that
51 // 0.1% as `std::result::Result`; instead, let `arbitrary::Result` have an
52 // overridable error type.
53 #[test]
54 fn can_use_custom_error_types_with_result() -> super::Result<(), String> {
55 Ok(())
56 }
57}