Compare commits

...

2 Commits

Author SHA1 Message Date
Max Bradbury b8c30fe873 update copyright notice 2021-12-11 17:44:48 +00:00
Max Bradbury b50bde1f64 better error handling for image 2021-12-11 17:44:17 +00:00
3 changed files with 24 additions and 6 deletions

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2020 Max Bradbury
Copyright © 2021 Max Bradbury
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -21,6 +21,12 @@ impl fmt::Display for NotFound {
}
}
#[derive(Debug)]
pub enum ImageError {
MalformedPixel,
WrongSize,
}
#[derive(Debug)]
pub enum Error {
Colour,
@ -31,7 +37,9 @@ pub enum Error {
Game {
missing: NotFound,
},
Image,
Image {
err: ImageError,
},
Item,
Palette,
Position,

View File

@ -1,4 +1,5 @@
use std::fmt;
use crate::error::ImageError;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Image {
@ -56,7 +57,7 @@ impl Image {
let mut warnings = Vec::new();
if str.contains("NaN") {
warnings.push(crate::Error::Image);
warnings.push(crate::Error::Image { err: ImageError::MalformedPixel });
}
let string = str.trim().replace("NaN", "0");
@ -68,15 +69,24 @@ impl Image {
for line in lines {
let line = &line[..dimension];
for char in line.chars().into_iter() {
// todo push warning on integers other than 0/1
pixels.push(match char {'1' => 1, _ => 0});
pixels.push(match char {
'0' => 0,
'1' => 1,
_ => {
warnings.push(
crate::Error::Image { err: ImageError::MalformedPixel }
);
0
}
});
}
}
// 8×8 (normal) or 16×16 (Bitsy HD)
if [64, 256].contains(&pixels.len()) {
Ok((Image { pixels }, warnings))
} else {
Err(crate::Error::Image)
Err(crate::Error::Image { err: ImageError::WrongSize })
}
}
}