diff --git a/src/bin/topola-egui/app.rs b/src/bin/topola-egui/app.rs index 4ae4176..9fa8d9e 100644 --- a/src/bin/topola-egui/app.rs +++ b/src/bin/topola-egui/app.rs @@ -1,6 +1,7 @@ use serde::{Deserialize, Serialize}; use std::{ future::Future, + io, sync::{ mpsc::{channel, Receiver, Sender}, Arc, Mutex, @@ -21,7 +22,6 @@ use crate::{ activity::{ActivityStatus, ActivityStepperWithStatus}, config::Config, error_dialog::ErrorDialog, - file_handler::FileHandlerData, layers::Layers, menu_bar::MenuBar, overlay::Overlay, @@ -250,3 +250,13 @@ pub fn execute + Send + 'static>(f: F) { pub fn execute + 'static>(f: F) { wasm_bindgen_futures::spawn_local(f); } + +pub async fn handle_file(file_handle: &rfd::FileHandle) -> io::Result { + #[cfg(not(target_arch = "wasm32"))] + let res = io::BufReader::new(std::fs::File::open(file_handle.path())?); + + #[cfg(target_arch = "wasm32")] + let res = io::Cursor::new(file_handle.read().await); + + Ok(res) +} diff --git a/src/bin/topola-egui/file_handler.rs b/src/bin/topola-egui/file_handler.rs deleted file mode 100644 index 80ef52d..0000000 --- a/src/bin/topola-egui/file_handler.rs +++ /dev/null @@ -1,61 +0,0 @@ -use std::io; -use std::sync::mpsc::{SendError, Sender}; - -#[repr(transparent)] -pub enum FileHandlerData { - #[cfg(not(target_arch = "wasm32"))] - File(io::BufReader), - - #[cfg(target_arch = "wasm32")] - Contents(io::Cursor>), -} - -macro_rules! fhd_forward { - (match $self:expr => $fname:ident($($arg:expr,)*)) => {{ - match $self { - #[cfg(not(target_arch = "wasm32"))] - FileHandlerData::File(brf) => brf.$fname($($arg,)*), - - #[cfg(target_arch = "wasm32")] - FileHandlerData::Contents(curs) => curs.$fname($($arg,)*), - } - }}; - - (fn $fname:ident(&mut self $(,$arg:ident : $argty:ty)*)) => { - #[inline] - fn $fname(&mut self $(,$arg : $argty)*) { - fhd_forward!(match self => $fname($($arg,)*)) - } - }; - (fn $fname:ident(&mut self $(,$arg:ident : $argty:ty)*) -> $ret:ty) => { - #[inline] - fn $fname(&mut self $(,$arg : $argty)*) -> $ret { - fhd_forward!(match self => $fname($($arg,)*)) - } - } -} - -impl io::Read for FileHandlerData { - fhd_forward!(fn read(&mut self, buf: &mut [u8]) -> io::Result); - fhd_forward!(fn read_to_end(&mut self, buf: &mut Vec) -> io::Result); - fhd_forward!(fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result); -} - -impl io::BufRead for FileHandlerData { - fhd_forward!(fn fill_buf(&mut self) -> io::Result<&[u8]>); - fhd_forward!(fn consume(&mut self, amt: usize)); -} - -pub async fn handle_file(file_handle: &rfd::FileHandle, callback: C) -> Result -where - E: From, - C: FnOnce(FileHandlerData) -> Result, -{ - #[cfg(not(target_arch = "wasm32"))] - let res = FileHandlerData::File(io::BufReader::new(std::fs::File::open(file_handle.path())?)); - - #[cfg(target_arch = "wasm32")] - let res = FileHandlerData::Contents(io::Cursor::new(file_handle.read().await)); - - callback(res) -} diff --git a/src/bin/topola-egui/main.rs b/src/bin/topola-egui/main.rs index aac10cb..274e84e 100644 --- a/src/bin/topola-egui/main.rs +++ b/src/bin/topola-egui/main.rs @@ -5,7 +5,6 @@ mod activity; mod app; mod config; mod error_dialog; -mod file_handler; mod layers; mod menu_bar; mod overlay; diff --git a/src/bin/topola-egui/menu_bar.rs b/src/bin/topola-egui/menu_bar.rs index acaa069..36dd4da 100644 --- a/src/bin/topola-egui/menu_bar.rs +++ b/src/bin/topola-egui/menu_bar.rs @@ -21,8 +21,7 @@ use topola::{ use crate::{ action::{Action, Switch, Trigger}, activity::{ActivityStatus, ActivityStepperWithStatus}, - app::execute, - file_handler::{handle_file, FileHandlerData}, + app::{execute, handle_file}, overlay::Overlay, translator::Translator, viewport::Viewport, @@ -243,9 +242,11 @@ impl MenuBar { execute(async move { if let Some(file_handle) = task.await { - content_sender.send(handle_file(&file_handle, |data| { - SpecctraDesign::load(data) - }).await); + let data = handle_file(&file_handle) + .await + .map_err(Into::into) + .and_then(SpecctraDesign::load); + content_sender.send(data); ctx.request_repaint(); } }); @@ -284,13 +285,14 @@ impl MenuBar { execute(async move { if let Some(file_handle) = task.await { - history_sender.send(handle_file(&file_handle, |data| { + let data = handle_file(&file_handle).await.and_then(|data| { match serde_json::from_reader(data) { Ok(history) => Ok(Ok(history)), Err(err) if err.is_io() => Err(err.into()), Err(err) => Ok(Err(err)), } - }).await); + }); + history_sender.send(data); ctx.request_repaint(); } });