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
//! Errors produced by this library.

use nom::error::{ErrorKind, ParseError};
use std::{fmt::Debug, num::ParseIntError};

/// Parser Errors.
#[derive(Debug, thiserror::Error)]
pub enum BencodeError<I> {
    /// A error from a nom parser.
    #[error("a nom error: {1:?}")]
    Nom(I, ErrorKind),
    /// A integer has an invalid form, e.g -0.
    #[error("invalid integer: {0:?}")]
    InvalidInteger(I),
    /// A byte array length is invalid..
    #[error("invalid bytes length: {0:?}")]
    InvalidBytesLength(I),
    /// A integer could not be parsed correctly.
    #[error("parse int error: {0:?}")]
    ParseIntError(I, ParseIntError),
}

impl<I> ParseError<I> for BencodeError<I> {
    fn from_error_kind(input: I, kind: nom::error::ErrorKind) -> Self {
        Self::Nom(input, kind)
    }

    fn append(_: I, _: nom::error::ErrorKind, other: Self) -> Self {
        other
    }
}

impl<I> From<BencodeError<I>> for nom::Err<BencodeError<I>> {
    fn from(value: BencodeError<I>) -> Self {
        match value {
            value @ BencodeError::Nom(_, _) => Self::Error(value),
            value => Self::Failure(value),
        }
    }
}