quickjs_rusty/errors/
value_error.rs

1use std::{error, fmt};
2
3/// Error during value conversion.
4#[derive(PartialEq, Eq, Debug)]
5pub enum ValueError {
6    /// Invalid non-utf8 string.
7    InvalidString(std::str::Utf8Error),
8    /// Encountered string with \0 bytes.
9    StringWithZeroBytes(std::ffi::NulError),
10    /// Value out of range.
11    OutOfRange,
12    /// Internal error.
13    Internal(String),
14    ///
15    BigIntOverflow,
16    /// Received an unexpected type that could not be converted.
17    UnexpectedType,
18    #[doc(hidden)]
19    __NonExhaustive,
20}
21
22// TODO: remove this once either the Never type get's stabilized or the compiler
23// can properly handle Infallible.
24impl From<std::convert::Infallible> for ValueError {
25    fn from(_: std::convert::Infallible) -> Self {
26        unreachable!()
27    }
28}
29
30impl fmt::Display for ValueError {
31    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32        use ValueError::*;
33        match self {
34            InvalidString(e) => write!(
35                f,
36                "Value conversion failed - invalid non-utf8 string: {}",
37                e
38            ),
39            StringWithZeroBytes(_) => write!(f, "String contains \\0 bytes",),
40            OutOfRange => write!(f, "Value conversion failed - out of range"),
41            Internal(e) => write!(f, "Value conversion failed - internal error: {}", e),
42            BigIntOverflow => write!(f, "BigInt overflow"),
43            UnexpectedType => write!(f, "Could not convert - received unexpected type"),
44            __NonExhaustive => unreachable!(),
45        }
46    }
47}
48
49impl error::Error for ValueError {}