mini_parser: Use custom error for EOF
This commit is contained in:
parent
ee84247a1d
commit
def3b7269d
|
@ -12,6 +12,8 @@ pub enum MiniParserError {
|
||||||
TryFromIntError(#[from] num::TryFromIntError),
|
TryFromIntError(#[from] num::TryFromIntError),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
IOError(#[from] io::Error),
|
IOError(#[from] io::Error),
|
||||||
|
#[error("file ended while there was data left to process")]
|
||||||
|
UnexpectedEof,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Provides convenience methods for parsing binary formats.
|
/// Provides convenience methods for parsing binary formats.
|
||||||
|
@ -43,13 +45,7 @@ impl<R: io::Read + ?Sized> MiniParser for R {}
|
||||||
/// Gets the requested `range` from `slice` and errors with `UnexpectedEof` when range does not fit
|
/// Gets the requested `range` from `slice` and errors with `UnexpectedEof` when range does not fit
|
||||||
/// in slice.
|
/// in slice.
|
||||||
pub fn get_slice_range(slice: &[u8], range: Range<usize>) -> Result<&[u8], MiniParserError> {
|
pub fn get_slice_range(slice: &[u8], range: Range<usize>) -> Result<&[u8], MiniParserError> {
|
||||||
slice.get(range).ok_or_else(|| {
|
slice.get(range).ok_or(MiniParserError::UnexpectedEof)
|
||||||
io::Error::new(
|
|
||||||
io::ErrorKind::UnexpectedEof,
|
|
||||||
"File ended while there was data left to process",
|
|
||||||
)
|
|
||||||
.into()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Reference in a new issue