Skip to content

Commit

Permalink
Add context to Error::ParsingIncomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
Ortham committed Nov 25, 2023
1 parent a97dcc0 commit bef1aa1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use std::error;
use std::fmt;
use std::io;
use std::num::NonZeroUsize;
use std::path::PathBuf;

use nom::Err;
Expand All @@ -28,15 +29,20 @@ use nom::Err;
pub enum Error {
IoError(io::Error),
NoFilename(PathBuf),
ParsingIncomplete,
ParsingIncomplete(MoreDataNeeded),
ParsingError(Vec<u8>, ParsingErrorKind),
DecodeError(Vec<u8>),
}

impl From<Err<nom::error::Error<&[u8]>>> for Error {
fn from(error: Err<nom::error::Error<&[u8]>>) -> 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()),
Expand All @@ -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?}: {}",
Expand Down Expand Up @@ -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),
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit bef1aa1

Please sign in to comment.