From def3b7269dc08675b28e2331127f6722dfddaa8f Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Fri, 7 Aug 2020 13:05:03 +0200 Subject: [PATCH] mini_parser: Use custom error for EOF --- src/mini_parser.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/mini_parser.rs b/src/mini_parser.rs index 32b4dea..072d726 100644 --- a/src/mini_parser.rs +++ b/src/mini_parser.rs @@ -12,6 +12,8 @@ pub enum MiniParserError { TryFromIntError(#[from] num::TryFromIntError), #[error(transparent)] IOError(#[from] io::Error), + #[error("file ended while there was data left to process")] + UnexpectedEof, } /// Provides convenience methods for parsing binary formats. @@ -43,13 +45,7 @@ impl MiniParser for R {} /// Gets the requested `range` from `slice` and errors with `UnexpectedEof` when range does not fit /// in slice. pub fn get_slice_range(slice: &[u8], range: Range) -> Result<&[u8], MiniParserError> { - slice.get(range).ok_or_else(|| { - io::Error::new( - io::ErrorKind::UnexpectedEof, - "File ended while there was data left to process", - ) - .into() - }) + slice.get(range).ok_or(MiniParserError::UnexpectedEof) } #[cfg(test)]