From 779500a99cffc88def3b754babfe50eac0f6d237 Mon Sep 17 00:00:00 2001 From: Michael Macias Date: Fri, 10 Jan 2025 15:32:02 -0600 Subject: [PATCH] cram/io/reader/num/itf8: Add convenience function that converts to another number type --- .../async/io/reader/header/container/block.rs | 15 +++------------ .../async/io/reader/header/container/header.rs | 6 ++---- noodles-cram/src/async/io/reader/num.rs | 5 ++++- noodles-cram/src/async/io/reader/num/itf8.rs | 13 +++++++++++++ .../src/io/reader/header/container/block.rs | 14 +++----------- .../src/io/reader/header/container/header.rs | 6 ++---- noodles-cram/src/io/reader/num.rs | 2 +- noodles-cram/src/io/reader/num/itf8.rs | 16 +++++++++++++++- 8 files changed, 43 insertions(+), 34 deletions(-) diff --git a/noodles-cram/src/async/io/reader/header/container/block.rs b/noodles-cram/src/async/io/reader/header/container/block.rs index ebb12e8e6..e4cfa8be5 100644 --- a/noodles-cram/src/async/io/reader/header/container/block.rs +++ b/noodles-cram/src/async/io/reader/header/container/block.rs @@ -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(reader: &mut R) -> io::Result> @@ -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 = match compression_method { CompressionMethod::None => Box::new(reader.take(uncompressed_size)), @@ -93,12 +93,3 @@ fn validate_content_type(actual: ContentType) -> io::Result<()> { )) } } - -async fn read_itf8_as_u64(reader: &mut R) -> io::Result -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))) -} diff --git a/noodles-cram/src/async/io/reader/header/container/header.rs b/noodles-cram/src/async/io/reader/header/container/header.rs index 2e1ec7b80..7073092cb 100644 --- a/noodles-cram/src/async/io/reader/header/container/header.rs +++ b/noodles-cram/src/async/io/reader/header/container/header.rs @@ -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, }; @@ -49,9 +49,7 @@ async fn read_landmarks(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?; diff --git a/noodles-cram/src/async/io/reader/num.rs b/noodles-cram/src/async/io/reader/num.rs index 371aae3ca..ed8492da2 100644 --- a/noodles-cram/src/async/io/reader/num.rs +++ b/noodles-cram/src/async/io/reader/num.rs @@ -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, +}; diff --git a/noodles-cram/src/async/io/reader/num/itf8.rs b/noodles-cram/src/async/io/reader/num/itf8.rs index f4d0c0cf9..bb7434436 100644 --- a/noodles-cram/src/async/io/reader/num/itf8.rs +++ b/noodles-cram/src/async/io/reader/num/itf8.rs @@ -1,3 +1,5 @@ +use std::num; + use tokio::io::{self, AsyncRead, AsyncReadExt}; pub async fn read_itf8(reader: &mut R) -> io::Result @@ -29,6 +31,17 @@ where } } +pub async fn read_itf8_as(reader: &mut R) -> io::Result +where + R: AsyncRead + Unpin, + N: TryFrom, +{ + 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(reader: &mut R) -> io::Result where R: AsyncRead + Unpin, diff --git a/noodles-cram/src/io/reader/header/container/block.rs b/noodles-cram/src/io/reader/header/container/block.rs index 2c24b9e7e..3d6e9d6a3 100644 --- a/noodles-cram/src/io/reader/header/container/block.rs +++ b/noodles-cram/src/io/reader/header/container/block.rs @@ -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(reader: &mut R) -> io::Result> @@ -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 = match compression_method { CompressionMethod::None => Box::new(reader.take(uncompressed_size)), @@ -93,11 +93,3 @@ fn validate_content_type(actual: ContentType) -> io::Result<()> { )) } } - -fn read_itf8_as_u64(reader: &mut R) -> io::Result -where - R: Read, -{ - read_itf8(reader) - .and_then(|n| u64::try_from(n).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))) -} diff --git a/noodles-cram/src/io/reader/header/container/header.rs b/noodles-cram/src/io/reader/header/container/header.rs index 3c993f39f..bc64e0266 100644 --- a/noodles-cram/src/io/reader/header/container/header.rs +++ b/noodles-cram/src/io/reader/header/container/header.rs @@ -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(reader: &mut R) -> io::Result where @@ -49,9 +49,7 @@ fn read_landmarks(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)?; diff --git a/noodles-cram/src/io/reader/num.rs b/noodles-cram/src/io/reader/num.rs index 1fbb70a0f..d58e819dd 100644 --- a/noodles-cram/src/io/reader/num.rs +++ b/noodles-cram/src/io/reader/num.rs @@ -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, }; diff --git a/noodles-cram/src/io/reader/num/itf8.rs b/noodles-cram/src/io/reader/num/itf8.rs index 9e8b2eacd..b39684ccb 100644 --- a/noodles-cram/src/io/reader/num/itf8.rs +++ b/noodles-cram/src/io/reader/num/itf8.rs @@ -1,4 +1,7 @@ -use std::io::{self, Read}; +use std::{ + io::{self, Read}, + num, +}; use byteorder::ReadBytesExt; use bytes::Buf; @@ -78,6 +81,17 @@ where Ok(value) } +pub fn read_itf8_as(reader: &mut R) -> io::Result +where + R: Read, + N: TryFrom, +{ + read_itf8(reader).and_then(|n| { + n.try_into() + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) + }) +} + fn read_u8_as_i32(reader: &mut R) -> io::Result where R: Read,