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),
}
}
}