Skip to content

Commit

Permalink
bam/io/reader/header: Move magic number reader to a module
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Dec 10, 2024
1 parent 376d609 commit a19e023
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 40 deletions.
42 changes: 2 additions & 40 deletions noodles-bam/src/io/reader/header.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod magic_number;
mod reference_sequences;
mod sam_header;

Expand All @@ -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> {
Expand Down Expand Up @@ -45,22 +46,6 @@ where
read_header_inner(&mut header_reader)
}

fn read_magic_number<R>(reader: &mut Reader<R>) -> 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<R>(reader: &mut Reader<R>) -> io::Result<sam::Header>
where
R: Read,
Expand Down Expand Up @@ -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();
Expand Down
48 changes: 48 additions & 0 deletions noodles-bam/src/io/reader/header/magic_number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::io::{self, Read};

use super::Reader;
use crate::MAGIC_NUMBER;

pub(super) fn read_magic_number<R>(reader: &mut Reader<R>) -> 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(())
}
}

0 comments on commit a19e023

Please sign in to comment.