diff --git a/ffi/src/helpers.rs b/ffi/src/helpers.rs index 80988bc..ef5229f 100644 --- a/ffi/src/helpers.rs +++ b/ffi/src/helpers.rs @@ -23,7 +23,7 @@ pub fn handle_error(err: Error) -> c_int { fn map_error(err: &Error) -> c_int { match err { - Error::ParsingIncomplete => LCI_ERROR_PARSING_ERROR, + Error::ParsingIncomplete(_) => LCI_ERROR_PARSING_ERROR, Error::UnconsumedInput(_) => LCI_ERROR_PARSING_ERROR, Error::ParsingError(_, _) => LCI_ERROR_PARSING_ERROR, Error::PeParsingError(_, _) => LCI_ERROR_PE_PARSING_ERROR, diff --git a/src/error.rs b/src/error.rs index 45cb1c8..dc780d0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,7 @@ use std::error; use std::fmt; use std::io; +use std::num::NonZeroUsize; use std::num::ParseIntError; use std::path::PathBuf; @@ -9,7 +10,7 @@ use nom::Err; #[derive(Debug)] pub enum Error { - ParsingIncomplete, + ParsingIncomplete(MoreDataNeeded), // The string is the input that was not parsed. UnconsumedInput(String), /// The string is the input at which the error was encountered. @@ -25,7 +26,12 @@ fn escape(input: I) -> String { impl From>> for Error { fn from(error: Err>) -> Self { match error { - Err::Incomplete(_) => Error::ParsingIncomplete, + Err::Incomplete(nom::Needed::Unknown) => { + Error::ParsingIncomplete(MoreDataNeeded::UnknownSize) + } + Err::Incomplete(nom::Needed::Size(size)) => { + Error::ParsingIncomplete(MoreDataNeeded::Size(size)) + } Err::Error(e) | Err::Failure(e) => Error::ParsingError(escape(e.input), e.kind), } } @@ -34,7 +40,14 @@ impl From>> for Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Error::ParsingIncomplete => write!(f, "More input was expected by the parser"), + Error::ParsingIncomplete(MoreDataNeeded::UnknownSize) => write!( + f, + "An unknown number of bytes of additional input was expected by the parser" + ), + Error::ParsingIncomplete(MoreDataNeeded::Size(size)) => write!( + f, + "{size} bytes of additional input was expected by the parser" + ), Error::UnconsumedInput(i) => write!( f, "The parser did not consume the following input: \"{}\"", @@ -72,6 +85,14 @@ impl error::Error for Error { } } +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum MoreDataNeeded { + /// It's not known how much more data are needed + UnknownSize, + /// Contains the number of bytes of data that are needed + Size(NonZeroUsize), +} + #[derive(Debug)] pub struct ParsingError { input: I, diff --git a/src/lib.rs b/src/lib.rs index ffa73f9..469b362 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,7 @@ use nom::sequence::{delimited, preceded}; use nom::IResult; use error::ParsingError; -pub use error::{Error, ParsingErrorKind}; +pub use error::{Error, MoreDataNeeded, ParsingErrorKind}; use function::Function; type ParsingResult<'a, T> = IResult<&'a str, T, ParsingError<&'a str>>;