1use std::fmt;
2use std::io;
3
4#[derive(Debug)]
6pub enum Error {
7 InvalidData,
9 MoreDataNeeded(usize),
11 Io(io::Error),
13}
14
15impl std::error::Error for Error {
16 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
17 match self {
18 Self::Io(inner) => Some(inner),
19 _ => None,
20 }
21 }
22}
23
24impl fmt::Display for Error {
25 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26 match self {
27 Error::InvalidData => write!(f, "Invalid Data"),
28 Error::MoreDataNeeded(n) => write!(f, "{n} more bytes needed"),
29 Error::Io(_) => write!(f, "I/O error"),
30 }
31 }
32}
33
34impl From<io::Error> for Error {
35 fn from(value: io::Error) -> Self {
36 Self::Io(value)
37 }
38}
39
40pub type Result<T> = ::std::result::Result<T, Error>;
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn io_error_conversion() {
49 let io_err = io::Error::new(io::ErrorKind::Other, "foobar");
50
51 let err: Error = io_err.into();
52
53 match err {
54 Error::Io(_) => {}
55 _ => panic!("Error doesn't match"),
56 }
57 }
58}