From 951502ca936067539146bd0a39127fde30bc3612 Mon Sep 17 00:00:00 2001 From: Carlos Bentzen Date: Sat, 13 May 2023 10:55:12 +0000 Subject: [PATCH] remove nom from the error type (#52) --- src/error.rs | 30 +++++++++++++++++++++++++++--- src/lib.rs | 7 +------ tests/insta.rs | 5 +++-- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/error.rs b/src/error.rs index 3e08467..290aa1b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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, @@ -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, @@ -37,3 +40,24 @@ pub enum Error { #[error("invalid date")] InvalidDate, } + +impl From> 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()); + } +} diff --git a/src/lib.rs b/src/lib.rs index 783cfd0..c33281b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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]; diff --git a/tests/insta.rs b/tests/insta.rs index cb8ad41..08615dc 100644 --- a/tests/insta.rs +++ b/tests/insta.rs @@ -17,11 +17,12 @@ fn parse_elements(input: &[u8], show_position: bool) -> Vec { } }); 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 }