From 888f41e9794f3814d0445776b4ce314fe1ee2d42 Mon Sep 17 00:00:00 2001 From: vcoppe Date: Sat, 3 Oct 2026 13:56:16 +0200 Subject: [PATCH] refactoring --- gpx-rs/Cargo.lock | 23 +++++- gpx-rs/Cargo.toml | 18 ++--- gpx-rs/README.md | 2 +- gpx-rs/engine/Cargo.toml | 9 +++ gpx-rs/{ => engine}/data/simple.gpx | 0 gpx-rs/{ => engine}/data/with_cad.gpx | 0 gpx-rs/{ => engine}/data/with_hr.gpx | 0 gpx-rs/{ => engine}/data/with_power_1.gpx | 0 gpx-rs/{ => engine}/data/with_power_2.gpx | 0 gpx-rs/{ => engine}/data/with_routes.gpx | 0 gpx-rs/{ => engine}/data/with_segments.gpx | 0 gpx-rs/{ => engine}/data/with_style.gpx | 0 gpx-rs/{ => engine}/data/with_surface.gpx | 0 gpx-rs/{ => engine}/data/with_temp.gpx | 0 gpx-rs/{ => engine}/data/with_time.gpx | 0 gpx-rs/{ => engine}/data/with_tracks.gpx | 0 .../data/with_tracks_and_segments.gpx | 0 gpx-rs/{ => engine}/data/with_waypoint.gpx | 0 gpx-rs/{ => engine}/src/core/algorithm/mod.rs | 0 .../src/core/algorithm/simplify.rs | 0 .../{ => engine}/src/core/algorithm/smooth.rs | 0 gpx-rs/{ => engine}/src/core/gpx/chunk.rs | 0 gpx-rs/{ => engine}/src/core/gpx/common.rs | 0 gpx-rs/{ => engine}/src/core/gpx/file.rs | 0 gpx-rs/{ => engine}/src/core/gpx/mod.rs | 0 gpx-rs/{ => engine}/src/core/gpx/segment.rs | 0 gpx-rs/{ => engine}/src/core/gpx/track.rs | 0 .../{ => engine}/src/core/gpx/trackpoint.rs | 0 gpx-rs/{ => engine}/src/core/gpx/waypoint.rs | 0 gpx-rs/{ => engine}/src/core/io/mod.rs | 0 gpx-rs/{ => engine}/src/core/io/parse.rs | 0 gpx-rs/{ => engine}/src/core/mod.rs | 0 .../{ => engine}/src/core/statistics/mod.rs | 0 .../src/core/statistics/statistics.rs | 0 .../{ => engine}/src/core/statistics/utils.rs | 0 gpx-rs/{ => engine}/src/core/utils.rs | 0 .../src/engine/command/command.rs | 4 -- gpx-rs/{ => engine}/src/engine/command/mod.rs | 0 gpx-rs/{ => engine}/src/engine/derived/mod.rs | 0 .../src/engine/derived/statistics_buffer.rs | 0 .../src/engine/derived/statistics_cache.rs | 0 gpx-rs/{ => engine}/src/engine/engine.rs | 43 +---------- gpx-rs/{ => engine}/src/engine/mod.rs | 0 gpx-rs/{ => engine}/src/engine/selection.rs | 0 gpx-rs/{ => engine}/src/engine/state/mod.rs | 0 gpx-rs/{ => engine}/src/engine/state/stack.rs | 0 gpx-rs/{ => engine}/src/lib.rs | 2 - gpx-rs/wasm/Cargo.toml | 14 ++++ gpx-rs/{src/wasm/mod.rs => wasm/src/lib.rs} | 71 ++++++++++++++++++- 49 files changed, 120 insertions(+), 66 deletions(-) create mode 100644 gpx-rs/engine/Cargo.toml rename gpx-rs/{ => engine}/data/simple.gpx (100%) rename gpx-rs/{ => engine}/data/with_cad.gpx (100%) rename gpx-rs/{ => engine}/data/with_hr.gpx (100%) rename gpx-rs/{ => engine}/data/with_power_1.gpx (100%) rename gpx-rs/{ => engine}/data/with_power_2.gpx (100%) rename gpx-rs/{ => engine}/data/with_routes.gpx (100%) rename gpx-rs/{ => engine}/data/with_segments.gpx (100%) rename gpx-rs/{ => engine}/data/with_style.gpx (100%) rename gpx-rs/{ => engine}/data/with_surface.gpx (100%) rename gpx-rs/{ => engine}/data/with_temp.gpx (100%) rename gpx-rs/{ => engine}/data/with_time.gpx (100%) rename gpx-rs/{ => engine}/data/with_tracks.gpx (100%) rename gpx-rs/{ => engine}/data/with_tracks_and_segments.gpx (100%) rename gpx-rs/{ => engine}/data/with_waypoint.gpx (100%) rename gpx-rs/{ => engine}/src/core/algorithm/mod.rs (100%) rename gpx-rs/{ => engine}/src/core/algorithm/simplify.rs (100%) rename gpx-rs/{ => engine}/src/core/algorithm/smooth.rs (100%) rename gpx-rs/{ => engine}/src/core/gpx/chunk.rs (100%) rename gpx-rs/{ => engine}/src/core/gpx/common.rs (100%) rename gpx-rs/{ => engine}/src/core/gpx/file.rs (100%) rename gpx-rs/{ => engine}/src/core/gpx/mod.rs (100%) rename gpx-rs/{ => engine}/src/core/gpx/segment.rs (100%) rename gpx-rs/{ => engine}/src/core/gpx/track.rs (100%) rename gpx-rs/{ => engine}/src/core/gpx/trackpoint.rs (100%) rename gpx-rs/{ => engine}/src/core/gpx/waypoint.rs (100%) rename gpx-rs/{ => engine}/src/core/io/mod.rs (100%) rename gpx-rs/{ => engine}/src/core/io/parse.rs (100%) rename gpx-rs/{ => engine}/src/core/mod.rs (100%) rename gpx-rs/{ => engine}/src/core/statistics/mod.rs (100%) rename gpx-rs/{ => engine}/src/core/statistics/statistics.rs (100%) rename gpx-rs/{ => engine}/src/core/statistics/utils.rs (100%) rename gpx-rs/{ => engine}/src/core/utils.rs (100%) rename gpx-rs/{ => engine}/src/engine/command/command.rs (96%) rename gpx-rs/{ => engine}/src/engine/command/mod.rs (100%) rename gpx-rs/{ => engine}/src/engine/derived/mod.rs (100%) rename gpx-rs/{ => engine}/src/engine/derived/statistics_buffer.rs (100%) rename gpx-rs/{ => engine}/src/engine/derived/statistics_cache.rs (100%) rename gpx-rs/{ => engine}/src/engine/engine.rs (70%) rename gpx-rs/{ => engine}/src/engine/mod.rs (100%) rename gpx-rs/{ => engine}/src/engine/selection.rs (100%) rename gpx-rs/{ => engine}/src/engine/state/mod.rs (100%) rename gpx-rs/{ => engine}/src/engine/state/stack.rs (100%) rename gpx-rs/{ => engine}/src/lib.rs (68%) create mode 100644 gpx-rs/wasm/Cargo.toml rename gpx-rs/{src/wasm/mod.rs => wasm/src/lib.rs} (73%) diff --git a/gpx-rs/Cargo.lock b/gpx-rs/Cargo.lock index 68170e416..624382ae3 100644 --- a/gpx-rs/Cargo.lock +++ b/gpx-rs/Cargo.lock @@ -52,6 +52,16 @@ dependencies = [ "windows-link", ] +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + [[package]] name = "core-foundation-sys" version = "0.8.7" @@ -100,13 +110,22 @@ dependencies = [ ] [[package]] -name = "gpx-rs" +name = "gpx-engine" version = "0.1.0" dependencies = [ "chrono", - "js-sys", "quick-xml", "uuid", +] + +[[package]] +name = "gpx-rs" +version = "0.1.0" +dependencies = [ + "console_error_panic_hook", + "gpx-engine", + "js-sys", + "uuid", "wasm-bindgen", ] diff --git a/gpx-rs/Cargo.toml b/gpx-rs/Cargo.toml index b7c4c929a..7ff8215fa 100644 --- a/gpx-rs/Cargo.toml +++ b/gpx-rs/Cargo.toml @@ -1,14 +1,6 @@ -[package] -name = "gpx-rs" -version = "0.1.0" -edition = "2024" +[workspace] +members = ["engine", "wasm"] +resolver = "3" -[lib] -crate-type = ["cdylib"] - -[dependencies] -chrono = "0.4.45" -js-sys = "0.3.106" -quick-xml = "0.42.0" -uuid = {version = "1.26.1", features = ["v4", "js"]} -wasm-bindgen = "0.2" +[profile.release] +debug = true diff --git a/gpx-rs/README.md b/gpx-rs/README.md index 447671d57..fe79689d7 100644 --- a/gpx-rs/README.md +++ b/gpx-rs/README.md @@ -1,3 +1,3 @@ ``` -wasm-pack build --target bundler +wasm-pack build wasm --target bundler --out-dir ../pkg ``` diff --git a/gpx-rs/engine/Cargo.toml b/gpx-rs/engine/Cargo.toml new file mode 100644 index 000000000..2edb870f9 --- /dev/null +++ b/gpx-rs/engine/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "gpx-engine" +version = "0.1.0" +edition = "2024" + +[dependencies] +chrono = "0.4.45" +quick-xml = "0.42.0" +uuid = { version = "1.26.1", features = ["v4"] } diff --git a/gpx-rs/data/simple.gpx b/gpx-rs/engine/data/simple.gpx similarity index 100% rename from gpx-rs/data/simple.gpx rename to gpx-rs/engine/data/simple.gpx diff --git a/gpx-rs/data/with_cad.gpx b/gpx-rs/engine/data/with_cad.gpx similarity index 100% rename from gpx-rs/data/with_cad.gpx rename to gpx-rs/engine/data/with_cad.gpx diff --git a/gpx-rs/data/with_hr.gpx b/gpx-rs/engine/data/with_hr.gpx similarity index 100% rename from gpx-rs/data/with_hr.gpx rename to gpx-rs/engine/data/with_hr.gpx diff --git a/gpx-rs/data/with_power_1.gpx b/gpx-rs/engine/data/with_power_1.gpx similarity index 100% rename from gpx-rs/data/with_power_1.gpx rename to gpx-rs/engine/data/with_power_1.gpx diff --git a/gpx-rs/data/with_power_2.gpx b/gpx-rs/engine/data/with_power_2.gpx similarity index 100% rename from gpx-rs/data/with_power_2.gpx rename to gpx-rs/engine/data/with_power_2.gpx diff --git a/gpx-rs/data/with_routes.gpx b/gpx-rs/engine/data/with_routes.gpx similarity index 100% rename from gpx-rs/data/with_routes.gpx rename to gpx-rs/engine/data/with_routes.gpx diff --git a/gpx-rs/data/with_segments.gpx b/gpx-rs/engine/data/with_segments.gpx similarity index 100% rename from gpx-rs/data/with_segments.gpx rename to gpx-rs/engine/data/with_segments.gpx diff --git a/gpx-rs/data/with_style.gpx b/gpx-rs/engine/data/with_style.gpx similarity index 100% rename from gpx-rs/data/with_style.gpx rename to gpx-rs/engine/data/with_style.gpx diff --git a/gpx-rs/data/with_surface.gpx b/gpx-rs/engine/data/with_surface.gpx similarity index 100% rename from gpx-rs/data/with_surface.gpx rename to gpx-rs/engine/data/with_surface.gpx diff --git a/gpx-rs/data/with_temp.gpx b/gpx-rs/engine/data/with_temp.gpx similarity index 100% rename from gpx-rs/data/with_temp.gpx rename to gpx-rs/engine/data/with_temp.gpx diff --git a/gpx-rs/data/with_time.gpx b/gpx-rs/engine/data/with_time.gpx similarity index 100% rename from gpx-rs/data/with_time.gpx rename to gpx-rs/engine/data/with_time.gpx diff --git a/gpx-rs/data/with_tracks.gpx b/gpx-rs/engine/data/with_tracks.gpx similarity index 100% rename from gpx-rs/data/with_tracks.gpx rename to gpx-rs/engine/data/with_tracks.gpx diff --git a/gpx-rs/data/with_tracks_and_segments.gpx b/gpx-rs/engine/data/with_tracks_and_segments.gpx similarity index 100% rename from gpx-rs/data/with_tracks_and_segments.gpx rename to gpx-rs/engine/data/with_tracks_and_segments.gpx diff --git a/gpx-rs/data/with_waypoint.gpx b/gpx-rs/engine/data/with_waypoint.gpx similarity index 100% rename from gpx-rs/data/with_waypoint.gpx rename to gpx-rs/engine/data/with_waypoint.gpx diff --git a/gpx-rs/src/core/algorithm/mod.rs b/gpx-rs/engine/src/core/algorithm/mod.rs similarity index 100% rename from gpx-rs/src/core/algorithm/mod.rs rename to gpx-rs/engine/src/core/algorithm/mod.rs diff --git a/gpx-rs/src/core/algorithm/simplify.rs b/gpx-rs/engine/src/core/algorithm/simplify.rs similarity index 100% rename from gpx-rs/src/core/algorithm/simplify.rs rename to gpx-rs/engine/src/core/algorithm/simplify.rs diff --git a/gpx-rs/src/core/algorithm/smooth.rs b/gpx-rs/engine/src/core/algorithm/smooth.rs similarity index 100% rename from gpx-rs/src/core/algorithm/smooth.rs rename to gpx-rs/engine/src/core/algorithm/smooth.rs diff --git a/gpx-rs/src/core/gpx/chunk.rs b/gpx-rs/engine/src/core/gpx/chunk.rs similarity index 100% rename from gpx-rs/src/core/gpx/chunk.rs rename to gpx-rs/engine/src/core/gpx/chunk.rs diff --git a/gpx-rs/src/core/gpx/common.rs b/gpx-rs/engine/src/core/gpx/common.rs similarity index 100% rename from gpx-rs/src/core/gpx/common.rs rename to gpx-rs/engine/src/core/gpx/common.rs diff --git a/gpx-rs/src/core/gpx/file.rs b/gpx-rs/engine/src/core/gpx/file.rs similarity index 100% rename from gpx-rs/src/core/gpx/file.rs rename to gpx-rs/engine/src/core/gpx/file.rs diff --git a/gpx-rs/src/core/gpx/mod.rs b/gpx-rs/engine/src/core/gpx/mod.rs similarity index 100% rename from gpx-rs/src/core/gpx/mod.rs rename to gpx-rs/engine/src/core/gpx/mod.rs diff --git a/gpx-rs/src/core/gpx/segment.rs b/gpx-rs/engine/src/core/gpx/segment.rs similarity index 100% rename from gpx-rs/src/core/gpx/segment.rs rename to gpx-rs/engine/src/core/gpx/segment.rs diff --git a/gpx-rs/src/core/gpx/track.rs b/gpx-rs/engine/src/core/gpx/track.rs similarity index 100% rename from gpx-rs/src/core/gpx/track.rs rename to gpx-rs/engine/src/core/gpx/track.rs diff --git a/gpx-rs/src/core/gpx/trackpoint.rs b/gpx-rs/engine/src/core/gpx/trackpoint.rs similarity index 100% rename from gpx-rs/src/core/gpx/trackpoint.rs rename to gpx-rs/engine/src/core/gpx/trackpoint.rs diff --git a/gpx-rs/src/core/gpx/waypoint.rs b/gpx-rs/engine/src/core/gpx/waypoint.rs similarity index 100% rename from gpx-rs/src/core/gpx/waypoint.rs rename to gpx-rs/engine/src/core/gpx/waypoint.rs diff --git a/gpx-rs/src/core/io/mod.rs b/gpx-rs/engine/src/core/io/mod.rs similarity index 100% rename from gpx-rs/src/core/io/mod.rs rename to gpx-rs/engine/src/core/io/mod.rs diff --git a/gpx-rs/src/core/io/parse.rs b/gpx-rs/engine/src/core/io/parse.rs similarity index 100% rename from gpx-rs/src/core/io/parse.rs rename to gpx-rs/engine/src/core/io/parse.rs diff --git a/gpx-rs/src/core/mod.rs b/gpx-rs/engine/src/core/mod.rs similarity index 100% rename from gpx-rs/src/core/mod.rs rename to gpx-rs/engine/src/core/mod.rs diff --git a/gpx-rs/src/core/statistics/mod.rs b/gpx-rs/engine/src/core/statistics/mod.rs similarity index 100% rename from gpx-rs/src/core/statistics/mod.rs rename to gpx-rs/engine/src/core/statistics/mod.rs diff --git a/gpx-rs/src/core/statistics/statistics.rs b/gpx-rs/engine/src/core/statistics/statistics.rs similarity index 100% rename from gpx-rs/src/core/statistics/statistics.rs rename to gpx-rs/engine/src/core/statistics/statistics.rs diff --git a/gpx-rs/src/core/statistics/utils.rs b/gpx-rs/engine/src/core/statistics/utils.rs similarity index 100% rename from gpx-rs/src/core/statistics/utils.rs rename to gpx-rs/engine/src/core/statistics/utils.rs diff --git a/gpx-rs/src/core/utils.rs b/gpx-rs/engine/src/core/utils.rs similarity index 100% rename from gpx-rs/src/core/utils.rs rename to gpx-rs/engine/src/core/utils.rs diff --git a/gpx-rs/src/engine/command/command.rs b/gpx-rs/engine/src/engine/command/command.rs similarity index 96% rename from gpx-rs/src/engine/command/command.rs rename to gpx-rs/engine/src/engine/command/command.rs index 055a18888..e1a53f8bc 100644 --- a/gpx-rs/src/engine/command/command.rs +++ b/gpx-rs/engine/src/engine/command/command.rs @@ -1,5 +1,3 @@ -use wasm_bindgen::prelude::*; - use crate::{FileId, LngLatBounds}; /// A user action, decoded from the WASM boundary. @@ -91,14 +89,12 @@ pub enum Command<'a> { SelectAll, } -#[wasm_bindgen] #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum MergeType { Connect, Group, } -#[wasm_bindgen] #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum CleanType { Inside, diff --git a/gpx-rs/src/engine/command/mod.rs b/gpx-rs/engine/src/engine/command/mod.rs similarity index 100% rename from gpx-rs/src/engine/command/mod.rs rename to gpx-rs/engine/src/engine/command/mod.rs diff --git a/gpx-rs/src/engine/derived/mod.rs b/gpx-rs/engine/src/engine/derived/mod.rs similarity index 100% rename from gpx-rs/src/engine/derived/mod.rs rename to gpx-rs/engine/src/engine/derived/mod.rs diff --git a/gpx-rs/src/engine/derived/statistics_buffer.rs b/gpx-rs/engine/src/engine/derived/statistics_buffer.rs similarity index 100% rename from gpx-rs/src/engine/derived/statistics_buffer.rs rename to gpx-rs/engine/src/engine/derived/statistics_buffer.rs diff --git a/gpx-rs/src/engine/derived/statistics_cache.rs b/gpx-rs/engine/src/engine/derived/statistics_cache.rs similarity index 100% rename from gpx-rs/src/engine/derived/statistics_cache.rs rename to gpx-rs/engine/src/engine/derived/statistics_cache.rs diff --git a/gpx-rs/src/engine/engine.rs b/gpx-rs/engine/src/engine/engine.rs similarity index 70% rename from gpx-rs/src/engine/engine.rs rename to gpx-rs/engine/src/engine/engine.rs index 1b4bf4790..1faac4f65 100644 --- a/gpx-rs/src/engine/engine.rs +++ b/gpx-rs/engine/src/engine/engine.rs @@ -2,15 +2,12 @@ use std::rc::Rc; -use js_sys::{Float64Array, Function, Int32Array}; use uuid::Uuid; use crate::{ Command, File, FileId, Selection, Stack, StackEntry, StatisticsBuffer, StatisticsCache, parse, }; -extern crate console_error_panic_hook; - #[derive(Debug, Default)] pub struct Engine { stack: Stack, @@ -20,44 +17,8 @@ pub struct Engine { } impl Engine { - pub fn total_distance(&self) -> Float64Array { - unsafe { Float64Array::view(&self.statistics_buffer.total_distance) } - } - - pub fn moving_distance(&self) -> Float64Array { - unsafe { Float64Array::view(&self.statistics_buffer.moving_distance) } - } - - pub fn total_time(&self) -> Int32Array { - unsafe { Int32Array::view(&self.statistics_buffer.total_time) } - } - - pub fn moving_time(&self) -> Int32Array { - unsafe { Int32Array::view(&self.statistics_buffer.moving_time) } - } - - pub fn speed(&self) -> Float64Array { - unsafe { Float64Array::view(&self.statistics_buffer.speed) } - } - - pub fn elevation_gain(&self) -> Float64Array { - unsafe { Float64Array::view(&self.statistics_buffer.elevation_gain) } - } - - pub fn elevation_loss(&self) -> Float64Array { - unsafe { Float64Array::view(&self.statistics_buffer.elevation_loss) } - } - - pub fn slope(&self) -> Float64Array { - unsafe { Float64Array::view(&self.statistics_buffer.slope) } - } - - pub fn slope_segment_slope(&self) -> Float64Array { - unsafe { Float64Array::view(&self.statistics_buffer.slope_segment_slope) } - } - - pub fn slope_segment_distance(&self) -> Float64Array { - unsafe { Float64Array::view(&self.statistics_buffer.slope_segment_distance) } + pub fn statistics(&self) -> &StatisticsBuffer { + &self.statistics_buffer } pub fn create_file(&mut self, name: &str) -> bool { diff --git a/gpx-rs/src/engine/mod.rs b/gpx-rs/engine/src/engine/mod.rs similarity index 100% rename from gpx-rs/src/engine/mod.rs rename to gpx-rs/engine/src/engine/mod.rs diff --git a/gpx-rs/src/engine/selection.rs b/gpx-rs/engine/src/engine/selection.rs similarity index 100% rename from gpx-rs/src/engine/selection.rs rename to gpx-rs/engine/src/engine/selection.rs diff --git a/gpx-rs/src/engine/state/mod.rs b/gpx-rs/engine/src/engine/state/mod.rs similarity index 100% rename from gpx-rs/src/engine/state/mod.rs rename to gpx-rs/engine/src/engine/state/mod.rs diff --git a/gpx-rs/src/engine/state/stack.rs b/gpx-rs/engine/src/engine/state/stack.rs similarity index 100% rename from gpx-rs/src/engine/state/stack.rs rename to gpx-rs/engine/src/engine/state/stack.rs diff --git a/gpx-rs/src/lib.rs b/gpx-rs/engine/src/lib.rs similarity index 68% rename from gpx-rs/src/lib.rs rename to gpx-rs/engine/src/lib.rs index 1630bdaf8..d6ea07f75 100644 --- a/gpx-rs/src/lib.rs +++ b/gpx-rs/engine/src/lib.rs @@ -1,7 +1,5 @@ mod core; mod engine; -mod wasm; pub use core::*; pub use engine::*; -pub use wasm::*; diff --git a/gpx-rs/wasm/Cargo.toml b/gpx-rs/wasm/Cargo.toml new file mode 100644 index 000000000..a233d97f7 --- /dev/null +++ b/gpx-rs/wasm/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "gpx-rs" +version = "0.1.0" +edition = "2024" + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +console_error_panic_hook = "0.1.7" +gpx-engine = { path = "../engine" } +js-sys = "0.3.106" +uuid = { version = "1.26.1", features = ["js"] } +wasm-bindgen = "0.2" diff --git a/gpx-rs/src/wasm/mod.rs b/gpx-rs/wasm/src/lib.rs similarity index 73% rename from gpx-rs/src/wasm/mod.rs rename to gpx-rs/wasm/src/lib.rs index 6dabc001e..06dceff5a 100644 --- a/gpx-rs/src/wasm/mod.rs +++ b/gpx-rs/wasm/src/lib.rs @@ -11,7 +11,40 @@ use std::cell::RefCell; use wasm_bindgen::prelude::*; -use crate::{CleanType, Command, Engine, FileId, LngLat, LngLatBounds, MergeType}; +use gpx_engine::{self as engine, Command, Engine, FileId, LngLat, LngLatBounds}; +use js_sys::{Float64Array, Int32Array}; + +#[wasm_bindgen] +#[derive(Clone, Copy)] +pub enum MergeType { + Connect, + Group, +} + +#[wasm_bindgen] +#[derive(Clone, Copy)] +pub enum CleanType { + Inside, + Outside, +} + +impl From for engine::MergeType { + fn from(t: MergeType) -> Self { + match t { + MergeType::Connect => Self::Connect, + MergeType::Group => Self::Group, + } + } +} + +impl From for engine::CleanType { + fn from(t: CleanType) -> Self { + match t { + CleanType::Inside => Self::Inside, + CleanType::Outside => Self::Outside, + } + } +} thread_local! { static ENGINE: RefCell> = RefCell::new(None); @@ -47,6 +80,36 @@ pub fn start() { }); } +// Statistics buffers +// +// These are views into wasm memory: they are invalidated by the next command (and any +// allocation), so read or copy them right away. + +fn with_stats(f: impl FnOnce(&engine::StatisticsBuffer) -> T) -> Option { + ENGINE.with(|engine| engine.borrow().as_ref().map(|e| f(e.statistics()))) +} + +macro_rules! stats_getter { + ($name:ident, $array:ident) => { + #[wasm_bindgen] + pub fn $name() -> $array { + with_stats(|s| unsafe { $array::view(&s.$name) }) + .unwrap_or_else(|| $array::new_with_length(0)) + } + }; +} + +stats_getter!(total_distance, Float64Array); +stats_getter!(moving_distance, Float64Array); +stats_getter!(total_time, Int32Array); +stats_getter!(moving_time, Int32Array); +stats_getter!(speed, Float64Array); +stats_getter!(elevation_gain, Float64Array); +stats_getter!(elevation_loss, Float64Array); +stats_getter!(slope, Float64Array); +stats_getter!(slope_segment_slope, Float64Array); +stats_getter!(slope_segment_distance, Float64Array); + // File commands #[wasm_bindgen] @@ -168,7 +231,9 @@ pub fn time() -> bool { #[wasm_bindgen] pub fn merge(type_: MergeType) -> bool { - execute(Command::Merge { type_ }) + execute(Command::Merge { + type_: type_.into(), + }) } #[wasm_bindgen] @@ -202,7 +267,7 @@ pub fn clean( lat: north, }, }, - type_, + type_: type_.into(), trkpt, wpt, })