From 549c478a8c74b2c9709441f5625b4ed858eb337c Mon Sep 17 00:00:00 2001 From: electria Date: Wed, 1 Jul 2026 09:52:20 -0700 Subject: [PATCH] feat: capture errors into the field --- src/main.rs | 117 ++++++++++++++++++++++++++++------------------------ 1 file changed, 64 insertions(+), 53 deletions(-) diff --git a/src/main.rs b/src/main.rs index dfc0ef0..663d849 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,28 +86,7 @@ impl State { } Key::Character("o") => { - let Some(path) = FileDialog::new() - .add_filter( - "image", - &[ - "png", "PNG", "jpg", "JPG", "jpeg", "JPEG", "avif", "bmp", "exr", - "ff", "gif", "hdr", "ico", "pnm", "qoi", "tga", "tiff", "webp", - ], - ) - .pick_file() - else { - eprintln!("no path to open provided"); - return Task::none(); - }; - - self.image = Some( - ImageReader::open(path) - .unwrap() - .decode() - .unwrap() - .into_rgba8(), - ); - + self.error = self.open_image(); return self.allocate_image(); } @@ -129,37 +108,7 @@ impl State { } Key::Character("s") => { - let Some(image) = self.image.as_ref() else { - eprintln!("no image to save"); - return Task::none(); - }; - - let Some(path) = FileDialog::new().save_file() else { - eprintln!("no path to save provided"); - return Task::none(); - }; - - let Ok(file) = fs::File::create(&path) - .inspect_err(|e| eprintln!("failed to create file: {e}")) - else { - return Task::none(); - }; - - let maybe_encoder = - match path.extension().map(OsStr::to_string_lossy).as_deref() { - Some("webp") => Some(WebPEncoder::new_lossless(file)), - - None | Some(_) => None, - }; - - let result = if let Some(encoder) = maybe_encoder { - image.write_with_encoder(encoder) - } else { - image.save(path) - }; - - #[allow(unused_must_use)] - result.inspect_err(|e| eprintln!("failed to save image: {e}")); + self.error = self.save_image(); } Key::Character("q") => return window::latest().and_then(window::close), @@ -175,6 +124,68 @@ impl State { Task::none() } + fn open_image(&mut self) -> Option { + let Some(path) = FileDialog::new() + .add_filter( + "image", + &[ + "png", "PNG", "jpg", "JPG", "jpeg", "JPEG", "avif", "bmp", "exr", "ff", "gif", + "hdr", "ico", "pnm", "qoi", "tga", "tiff", "webp", + ], + ) + .pick_file() + else { + return Some("no path to open provided".into()); + }; + + let reader = match ImageReader::open(path) { + Ok(r) => r, + Err(e) => return Some(format!("failed to create reader: {e}")), + }; + + let decoded_image = match reader.decode() { + Ok(i) => i, + Err(e) => return Some(format!("failed to decode image: {e}")), + }; + + self.image = Some(decoded_image.into_rgba8()); + + None + } + + fn save_image(&self) -> Option { + let Some(image) = self.image.as_ref() else { + return Some("no image to save".into()); + }; + + let Some(path) = FileDialog::new().save_file() else { + return Some("no path to save provided".into()); + }; + + match fs::File::create(&path) { + Err(e) => return Some(format!("failed to create file: {e}")), + Ok(file) => { + let maybe_encoder = match path.extension().map(OsStr::to_string_lossy).as_deref() { + Some("webp") => Some(WebPEncoder::new_lossless(file)), + + None | Some(_) => None, + }; + + let result = if let Some(encoder) = maybe_encoder { + image.write_with_encoder(encoder) + } else { + image.save(path) + }; + + if let Err(e) = result { + return Some(format!("failed to save image: {e}")); + } + } + }; + + None + } + fn allocate_image(&self) -> Task { let Some(image) = self.image.as_ref() else { eprintln!("no image to allocate");