refactor: use Result with a unit type over Option

this helps convey intent, and makes it more consistent
This commit is contained in:
electria 2026-07-09 20:58:44 -07:00
commit ab24e5f10d
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs
2 changed files with 9 additions and 10 deletions

View file

@ -155,7 +155,7 @@ impl State {
} }
fn save_image(&mut self) { 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] #[must_use]

View file

@ -105,19 +105,18 @@ pub fn pick_image() -> Result<PathBuf, String> {
Ok(path) Ok(path)
} }
#[must_use] pub fn save_image(image: Option<&RgbaImage>) -> Result<(), String> {
pub fn save_image(image: Option<&RgbaImage>) -> Option<String> {
let Some(image) = image else { 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 { 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) { let mut file = match fs::File::create(&path) {
Ok(f) => f, 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() { match path.extension().map(OsStr::to_string_lossy).as_deref() {
@ -132,20 +131,20 @@ pub fn save_image(image: Option<&RgbaImage>) -> Option<String> {
match encoder.encode::<u8, u8>(&rgb_image, rgb_image.width(), rgb_image.height()) { match encoder.encode::<u8, u8>(&rgb_image, rgb_image.width(), rgb_image.height()) {
Ok(j) => j, Ok(j) => j,
Err(e) => { 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) { 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) { 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(())
} }