1use nom::error::{ErrorKind, ParseError};
4use std::{fmt::Debug, num::ParseIntError};
5
6#[derive(Debug, thiserror::Error)]
8pub enum BencodeError<I> {
9 #[error("a nom error: {1:?}")]
11 Nom(I, ErrorKind),
12 #[error("invalid integer: {0:?}")]
14 InvalidInteger(I),
15 #[error("invalid bytes length: {0:?}")]
17 InvalidBytesLength(I),
18 #[error("parse int error: {0:?}")]
20 ParseIntError(I, ParseIntError),
21}
22
23impl<I> ParseError<I> for BencodeError<I> {
24 fn from_error_kind(input: I, kind: nom::error::ErrorKind) -> Self {
25 Self::Nom(input, kind)
26 }
27
28 fn append(_: I, _: nom::error::ErrorKind, other: Self) -> Self {
29 other
30 }
31}
32
33impl<I> From<BencodeError<I>> for nom::Err<BencodeError<I>> {
34 fn from(value: BencodeError<I>) -> Self {
35 match value {
36 value @ BencodeError::Nom(_, _) => Self::Error(value),
37 value => Self::Failure(value),
38 }
39 }
40}