Skip to content

Improved error handling #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions src/async_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ pub(crate) struct AsyncCursor {
/// Macro to generate functions to read scalar values from the cursor
macro_rules! impl_read_byteorder {
($method_name:ident, $typ:ty) => {
pub(crate) async fn $method_name(&mut self) -> $typ {
let mut buf = Cursor::new(self.read(<$typ>::BITS as usize / 8).await);
pub(crate) async fn $method_name(&mut self) -> Result<$typ> {
let mut buf = Cursor::new(self.read(<$typ>::BITS as usize / 8).await?);
match self.endianness {
Endianness::LittleEndian => buf.$method_name::<LittleEndian>().unwrap(),
Endianness::BigEndian => buf.$method_name::<BigEndian>().unwrap(),
Endianness::LittleEndian => Ok(buf.$method_name::<LittleEndian>()?),
Endianness::BigEndian => Ok(buf.$method_name::<BigEndian>()?),
}
}
};
Expand All @@ -157,7 +157,7 @@ impl AsyncCursor {
pub(crate) async fn try_open_tiff(reader: Box<dyn AsyncFileReader>) -> Result<Self> {
// Initialize with default endianness and then set later
let mut cursor = Self::new(reader, Default::default());
let magic_bytes = cursor.read(2).await;
let magic_bytes = cursor.read(2).await?;

// Should be b"II" for little endian or b"MM" for big endian
if magic_bytes == Bytes::from_static(b"II") {
Expand All @@ -179,22 +179,22 @@ impl AsyncCursor {
}

/// Read the given number of bytes, advancing the internal cursor state by the same amount.
pub(crate) async fn read(&mut self, length: usize) -> Bytes {
pub(crate) async fn read(&mut self, length: usize) -> Result<Bytes> {
let range = self.offset as _..(self.offset + length) as _;
self.offset += length;
self.reader.get_bytes(range).await.unwrap()
self.reader.get_bytes(range).await
}

/// Read a u8 from the cursor
pub(crate) async fn read_u8(&mut self) -> u8 {
let buf = self.read(1).await;
Cursor::new(buf).read_u8().unwrap()
/// Read a u8 from the cursor, advancing the internal state by 1 byte.
pub(crate) async fn read_u8(&mut self) -> Result<u8> {
let buf = self.read(1).await?;
Ok(Cursor::new(buf).read_u8()?)
}

/// Read a i8 from the cursor
pub(crate) async fn read_i8(&mut self) -> i8 {
let buf = self.read(1).await;
Cursor::new(buf).read_i8().unwrap()
/// Read a i8 from the cursor, advancing the internal state by 1 byte.
pub(crate) async fn read_i8(&mut self) -> Result<i8> {
let buf = self.read(1).await?;
Ok(Cursor::new(buf).read_i8()?)
}

impl_read_byteorder!(read_u16, u16);
Expand All @@ -204,20 +204,22 @@ impl AsyncCursor {
impl_read_byteorder!(read_i32, i32);
impl_read_byteorder!(read_i64, i64);

pub(crate) async fn read_f32(&mut self) -> f32 {
let mut buf = Cursor::new(self.read(4).await);
match self.endianness {
Endianness::LittleEndian => buf.read_f32::<LittleEndian>().unwrap(),
Endianness::BigEndian => buf.read_f32::<BigEndian>().unwrap(),
}
pub(crate) async fn read_f32(&mut self) -> Result<f32> {
let mut buf = Cursor::new(self.read(4).await?);
let out = match self.endianness {
Endianness::LittleEndian => buf.read_f32::<LittleEndian>()?,
Endianness::BigEndian => buf.read_f32::<BigEndian>()?,
};
Ok(out)
}

pub(crate) async fn read_f64(&mut self) -> f64 {
let mut buf = Cursor::new(self.read(8).await);
match self.endianness {
Endianness::LittleEndian => buf.read_f64::<LittleEndian>().unwrap(),
Endianness::BigEndian => buf.read_f64::<BigEndian>().unwrap(),
}
pub(crate) async fn read_f64(&mut self) -> Result<f64> {
let mut buf = Cursor::new(self.read(8).await?);
let out = match self.endianness {
Endianness::LittleEndian => buf.read_f64::<LittleEndian>()?,
Endianness::BigEndian => buf.read_f64::<BigEndian>()?,
};
Ok(out)
}

#[allow(dead_code)]
Expand Down
8 changes: 3 additions & 5 deletions src/cog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ pub struct COGReader {
impl COGReader {
pub async fn try_open(reader: Box<dyn AsyncFileReader>) -> Result<Self> {
let mut cursor = AsyncCursor::try_open_tiff(reader).await?;
let version = cursor.read_u16().await;
let version = cursor.read_u16().await?;

// Assert it's a standard non-big tiff
assert_eq!(version, 42);

let first_ifd_location = cursor.read_u32().await;
let first_ifd_location = cursor.read_u32().await?;

let ifds = ImageFileDirectories::open(&mut cursor, first_ifd_location as usize)
.await
.unwrap();
let ifds = ImageFileDirectories::open(&mut cursor, first_ifd_location as usize).await?;

let reader = cursor.into_inner();
Ok(Self { reader, ifds })
Expand Down
Loading