ruby_marshal/
error.rs

1use serde::{de, ser};
2use std::{
3    error,
4    fmt::{self, Display},
5};
6
7/// Define a struct of possible errors that can occur during serialization or deserialization.
8/// There aren't many because Marshal (surprisingly) doesn't have many error cases.
9#[derive(Debug)]
10pub enum Error {
11    Message(String),
12    IncompatibleVersion(String),
13    FormatError,
14}
15
16impl ser::Error for Error {
17    fn custom<T>(msg: T) -> Self
18    where
19        T: Display,
20    {
21        Error::Message(msg.to_string())
22    }
23}
24
25impl de::Error for Error {
26    fn custom<T>(msg: T) -> Self
27    where
28        T: Display,
29    {
30        Error::Message(msg.to_string())
31    }
32}
33
34impl Display for Error {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            Error::Message(msg) => f.write_str(msg),
38            Error::IncompatibleVersion(version) => f.write_fmt(format_args!(
39                "incompatible marshal format found {} expected 4.8",
40                version
41            )),
42            Error::FormatError => f.write_str("unexpeccted marshal format error"),
43        }
44    }
45}
46
47impl error::Error for Error {}
48
49pub type Result<T> = std::result::Result<T, Error>;