Iterators are melting my brain
This commit is contained in:
8
Cargo.toml
Normal file
8
Cargo.toml
Normal 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]
|
||||
50
src/main.rs
50
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<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)]
|
||||
|
||||
Reference in New Issue
Block a user