From 3a35dc3a207362efc7f56940a09531ed348c174c Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Fri, 10 May 2024 16:44:59 -0500 Subject: [PATCH] Iterators are melting my brain --- Cargo.toml | 8 ++++++++ src/main.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 Cargo.toml diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e1d31dc --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "qoicodec" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs index 34d4f30..6c2eb9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,53 @@ + +use std::{fs::File, io::Read}; + fn main() { - println!("Hello, world!"); + let file = File::open("qoi_test_images/dice.qoi").unwrap(); + let mut bytes = file.bytes() + .map(|maybe_bytes| + { + match maybe_bytes { + Ok(byte) => byte, + Err(oops) => panic!("Oops, the file failed to load: {}", oops) + } + }); + if let Err(e) = try_read_magic(&mut bytes) { + match e { + DecodeError::Magic => panic!("QOI Magic bytes are bad!"), + DecodeError::EarlyIteratorExhaustion => panic!("File iterator exhausted earlier than expected."), + _ => panic!("Unhandled error reading magic: {:?}", e), + } + } + + todo!("The rest of the main function"); +} + +fn try_read_magic>(bytes: &mut I) -> Result<(), DecodeError> { + let magic: [u8; 4] = [b'q', b'o', b'i', b'f']; + for i in 0..4 { + if let Some(letter) = bytes.next() { + if letter != magic[i] { + return Err(DecodeError::Magic) + } + } else { + return Err(DecodeError::EarlyIteratorExhaustion) + } + } + return Ok(()) +} + +fn try_read_u32>(bytes: &mut I) -> Result { + let mut res: u32 = 0; + + Ok(res) +} + +#[derive(Debug)] +enum DecodeError { + Magic, + Channels, + ColorSpace, + EarlyIteratorExhaustion, } #[derive(Clone, Copy, Default, Debug, PartialEq)]