Coverage Report

Created: 2025-08-26 06:03

/rust/registry/src/index.crates.io-6f17d22bba15001f/arbitrary-1.4.2/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
    ///
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
20
impl fmt::Display for Error {
21
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22
0
        match self {
23
0
            Error::EmptyChoose => write!(
24
0
                f,
25
0
                "`arbitrary::Unstructured::choose` must be given a non-empty set of choices"
26
0
            ),
27
0
            Error::NotEnoughData => write!(
28
0
                f,
29
0
                "There is not enough underlying raw data to construct an `Arbitrary` instance"
30
0
            ),
31
0
            Error::IncorrectFormat => write!(
32
0
                f,
33
0
                "The raw data is not of the correct format to construct this type"
34
0
            ),
35
        }
36
0
    }
37
}
38
39
impl 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)`.
44
pub type Result<T, E = Error> = std::result::Result<T, E>;
45
46
#[cfg(test)]
47
mod 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
}