From bef1aa1f569d180632acf02bf9df5195a68a99b9 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sat, 25 Nov 2023 21:22:26 +0000 Subject: [PATCH] Add context to Error::ParsingIncomplete --- src/error.rs | 21 ++++++++++++++++++--- src/lib.rs | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index 2c9a417..d84d9f2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -20,6 +20,7 @@ use std::error; use std::fmt; use std::io; +use std::num::NonZeroUsize; use std::path::PathBuf; use nom::Err; @@ -28,7 +29,7 @@ use nom::Err; pub enum Error { IoError(io::Error), NoFilename(PathBuf), - ParsingIncomplete, + ParsingIncomplete(MoreDataNeeded), ParsingError(Vec, ParsingErrorKind), DecodeError(Vec), } @@ -36,7 +37,12 @@ pub enum Error { 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(err) | Err::Failure(err) => Error::ParsingError( err.input.to_vec(), ParsingErrorKind::GenericParserError(err.code.description().to_string()), @@ -56,7 +62,8 @@ impl fmt::Display for Error { match self { Error::IoError(x) => x.fmt(f), Error::NoFilename(path) => write!(f, "The plugin path {path:?} has no filename part"), - Error::ParsingIncomplete => write!(f, "More input was expected by the plugin parser"), + Error::ParsingIncomplete(MoreDataNeeded::UnknownSize) => write!(f, "An unknown number of bytes of additional input was expected by the plugin parser"), + Error::ParsingIncomplete(MoreDataNeeded::Size(size)) => write!(f, "{size} bytes of additional input was expected by the plugin parser"), Error::ParsingError(input, kind) => write!( f, "An error was encountered while parsing the plugin content {:02X?}: {}", @@ -104,3 +111,11 @@ impl fmt::Display for ParsingErrorKind { } } } + +#[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), +} diff --git a/src/lib.rs b/src/lib.rs index ce474f8..e480e5f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,7 +18,7 @@ */ use std::convert::{TryFrom, TryInto}; -pub use crate::error::Error; +pub use crate::error::{Error, MoreDataNeeded, ParsingErrorKind}; pub use crate::game_id::GameId; pub use crate::plugin::Plugin;