Skip to content

Commit

Permalink
cram/io/reader/num/itf8: Add convenience function that converts to an…
Browse files Browse the repository at this point in the history
…other number type
  • Loading branch information
zaeleus committed Jan 10, 2025
1 parent 87808fa commit 779500a
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 34 deletions.
15 changes: 3 additions & 12 deletions noodles-cram/src/async/io/reader/header/container/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use tokio::io::{self, AsyncRead, AsyncReadExt, BufReader};

use crate::{
container::block::{CompressionMethod, ContentType},
r#async::io::reader::num::read_itf8,
r#async::io::reader::num::{read_itf8, read_itf8_as},
};

pub(super) async fn read_block<R>(reader: &mut R) -> io::Result<Box<dyn AsyncRead + Unpin + '_>>
Expand All @@ -16,8 +16,8 @@ where
validate_content_type(content_type)?;

let _content_type_id = read_itf8(reader).await?;
let compressed_size = read_itf8_as_u64(reader).await?;
let uncompressed_size = read_itf8_as_u64(reader).await?;
let compressed_size = read_itf8_as(reader).await?;
let uncompressed_size = read_itf8_as(reader).await?;

let reader: Box<dyn AsyncRead + Unpin + '_> = match compression_method {
CompressionMethod::None => Box::new(reader.take(uncompressed_size)),
Expand Down Expand Up @@ -93,12 +93,3 @@ fn validate_content_type(actual: ContentType) -> io::Result<()> {
))
}
}

async fn read_itf8_as_u64<R>(reader: &mut R) -> io::Result<u64>
where
R: AsyncRead + Unpin,
{
read_itf8(reader)
.await
.and_then(|n| u64::try_from(n).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)))
}
6 changes: 2 additions & 4 deletions noodles-cram/src/async/io/reader/header/container/header.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tokio::io::{self, AsyncRead, AsyncReadExt};

use crate::r#async::io::reader::{
num::{read_itf8, read_ltf8},
num::{read_itf8, read_itf8_as, read_ltf8},
CrcReader,
};

Expand Down Expand Up @@ -49,9 +49,7 @@ async fn read_landmarks<R>(reader: &mut R) -> io::Result<()>
where
R: AsyncRead + Unpin,
{
let len = read_itf8(reader).await.and_then(|n| {
usize::try_from(n).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
})?;
let len: usize = read_itf8_as(reader).await?;

for _ in 0..len {
read_itf8(reader).await?;
Expand Down
5 changes: 4 additions & 1 deletion noodles-cram/src/async/io/reader/num.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
mod itf8;
mod ltf8;

pub use self::{itf8::read_itf8, ltf8::read_ltf8};
pub use self::{
itf8::{read_itf8, read_itf8_as},
ltf8::read_ltf8,
};
13 changes: 13 additions & 0 deletions noodles-cram/src/async/io/reader/num/itf8.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::num;

use tokio::io::{self, AsyncRead, AsyncReadExt};

pub async fn read_itf8<R>(reader: &mut R) -> io::Result<i32>
Expand Down Expand Up @@ -29,6 +31,17 @@ where
}
}

pub async fn read_itf8_as<R, N>(reader: &mut R) -> io::Result<N>
where
R: AsyncRead + Unpin,
N: TryFrom<i32, Error = num::TryFromIntError>,
{
read_itf8(reader).await.and_then(|n| {
n.try_into()
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
})
}

async fn read_u8_as_i32<R>(reader: &mut R) -> io::Result<i32>
where
R: AsyncRead + Unpin,
Expand Down
14 changes: 3 additions & 11 deletions noodles-cram/src/io/reader/header/container/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use flate2::read::GzDecoder;

use crate::{
container::block::{CompressionMethod, ContentType},
io::reader::num::read_itf8,
io::reader::num::{read_itf8, read_itf8_as},
};

pub(super) fn read_block<R>(reader: &mut R) -> io::Result<Box<dyn Read + '_>>
Expand All @@ -18,8 +18,8 @@ where
validate_content_type(content_type)?;

let _content_type_id = read_itf8(reader)?;
let compressed_size = read_itf8_as_u64(reader)?;
let uncompressed_size = read_itf8_as_u64(reader)?;
let compressed_size = read_itf8_as(reader)?;
let uncompressed_size = read_itf8_as(reader)?;

let reader: Box<dyn Read + '_> = match compression_method {
CompressionMethod::None => Box::new(reader.take(uncompressed_size)),
Expand Down Expand Up @@ -93,11 +93,3 @@ fn validate_content_type(actual: ContentType) -> io::Result<()> {
))
}
}

fn read_itf8_as_u64<R>(reader: &mut R) -> io::Result<u64>
where
R: Read,
{
read_itf8(reader)
.and_then(|n| u64::try_from(n).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)))
}
6 changes: 2 additions & 4 deletions noodles-cram/src/io/reader/header/container/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::{self, Read};
use byteorder::{LittleEndian, ReadBytesExt};
use flate2::CrcReader;

use crate::io::reader::num::{read_itf8, read_ltf8};
use crate::io::reader::num::{read_itf8, read_itf8_as, read_ltf8};

pub(crate) fn read_header<R>(reader: &mut R) -> io::Result<u64>
where
Expand Down Expand Up @@ -49,9 +49,7 @@ fn read_landmarks<R>(reader: &mut R) -> io::Result<()>
where
R: Read,
{
let len = read_itf8(reader).and_then(|n| {
usize::try_from(n).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
})?;
let len: usize = read_itf8_as(reader)?;

for _ in 0..len {
read_itf8(reader)?;
Expand Down
2 changes: 1 addition & 1 deletion noodles-cram/src/io/reader/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod ltf8;
mod vlq;

pub use self::{
itf8::{get_itf8, read_itf8},
itf8::{get_itf8, read_itf8, read_itf8_as},
ltf8::{get_ltf8, read_ltf8},
vlq::read_uint7,
};
16 changes: 15 additions & 1 deletion noodles-cram/src/io/reader/num/itf8.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::io::{self, Read};
use std::{
io::{self, Read},
num,
};

use byteorder::ReadBytesExt;
use bytes::Buf;
Expand Down Expand Up @@ -78,6 +81,17 @@ where
Ok(value)
}

pub fn read_itf8_as<R, N>(reader: &mut R) -> io::Result<N>
where
R: Read,
N: TryFrom<i32, Error = num::TryFromIntError>,
{
read_itf8(reader).and_then(|n| {
n.try_into()
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
})
}

fn read_u8_as_i32<R>(reader: &mut R) -> io::Result<i32>
where
R: Read,
Expand Down

0 comments on commit 779500a

Please sign in to comment.