Skip to content

Commit

Permalink
remove nom from the error type (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
cadubentzen authored May 13, 2023
1 parent 7522bbf commit 951502c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
30 changes: 27 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use std::{num::TryFromIntError, string::FromUtf8Error};
/// An Error while parsing Matroska/WebM files
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum Error {
/// Need data
#[error("need data")]
NeedData,
/// Parsing error
#[error("parsing error")]
Parsing(#[from] nom::Err<()>),
#[error("parser error")]
Parser,
/// Invalid ID
#[error("invalid id")]
InvalidId,
Expand All @@ -17,7 +20,7 @@ pub enum Error {
ForbiddenUnknownSize,
/// Error building UTF-8 string
#[error("{0}")]
Utf8Error(#[from] FromUtf8Error),
Utf8(#[from] FromUtf8Error),
/// Forbidden Integer size
#[error("forbidden integer size")]
ForbiddenIntegerSize,
Expand All @@ -37,3 +40,24 @@ pub enum Error {
#[error("invalid date")]
InvalidDate,
}

impl From<nom::Err<()>> for Error {
fn from(value: nom::Err<()>) -> Self {
match value {
nom::Err::Incomplete(_) => Self::NeedData,
_ => Self::Parser,
}
}
}

// FIXME(#53) This is mostly to keep coverage happy, but that error type will
// in practice never be instantiated as we don't use combinators in nom.
// After removing nom as a dependency we should be able to remove this test as well.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parser() {
assert_eq!(Error::Parser, nom::Err::Error(()).into());
}
}
7 changes: 1 addition & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,7 @@ mod tests {
assert_eq!(parse_id(&[0x23, 0x83, 0xE3]), Ok((EMPTY, Id::FrameRate)));

// 1 byte missing from FrameRate (3-bytes long)
assert_eq!(
parse_id(&[0x23, 0x83]),
Err(Error::Parsing(nom::Err::Incomplete(nom::Needed::Size(
1.try_into().unwrap()
))))
);
assert_eq!(parse_id(&[0x23, 0x83]), Err(Error::NeedData));

// Longer than 4 bytes
const FAILURE_INPUT: &[u8] = &[0x08, 0x45, 0xDF, 0xA3];
Expand Down
5 changes: 3 additions & 2 deletions tests/insta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ fn parse_elements(input: &[u8], show_position: bool) -> Vec<Element> {
}
});
elements.push(element);
if new_read_buffer.is_empty() {
read_buffer = new_read_buffer;
if read_buffer.is_empty() {
break;
}
read_buffer = new_read_buffer;
}
assert_eq!(read_buffer.len(), 0);
elements
}

Expand Down

0 comments on commit 951502c

Please sign in to comment.