1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::fmt;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Clone, PartialEq)]
pub enum Error {
    /// The string is not a full Bencode packet, more bytes expected
    Eof,

    /// Unexpected character at given position
    Unexpected { pos: usize },

    /// Invalid data at given position
    Invalid { reason: &'static str, pos: usize },

    /// Exceeded Token limit
    TokenLimit { limit: usize },

    /// Exceeded Depth limit
    DepthLimit { limit: usize },

    /// Integer Overflow
    Overflow { pos: usize },

    /// Type mismatch
    TypeMismatch(&'static str),

    /// Other
    Other(&'static str),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Eof => write!(f, "Unexpected End of File"),
            Self::Unexpected { pos } => write!(f, "Unexpected character at {}", pos),
            Self::Invalid { reason, pos } => write!(f, "Invalid input at {}: {}", pos, reason),
            Self::TokenLimit { limit } => write!(f, "Exceeded Token limit of {}", limit),
            Self::DepthLimit { limit } => write!(f, "Exceeded Depth limit of {}", limit),
            Self::Overflow { pos } => write!(f, "Integer overflow at {}", pos),
            Self::TypeMismatch(reason) => write!(f, "Type mismatch: {}", reason),
            Self::Other(reason) => f.write_str(reason),
        }
    }
}

impl std::error::Error for Error {}