-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bam/record/codec/decoder: Remove panic when reading bin on EOF
- Loading branch information
Showing
3 changed files
with
65 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use std::{error, fmt, mem}; | ||
|
||
use bytes::Buf; | ||
|
||
/// An error when raw BAM record bin fail to parse. | ||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub enum DecodeError { | ||
/// Unexpected EOF. | ||
UnexpectedEof, | ||
} | ||
|
||
impl error::Error for DecodeError {} | ||
|
||
impl fmt::Display for DecodeError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Self::UnexpectedEof => write!(f, "unexpected EOF"), | ||
} | ||
} | ||
} | ||
|
||
pub(super) fn discard_bin<B>(src: &mut B) -> Result<(), DecodeError> | ||
where | ||
B: Buf, | ||
{ | ||
if src.remaining() < mem::size_of::<u16>() { | ||
return Err(DecodeError::UnexpectedEof); | ||
} | ||
|
||
src.advance(mem::size_of::<u16>()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_discard_bin() { | ||
let mut src = &[0x00, 0x00][..]; | ||
assert!(discard_bin(&mut src).is_ok()); | ||
|
||
let mut src = &[][..]; | ||
assert_eq!(discard_bin(&mut src), Err(DecodeError::UnexpectedEof)); | ||
|
||
let mut src = &[0x00][..]; | ||
assert_eq!(discard_bin(&mut src), Err(DecodeError::UnexpectedEof)); | ||
} | ||
} |