feat: capture errors into the field
This commit is contained in:
parent
ad0b37d0c3
commit
549c478a8c
1 changed files with 64 additions and 53 deletions
117
src/main.rs
117
src/main.rs
|
|
@ -86,28 +86,7 @@ impl State {
|
||||||
}
|
}
|
||||||
|
|
||||||
Key::Character("o") => {
|
Key::Character("o") => {
|
||||||
let Some(path) = FileDialog::new()
|
self.error = self.open_image();
|
||||||
.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(),
|
|
||||||
);
|
|
||||||
|
|
||||||
return self.allocate_image();
|
return self.allocate_image();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -129,37 +108,7 @@ impl State {
|
||||||
}
|
}
|
||||||
|
|
||||||
Key::Character("s") => {
|
Key::Character("s") => {
|
||||||
let Some(image) = self.image.as_ref() else {
|
self.error = self.save_image();
|
||||||
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}"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Key::Character("q") => return window::latest().and_then(window::close),
|
Key::Character("q") => return window::latest().and_then(window::close),
|
||||||
|
|
@ -175,6 +124,68 @@ impl State {
|
||||||
Task::none()
|
Task::none()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn open_image(&mut self) -> Option<String> {
|
||||||
|
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<String> {
|
||||||
|
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<Message> {
|
fn allocate_image(&self) -> Task<Message> {
|
||||||
let Some(image) = self.image.as_ref() else {
|
let Some(image) = self.image.as_ref() else {
|
||||||
eprintln!("no image to allocate");
|
eprintln!("no image to allocate");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue