-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cram/io/reader/header/magic_number: Split read and validation
- Loading branch information
Showing
5 changed files
with
48 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,32 @@ | ||
use tokio::io::{self, AsyncRead, AsyncReadExt}; | ||
|
||
pub(crate) async fn read_magic_number<R>(reader: &mut R) -> io::Result<()> | ||
use crate::MAGIC_NUMBER; | ||
|
||
pub(crate) async fn read_magic_number<R>(reader: &mut R) -> io::Result<[u8; MAGIC_NUMBER.len()]> | ||
where | ||
R: AsyncRead + Unpin, | ||
{ | ||
use crate::MAGIC_NUMBER; | ||
|
||
let mut magic = [0; 4]; | ||
reader.read_exact(&mut magic).await?; | ||
|
||
if magic == MAGIC_NUMBER { | ||
Ok(()) | ||
} else { | ||
Err(io::Error::new( | ||
io::ErrorKind::InvalidData, | ||
"invalid CRAM header", | ||
)) | ||
} | ||
let mut buf = [0; MAGIC_NUMBER.len()]; | ||
reader.read_exact(&mut buf).await?; | ||
Ok(buf) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_read_magic_number() { | ||
let data = b"CRAM"; | ||
let mut reader = &data[..]; | ||
assert!(read_magic_number(&mut reader).await.is_ok()); | ||
async fn test_read_magic_number() -> io::Result<()> { | ||
let src = b"CRAM"; | ||
let mut reader = &src[..]; | ||
assert_eq!(read_magic_number(&mut reader).await?, *b"CRAM"); | ||
|
||
let data = []; | ||
let mut reader = &data[..]; | ||
let mut src = &[][..]; | ||
assert!(matches!( | ||
read_magic_number(&mut reader).await, | ||
Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof | ||
read_magic_number(&mut src).await, | ||
Err(e) if e.kind() == io::ErrorKind::UnexpectedEof | ||
)); | ||
|
||
let data = b"BAM\x01"; | ||
let mut reader = &data[..]; | ||
assert!(matches!( | ||
read_magic_number(&mut reader).await, | ||
Err(ref e) if e.kind() == io::ErrorKind::InvalidData | ||
)); | ||
Ok(()) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
mod magic_number; | ||
pub(crate) mod magic_number; | ||
|
||
pub(super) use self::magic_number::read_magic_number; |
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