stremio_serde_hex/
types.rs

1//! Miscellaneous type used by this crate.
2use std::{error, fmt, io, result};
3
4/// An alias of `std::result::Result` with this crate's
5/// `Error` type inserted by default.
6pub type Result<T> = result::Result<T, Error>;
7
8/// error raised during hexadecimal parsing operations
9#[derive(Debug, PartialEq, Eq)]
10pub enum ParseHexError {
11    /// hexadecimal buffer was outside allowed range
12    Range {
13        /// minimum allowed size
14        min: usize,
15        /// maximum allowed size
16        max: usize,
17        /// size the was found
18        got: usize,
19    },
20    /// hexadecimal buffer was of expected size
21    Size {
22        /// expected size
23        expect: usize,
24        /// size that was found
25        actual: usize,
26    },
27    /// non-hexadecimal character encountered
28    Char {
29        /// value encountered
30        val: char,
31    },
32}
33
34impl fmt::Display for ParseHexError {
35    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36        match *self {
37            ParseHexError::Range {
38                ref min,
39                ref max,
40                ref got,
41            } => write!(
42                f,
43                "expected buff size in `{}...{}`, got `{}`",
44                min, max, got
45            ),
46            ParseHexError::Size {
47                ref expect,
48                ref actual,
49            } => write!(f, "expected buff size `{}` got `{}`", expect, actual),
50            ParseHexError::Char { ref val } => write!(f, "non-hex character `{}`", val),
51        }
52    }
53}
54
55impl error::Error for ParseHexError {
56    fn description(&self) -> &str {
57        match *self {
58            ParseHexError::Range { .. } => "hexadecimal outside valid range",
59            ParseHexError::Size { .. } => "invalid hexadecimal size",
60            ParseHexError::Char { .. } => "non-hex character",
61        }
62    }
63}
64
65/// top-level error kind of this crate
66#[derive(Debug)]
67pub enum Error {
68    /// error raised during i/o operations
69    IoError(io::Error),
70    /// error raised during parsing operations
71    Parsing(ParseHexError),
72}
73
74// implement `Display` to allow user-facing errors.  Required
75// by the `std::error::Error` trait.
76impl fmt::Display for Error {
77    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78        match *self {
79            Error::IoError(ref err) => err.fmt(f),
80            Error::Parsing(ref err) => err.fmt(f),
81        }
82    }
83}
84
85// implement the standard error trait for hexadecimal errors.
86impl error::Error for Error {
87    fn cause(&self) -> Option<&dyn error::Error> {
88        match *self {
89            Error::IoError(ref err) => Some(err),
90            Error::Parsing(ref err) => Some(err),
91        }
92    }
93}
94
95impl From<io::Error> for Error {
96    fn from(err: io::Error) -> Self {
97        Error::IoError(err)
98    }
99}
100
101impl From<ParseHexError> for Error {
102    fn from(err: ParseHexError) -> Self {
103        Error::Parsing(err)
104    }
105}