Iterators are melting my brain

This commit is contained in:
2024-05-10 16:44:59 -05:00
parent 761beac5fd
commit 3a35dc3a20
2 changed files with 57 additions and 1 deletions

8
Cargo.toml Normal file
View File

@@ -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]

View File

@@ -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<I: Iterator<Item=u8>>(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<I: Iterator<Item=u8>>(bytes: &mut I) -> Result<u32, DecodeError> {
let mut res: u32 = 0;
Ok(res)
}
#[derive(Debug)]
enum DecodeError {
Magic,
Channels,
ColorSpace,
EarlyIteratorExhaustion,
}
#[derive(Clone, Copy, Default, Debug, PartialEq)]