Coverage Report

Created: 2023-04-25 07:07

/rust/registry/src/index.crates.io-6f17d22bba15001f/bincode-1.3.3/src/error.rs
Line
Count
Source (jump to first uncovered line)
1
use std::error::Error as StdError;
2
use std::io;
3
use std::str::Utf8Error;
4
use std::{error, fmt};
5
6
use serde;
7
8
/// The result of a serialization or deserialization operation.
9
pub type Result<T> = ::std::result::Result<T, Error>;
10
11
/// An error that can be produced during (de)serializing.
12
pub type Error = Box<ErrorKind>;
13
14
/// The kind of error that can be produced during a serialization or deserialization.
15
0
#[derive(Debug)]
16
pub enum ErrorKind {
17
    /// If the error stems from the reader/writer that is being used
18
    /// during (de)serialization, that error will be stored and returned here.
19
    Io(io::Error),
20
    /// Returned if the deserializer attempts to deserialize a string that is not valid utf8
21
    InvalidUtf8Encoding(Utf8Error),
22
    /// Returned if the deserializer attempts to deserialize a bool that was
23
    /// not encoded as either a 1 or a 0
24
    InvalidBoolEncoding(u8),
25
    /// Returned if the deserializer attempts to deserialize a char that is not in the correct format.
26
    InvalidCharEncoding,
27
    /// Returned if the deserializer attempts to deserialize the tag of an enum that is
28
    /// not in the expected ranges
29
    InvalidTagEncoding(usize),
30
    /// Serde has a deserialize_any method that lets the format hint to the
31
    /// object which route to take in deserializing.
32
    DeserializeAnyNotSupported,
33
    /// If (de)serializing a message takes more than the provided size limit, this
34
    /// error is returned.
35
    SizeLimit,
36
    /// Bincode can not encode sequences of unknown length (like iterators).
37
    SequenceMustHaveLength,
38
    /// A custom error message from Serde.
39
    Custom(String),
40
}
41
42
impl StdError for ErrorKind {
43
0
    fn description(&self) -> &str {
44
0
        match *self {
45
0
            ErrorKind::Io(ref err) => error::Error::description(err),
46
0
            ErrorKind::InvalidUtf8Encoding(_) => "string is not valid utf8",
47
0
            ErrorKind::InvalidBoolEncoding(_) => "invalid u8 while decoding bool",
48
0
            ErrorKind::InvalidCharEncoding => "char is not valid",
49
0
            ErrorKind::InvalidTagEncoding(_) => "tag for enum is not valid",
50
            ErrorKind::SequenceMustHaveLength => {
51
0
                "Bincode can only encode sequences and maps that have a knowable size ahead of time"
52
            }
53
            ErrorKind::DeserializeAnyNotSupported => {
54
0
                "Bincode doesn't support serde::Deserializer::deserialize_any"
55
            }
56
0
            ErrorKind::SizeLimit => "the size limit has been reached",
57
0
            ErrorKind::Custom(ref msg) => msg,
58
        }
59
0
    }
60
61
0
    fn cause(&self) -> Option<&error::Error> {
62
0
        match *self {
63
0
            ErrorKind::Io(ref err) => Some(err),
64
0
            ErrorKind::InvalidUtf8Encoding(_) => None,
65
0
            ErrorKind::InvalidBoolEncoding(_) => None,
66
0
            ErrorKind::InvalidCharEncoding => None,
67
0
            ErrorKind::InvalidTagEncoding(_) => None,
68
0
            ErrorKind::SequenceMustHaveLength => None,
69
0
            ErrorKind::DeserializeAnyNotSupported => None,
70
0
            ErrorKind::SizeLimit => None,
71
0
            ErrorKind::Custom(_) => None,
72
        }
73
0
    }
74
}
75
76
impl From<io::Error> for Error {
77
0
    fn from(err: io::Error) -> Error {
78
0
        ErrorKind::Io(err).into()
79
0
    }
80
}
81
82
impl fmt::Display for ErrorKind {
83
0
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
84
0
        match *self {
85
0
            ErrorKind::Io(ref ioerr) => write!(fmt, "io error: {}", ioerr),
86
0
            ErrorKind::InvalidUtf8Encoding(ref e) => write!(fmt, "{}: {}", self.description(), e),
87
0
            ErrorKind::InvalidBoolEncoding(b) => {
88
0
                write!(fmt, "{}, expected 0 or 1, found {}", self.description(), b)
89
            }
90
0
            ErrorKind::InvalidCharEncoding => write!(fmt, "{}", self.description()),
91
0
            ErrorKind::InvalidTagEncoding(tag) => {
92
0
                write!(fmt, "{}, found {}", self.description(), tag)
93
            }
94
0
            ErrorKind::SequenceMustHaveLength => write!(fmt, "{}", self.description()),
95
0
            ErrorKind::SizeLimit => write!(fmt, "{}", self.description()),
96
0
            ErrorKind::DeserializeAnyNotSupported => write!(
97
0
                fmt,
98
0
                "Bincode does not support the serde::Deserializer::deserialize_any method"
99
0
            ),
100
0
            ErrorKind::Custom(ref s) => s.fmt(fmt),
101
        }
102
0
    }
103
}
104
105
impl serde::de::Error for Error {
106
0
    fn custom<T: fmt::Display>(desc: T) -> Error {
107
0
        ErrorKind::Custom(desc.to_string()).into()
108
0
    }
Unexecuted instantiation: <alloc::boxed::Box<bincode::error::ErrorKind> as serde::de::Error>::custom::<&str>
Unexecuted instantiation: <alloc::boxed::Box<bincode::error::ErrorKind> as serde::de::Error>::custom::<core::fmt::Arguments>
Unexecuted instantiation: <alloc::boxed::Box<bincode::error::ErrorKind> as serde::de::Error>::custom::<smallvec::CollectionAllocErr>
Unexecuted instantiation: <alloc::boxed::Box<bincode::error::ErrorKind> as serde::de::Error>::custom::<&alloc::string::String>
Unexecuted instantiation: <alloc::boxed::Box<bincode::error::ErrorKind> as serde::de::Error>::custom::<_>
109
}
110
111
impl serde::ser::Error for Error {
112
0
    fn custom<T: fmt::Display>(msg: T) -> Self {
113
0
        ErrorKind::Custom(msg.to_string()).into()
114
0
    }
115
}