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    /// Internal error.
11    Internal(String),
12    ///
13    BigIntOverflow,
14    /// Received an unexpected type that could not be converted.
15    UnexpectedType,
16    #[doc(hidden)]
17    __NonExhaustive,
18}
19
20// TODO: remove this once either the Never type get's stabilized or the compiler
21// can properly handle Infallible.
22impl From<std::convert::Infallible> for ValueError {
23    fn from(_: std::convert::Infallible) -> Self {
24        unreachable!()
25    }
26}
27
28impl fmt::Display for ValueError {
29    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30        use ValueError::*;
31        match self {
32            InvalidString(e) => write!(
33                f,
34                "Value conversion failed - invalid non-utf8 string: {}",
35                e
36            ),
37            StringWithZeroBytes(_) => write!(f, "String contains \\0 bytes",),
38            Internal(e) => write!(f, "Value conversion failed - internal error: {}", e),
39            BigIntOverflow => write!(f, "BigInt overflow"),
40            UnexpectedType => write!(f, "Could not convert - received unexpected type"),
41            __NonExhaustive => unreachable!(),
42        }
43    }
44}
45
46impl error::Error for ValueError {}