nom_bencode/
errors.rs

1//! Errors produced by this library.
2
3use nom::error::{ErrorKind, ParseError};
4use std::{fmt::Debug, num::ParseIntError};
5
6/// Parser Errors.
7#[derive(Debug, thiserror::Error)]
8pub enum BencodeError<I> {
9    /// A error from a nom parser.
10    #[error("a nom error: {1:?}")]
11    Nom(I, ErrorKind),
12    /// A integer has an invalid form, e.g -0.
13    #[error("invalid integer: {0:?}")]
14    InvalidInteger(I),
15    /// A byte array length is invalid..
16    #[error("invalid bytes length: {0:?}")]
17    InvalidBytesLength(I),
18    /// A integer could not be parsed correctly.
19    #[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}