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
use nom::error::{ErrorKind, ParseError};
use std::{fmt::Debug, num::ParseIntError};
#[derive(Debug, thiserror::Error)]
pub enum BencodeError<I> {
#[error("a nom error: {1:?}")]
Nom(I, ErrorKind),
#[error("invalid integer: {0:?}")]
InvalidInteger(I),
#[error("invalid bytes length: {0:?}")]
InvalidBytesLength(I),
#[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),
}
}
}