diff --git a/noodles-bam/src/io/reader/header.rs b/noodles-bam/src/io/reader/header.rs index c9fd80508..e8dc7470a 100644 --- a/noodles-bam/src/io/reader/header.rs +++ b/noodles-bam/src/io/reader/header.rs @@ -1,3 +1,4 @@ +mod magic_number; mod reference_sequences; mod sam_header; @@ -6,7 +7,7 @@ use std::io::{self, BufRead, Read}; use byteorder::{LittleEndian, ReadBytesExt}; use noodles_sam::{self as sam, header::ReferenceSequences}; -use self::reference_sequences::read_reference_sequences; +use self::{magic_number::read_magic_number, reference_sequences::read_reference_sequences}; use crate::MAGIC_NUMBER; struct Reader<'r, R> { @@ -45,22 +46,6 @@ where read_header_inner(&mut header_reader) } -fn read_magic_number(reader: &mut Reader) -> io::Result<()> -where - R: Read, -{ - let magic_number = reader.read_magic_number()?; - - if magic_number == MAGIC_NUMBER { - Ok(()) - } else { - Err(io::Error::new( - io::ErrorKind::InvalidData, - "invalid BAM header", - )) - } -} - fn read_header_inner(reader: &mut Reader) -> io::Result where R: Read, @@ -161,29 +146,6 @@ mod tests { None => unreachable!(), }; - #[test] - fn test_read_magic_number() -> io::Result<()> { - let mut src = &b"BAM\x01"[..]; - let mut reader = Reader::new(&mut src); - assert!(read_magic_number(&mut reader).is_ok()); - - let mut src = &[][..]; - let mut reader = Reader::new(&mut src); - assert!(matches!( - read_magic_number(&mut reader), - Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof - )); - - let mut src = &b"MThd"[..]; - let mut reader = Reader::new(&mut src); - assert!(matches!( - read_magic_number(&mut reader), - Err(ref e) if e.kind() == io::ErrorKind::InvalidData - )); - - Ok(()) - } - #[test] fn test_read_header() -> io::Result<()> { let mut data = Vec::new(); diff --git a/noodles-bam/src/io/reader/header/magic_number.rs b/noodles-bam/src/io/reader/header/magic_number.rs new file mode 100644 index 000000000..c6f031b42 --- /dev/null +++ b/noodles-bam/src/io/reader/header/magic_number.rs @@ -0,0 +1,48 @@ +use std::io::{self, Read}; + +use super::Reader; +use crate::MAGIC_NUMBER; + +pub(super) fn read_magic_number(reader: &mut Reader) -> io::Result<()> +where + R: Read, +{ + let magic_number = reader.read_magic_number()?; + + if magic_number == MAGIC_NUMBER { + Ok(()) + } else { + Err(io::Error::new( + io::ErrorKind::InvalidData, + "invalid BAM header", + )) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_read_magic_number() -> io::Result<()> { + let mut src = &b"BAM\x01"[..]; + let mut reader = Reader::new(&mut src); + assert!(read_magic_number(&mut reader).is_ok()); + + let mut src = &[][..]; + let mut reader = Reader::new(&mut src); + assert!(matches!( + read_magic_number(&mut reader), + Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof + )); + + let mut src = &b"MThd"[..]; + let mut reader = Reader::new(&mut src); + assert!(matches!( + read_magic_number(&mut reader), + Err(ref e) if e.kind() == io::ErrorKind::InvalidData + )); + + Ok(()) + } +}