Start refactoring for better error handling
- ID
f4600ee- date
2024-09-04 07:04:24+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
c68216c- message
Start refactoring for better error handling- changed files
1 file, 14 additions, 14 deletions
Changed files
src/get_image_colors.rs (6101) → src/get_image_colors.rs (6156)
diff --git a/src/get_image_colors.rs b/src/get_image_colors.rs
index 66cbedd..2c8100c 100644
--- a/src/get_image_colors.rs
+++ b/src/get_image_colors.rs
@@ -7,7 +7,7 @@
// It returns a Vec<Lab>, which can be passed to the k-means process.
use std::fs::File;
-use std::io::BufReader;
+use std::io::{BufReader, Error, ErrorKind, Result};
use std::path::PathBuf;
use image::codecs::gif::GifDecoder;
@@ -17,20 +17,10 @@ use image::{AnimationDecoder, DynamicImage, Frame, ImageFormat};
use palette::cast::from_component_slice;
use palette::{IntoColor, Lab, Srgba};
-pub fn get_image_colors(path: &PathBuf) -> Result<Vec<Lab>, &str> {
- let format = match path.extension() {
- Some(ext) => image::ImageFormat::from_extension(ext),
- None => return Err("Path has no file extension, so could not determine image format"),
- };
-
- let f = match File::open(path) {
- Ok(im) => im,
- Err(e) => {
- eprintln!("{}", e);
- std::process::exit(1);
- }
- };
+pub fn get_image_colors(path: &PathBuf) -> std::io::Result<Vec<Lab>> {
+ let format = get_format(path)?;
+ let f = File::open(path)?;
let reader = BufReader::new(f);
let image_bytes = match format {
@@ -69,6 +59,16 @@ pub fn get_image_colors(path: &PathBuf) -> Result<Vec<Lab>, &str> {
Ok(lab)
}
+fn get_format(path: &PathBuf) -> Result<Option<ImageFormat>> {
+ match path.extension() {
+ Some(ext) => Ok(image::ImageFormat::from_extension(ext)),
+ None => Err(Error::new(
+ ErrorKind::InvalidInput,
+ "Path has no file extension, so could not determine image format",
+ )),
+ }
+}
+
fn get_bytes_for_static_image(img: DynamicImage) -> Vec<u8> {
// Resize the image after we open it. For this tool I'd rather get a good answer
// quickly than a great answer slower.