diff --git a/src/main.rs b/src/main.rs index 3aef49b..a691062 100644 --- a/src/main.rs +++ b/src/main.rs @@ -155,7 +155,7 @@ impl State { } fn save_image(&mut self) { - self.error = utils::save_image(self.image.as_ref()); + self.error = utils::save_image(self.image.as_ref()).err(); } #[must_use] diff --git a/src/utils.rs b/src/utils.rs index 85a5bfc..961a27d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -105,19 +105,18 @@ pub fn pick_image() -> Result { Ok(path) } -#[must_use] -pub fn save_image(image: Option<&RgbaImage>) -> Option { +pub fn save_image(image: Option<&RgbaImage>) -> Result<(), String> { let Some(image) = image else { - return Some("no image to save".into()); + return Err("no image to save".into()); }; let Some(path) = FileDialog::new().save_file() else { - return Some("no path to save provided".into()); + return Err("no path to save provided".into()); }; let mut file = match fs::File::create(&path) { Ok(f) => f, - Err(e) => return Some(format!("failed to create file '{}': {e}", path.display())), + Err(e) => return Err(format!("failed to create file '{}': {e}", path.display())), }; match path.extension().map(OsStr::to_string_lossy).as_deref() { @@ -132,20 +131,20 @@ pub fn save_image(image: Option<&RgbaImage>) -> Option { match encoder.encode::(&rgb_image, rgb_image.width(), rgb_image.height()) { Ok(j) => j, Err(e) => { - return Some(format!("failed to encode jxl '{}': {e}", path.display())); + return Err(format!("failed to encode jxl '{}': {e}", path.display())); } }; if let Err(e) = file.write(&jxl.data) { - return Some(format!("failed to write jxl to '{}': {e}", path.display())); + return Err(format!("failed to write jxl to '{}': {e}", path.display())); }; } _ => { if let Err(e) = image.save(&path) { - return Some(format!("failed to save '{}': {e}", path.display())); + return Err(format!("failed to save '{}': {e}", path.display())); }; } }; - None + Ok(()) }