byte_num/
error.rs

1use std::{error::Error, fmt, str};
2
3/// An enum representing the possible Errors encountered while parsing a slice of bytes to an integer.
4#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
5pub enum ParseIntErr {
6    /// Represents a character that could not be converted to a number.
7    InvalidDigit([u8; 1]),
8
9    /// Represents that parsing of the slice could not be started, the slice was too large.
10    Overflow,
11}
12
13impl fmt::Display for ParseIntErr {
14    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15        match *self {
16            ParseIntErr::InvalidDigit([ref c]) => write!(f, "ParseIntErr::InvalidDigit({})", c),
17            ParseIntErr::Overflow => f.pad("ParseIntErr::Overflow"),
18        }
19    }
20}
21
22impl Error for ParseIntErr {
23    fn description(&self) -> &str {
24        match *self {
25            ParseIntErr::InvalidDigit(ref c) => str::from_utf8(c).unwrap(),
26            ParseIntErr::Overflow => "number too large to fit in the target type",
27        }
28    }
29}
30
31impl ParseIntErr {
32    pub fn with_byte(c: u8) -> Self {
33        ParseIntErr::InvalidDigit([c])
34    }
35}