-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bam/async/io/reader/header: Add magic number reader
- Loading branch information
Showing
3 changed files
with
41 additions
and
4 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,3 +1,4 @@ | ||
pub(super) mod magic_number; | ||
mod reference_sequences; | ||
|
||
use noodles_sam as sam; | ||
|
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,32 @@ | ||
use tokio::io::{self, AsyncRead, AsyncReadExt}; | ||
|
||
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, | ||
{ | ||
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() -> io::Result<()> { | ||
let src = b"BAM\x01"; | ||
let mut reader = &src[..]; | ||
assert_eq!(read_magic_number(&mut reader).await?, *b"BAM\x01"); | ||
|
||
let mut src = &[][..]; | ||
assert!(matches!( | ||
read_magic_number(&mut src).await, | ||
Err(e) if e.kind() == io::ErrorKind::UnexpectedEof | ||
)); | ||
|
||
Ok(()) | ||
} | ||
} |