Skip to main content

rustler/serde/
error.rs

1use crate::Error as NifError;
2use serde::{de, ser};
3use std::fmt::{Display, Formatter, Result};
4
5#[derive(Debug)]
6pub enum Error {
7    DeserializationError(String),
8    TypeHintsRequired,
9    InvalidAtom,
10    InvalidBoolean,
11    InvalidNumber,
12    InvalidStringable,
13    InvalidList,
14    InvalidTuple,
15    InvalidSequenceElement,
16    ExpectedAtom,
17    ExpectedBoolean,
18    ExpectedBinary,
19    ExpectedNumber,
20    ExpectedChar,
21    ExpectedStringable,
22    ExpectedNil,
23    ExpectedList,
24    ExpectedTuple,
25    ExpectedEnum,
26    ExpectedMap,
27    ExpectedStruct,
28    ExpectedStructName,
29    ExpectedStructValue,
30    ExpectedUnitVariant,
31    ExpectedNewtypeStruct,
32    ExpectedNewtypeVariant,
33    ExpectedTupleVariant,
34    ExpectedStructVariant,
35
36    SerializationError(String),
37    InvalidVariantName,
38    InvalidStructName,
39    InvalidBinary,
40    InvalidMap,
41    InvalidStruct,
42    InvalidStructKey,
43
44    NonFiniteFloat,
45}
46
47impl Display for Error {
48    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
49        write!(f, "{self:?}")
50    }
51}
52
53impl From<Error> for NifError {
54    fn from(err: Error) -> NifError {
55        NifError::RaiseTerm(Box::new(err.to_string()))
56    }
57}
58
59impl std::error::Error for Error {}
60
61impl ser::Error for Error {
62    fn custom<T: Display>(msg: T) -> Error {
63        Error::SerializationError(msg.to_string())
64    }
65}
66
67impl de::Error for Error {
68    fn custom<T: Display>(msg: T) -> Error {
69        Error::DeserializationError(msg.to_string())
70    }
71}