refactor: improve helper function sanity

pure(er) functions are so much easier to work with
(see the bug fix in the pior commit)

there's a bit of duplicate code introduced but it's worth it
This commit is contained in:
electria 2026-07-27 21:23:59 -07:00
commit e32c18e304
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -79,9 +79,13 @@ impl State {
}
}
Key::Character("o") => {
return self.pick_and_load_image();
}
Key::Character("o") => match self.pick_and_load_image() {
Ok(task) => {
self.error = None;
return task;
}
Err(e) => self.error = Some(e),
},
Key::Character("r") => {
match self.image.as_ref() {
@ -115,9 +119,13 @@ impl State {
},
Message::Event(iced::Event::Window(window_event)) => match window_event {
window::Event::FileDropped(file) => {
return self.load_image(file);
}
window::Event::FileDropped(file) => match self.load_image(file) {
Ok(task) => {
self.error = None;
return task;
}
Err(e) => self.error = Some(e),
},
// ignore unused window events
_ => {}
@ -130,28 +138,14 @@ impl State {
Task::none()
}
#[must_use]
fn load_image(&mut self, path: impl AsRef<Path>) -> Task<Message> {
let task;
(self.error, task) = match utils::load_image(path) {
Ok(image) => {
self.image = Some(image);
(None, self.allocate_image())
}
Err(e) => (Some(e), Task::none()),
};
task
fn load_image(&mut self, path: impl AsRef<Path>) -> Result<Task<Message>, String> {
utils::load_image(path).and_then(|image| {
self.image = Some(image);
Ok(self.allocate_image())
})
}
#[must_use]
fn pick_and_load_image(&mut self) -> Task<Message> {
match utils::pick_image() {
Ok(path) => self.load_image(path),
Err(e) => {
self.error = Some(e);
Task::none()
}
}
fn pick_and_load_image(&mut self) -> Result<Task<Message>, String> {
utils::pick_image().and_then(|path| self.load_image(path))
}
fn save_image(&mut self) {