From 78153e8bf4b1a92a4f5a1305a5b1f5c975b2b201 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Tue, 7 Dec 2021 21:20:33 -0700 Subject: [PATCH] wip --- src/lib.rs | 17 ----------------- src/loader.rs | 39 ++++++++++++++++++++++++++++++++------- src/utils.rs | 28 ++++++++++++++++++---------- 3 files changed, 50 insertions(+), 34 deletions(-) delete mode 100644 src/lib.rs diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index de5d775..0000000 --- a/src/lib.rs +++ /dev/null @@ -1,17 +0,0 @@ -mod utils; - -// use wasm_bindgen::prelude::*; -// -// #[cfg(feature = "wee_alloc")] -// #[global_allocator] -// static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; -// -// #[wasm_bindgen] -// pub fn merge(files: js_sys::Array) -> wasm_bindgen::JsValue { -// utils::set_panic_hook(); -// let files: Vec = utils::translate_js_to_rust(files); -// let merged: gpx::Gpx = utils::join_gpx_files(files); -// let out_vec: Vec = utils::write_gpx_to_buffer(merged); -// -// JsValue::from_str(&String::from_utf8(out_vec).unwrap()) -// } diff --git a/src/loader.rs b/src/loader.rs index e81f614..6644c4b 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -9,7 +9,7 @@ use super::utils; pub enum Msg { FileLoaded(String, String), - Files(Vec), + StartLoad(Vec), FilesLoaded, Reset, } @@ -18,6 +18,9 @@ pub struct Loader { readers: HashMap, files: Vec, count: usize, + is_loading: bool, + // This field is to handle resetting the native HTML widget's state on error + field_value: &'static str, } impl Component for Loader { @@ -29,6 +32,8 @@ impl Component for Loader { readers: HashMap::default(), files: vec![], count: 0, + is_loading: false, + field_value: "", } } @@ -43,8 +48,15 @@ impl Component for Loader { true } - Msg::Files(files) => { + Msg::StartLoad(files) => { self.count = files.len(); + if self.count < 2 { + utils::alert("must load two or more files"); + ctx.link().send_message(Msg::Reset); + return true; + } + self.is_loading = true; + for file in files.into_iter() { let file_name = file.name(); let task = { @@ -64,7 +76,14 @@ impl Component for Loader { } Msg::FilesLoaded => { - let merged = utils::merge(&self.files); + let merged = match utils::merge(&self.files) { + Ok(result) => result, + Err(err) => { + utils::alert(&err.to_string()); + ctx.link().send_message(Msg::Reset); + return true + } + }; let window = web_sys::window().expect("no global `window` exists"); let document = window.document().expect("should have a document on window"); @@ -75,6 +94,8 @@ impl Component for Loader { anchor_element.set_attribute("href", &url).unwrap(); anchor_element.set_attribute("download", "merged.gpx").unwrap(); + self.is_loading = false; + let event = MouseEvent::new("click").unwrap(); anchor_element.dispatch_event(&event).unwrap(); @@ -85,6 +106,8 @@ impl Component for Loader { self.readers = HashMap::default(); self.files = vec![]; self.count = 0; + self.is_loading = false; + self.field_value = ""; true } } @@ -93,8 +116,10 @@ impl Component for Loader { fn view(&self, ctx: &Context) -> Html { let link = ctx.link(); html! { - <> - {"loading..."} + } else { + - + } } } } diff --git a/src/utils.rs b/src/utils.rs index 6b402e1..4201c17 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,12 +1,14 @@ +use std::error::Error; use web_sys::Blob; +use wasm_bindgen::prelude::*; -fn join_gpx_files(files: &Vec) -> gpx::Gpx { +fn join_gpx_files(files: &Vec) -> Result> { let mut merged_gpx: gpx::Gpx = Default::default(); let mut merged_track: gpx::Track = gpx::Track::new(); for file in files.iter() { let buffer = std::io::BufReader::new(file.as_bytes()); - let mut parsed_gpx: gpx::Gpx = gpx::read(buffer).expect("invalid gpx"); + let mut parsed_gpx: gpx::Gpx = gpx::read(buffer)?; // consolidate all track segements into one single track. for track in parsed_gpx.tracks { @@ -36,22 +38,28 @@ fn join_gpx_files(files: &Vec) -> gpx::Gpx { merged_gpx.metadata = Some(metadata); merged_gpx.version = gpx::GpxVersion::Gpx11; - merged_gpx + Ok(merged_gpx) } -fn write_gpx_to_buffer(gpx: gpx::Gpx) -> js_sys::Array { +fn write_gpx_to_buffer(gpx: gpx::Gpx) -> Result> { let mut buffer = Vec::new(); - gpx::write(&gpx, &mut buffer).unwrap(); + gpx::write(&gpx, &mut buffer)?; let uint8arr = js_sys::Uint8Array::new(&unsafe { js_sys::Uint8Array::view(&buffer) }.into()); let array = js_sys::Array::new(); array.push(&uint8arr.buffer()); - array + Ok(array) } -pub fn merge(files: &Vec) -> Blob { - let merged: gpx::Gpx = join_gpx_files(files); - let out_vec = write_gpx_to_buffer(merged); +pub fn merge(files: &Vec) -> Result> { + let merged: gpx::Gpx = join_gpx_files(files)?; + let out_vec = write_gpx_to_buffer(merged)?; + let result = Blob::new_with_u8_array_sequence(&out_vec).map_err(|e| e.as_string().unwrap() )?; - Blob::new_with_u8_array_sequence(&out_vec).unwrap() + Ok(result) +} + +#[wasm_bindgen] +extern { + pub fn alert(s: &str); }