refactor: do not format each ImageError
this makes the code much nicer, but also it reduces the amount of extra fluff in the error messages.
This commit is contained in:
parent
973703726f
commit
e98f268e59
1 changed files with 10 additions and 38 deletions
48
src/utils.rs
48
src/utils.rs
|
|
@ -1,48 +1,20 @@
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use iced::widget;
|
use iced::widget;
|
||||||
use image::{DynamicImage, ImageDecoder, ImageReader, RgbaImage};
|
use image::{DynamicImage, ImageDecoder, ImageReader, ImageResult, RgbaImage};
|
||||||
use rfd::FileDialog;
|
use rfd::FileDialog;
|
||||||
|
|
||||||
pub fn load_image(path: impl AsRef<Path>) -> Result<RgbaImage, String> {
|
pub fn load_image(path: impl AsRef<Path>) -> Result<RgbaImage, String> {
|
||||||
let path = path.as_ref();
|
_load_image(path).map_err(|e| e.to_string())
|
||||||
|
}
|
||||||
|
fn _load_image(path: impl AsRef<Path>) -> ImageResult<RgbaImage> {
|
||||||
|
let mut decoder = ImageReader::open(path)?
|
||||||
|
.with_guessed_format()?
|
||||||
|
.into_decoder()?;
|
||||||
|
|
||||||
let image_reader = match ImageReader::open(path).and_then(|r| r.with_guessed_format()) {
|
let oreintation = decoder.orientation()?;
|
||||||
Ok(r) => r,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(format!(
|
|
||||||
"failed to create image reader for '{}': {e}",
|
|
||||||
path.display()
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut decoder = match image_reader.into_decoder() {
|
let mut decoded_image = DynamicImage::from_decoder(decoder)?;
|
||||||
Ok(d) => d,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(format!(
|
|
||||||
"failed to create decoder for '{}': {e}",
|
|
||||||
path.display()
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let oreintation = match decoder.orientation() {
|
|
||||||
Ok(o) => o,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(format!(
|
|
||||||
"failed to get oreintation of '{}': {e}",
|
|
||||||
path.display()
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut decoded_image = match DynamicImage::from_decoder(decoder) {
|
|
||||||
Ok(i) => i,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(format!("failed to decode image '{}': {e}", path.display()));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
decoded_image.apply_orientation(oreintation);
|
decoded_image.apply_orientation(oreintation);
|
||||||
|
|
||||||
|
|
@ -75,7 +47,7 @@ pub fn save_image(image: Option<&RgbaImage>) -> Result<(), String> {
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(e) = image.save(&path) {
|
if let Err(e) = image.save(&path) {
|
||||||
return Err(format!("failed to save '{}': {e}", path.display()));
|
return Err(e.to_string());
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue