refactoring

This commit is contained in:
vcoppe
2026-10-03 13:56:16 +02:00
parent 17533586ef
commit 888f41e979
49 changed files with 120 additions and 66 deletions
+21 -2
View File
@@ -52,6 +52,16 @@ dependencies = [
"windows-link", "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]] [[package]]
name = "core-foundation-sys" name = "core-foundation-sys"
version = "0.8.7" version = "0.8.7"
@@ -100,13 +110,22 @@ dependencies = [
] ]
[[package]] [[package]]
name = "gpx-rs" name = "gpx-engine"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"js-sys",
"quick-xml", "quick-xml",
"uuid", "uuid",
]
[[package]]
name = "gpx-rs"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"gpx-engine",
"js-sys",
"uuid",
"wasm-bindgen", "wasm-bindgen",
] ]
+5 -13
View File
@@ -1,14 +1,6 @@
[package] [workspace]
name = "gpx-rs" members = ["engine", "wasm"]
version = "0.1.0" resolver = "3"
edition = "2024"
[lib] [profile.release]
crate-type = ["cdylib"] debug = true
[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"
+1 -1
View File
@@ -1,3 +1,3 @@
``` ```
wasm-pack build --target bundler wasm-pack build wasm --target bundler --out-dir ../pkg
``` ```
+9
View File
@@ -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"] }
@@ -1,5 +1,3 @@
use wasm_bindgen::prelude::*;
use crate::{FileId, LngLatBounds}; use crate::{FileId, LngLatBounds};
/// A user action, decoded from the WASM boundary. /// A user action, decoded from the WASM boundary.
@@ -91,14 +89,12 @@ pub enum Command<'a> {
SelectAll, SelectAll,
} }
#[wasm_bindgen]
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MergeType { pub enum MergeType {
Connect, Connect,
Group, Group,
} }
#[wasm_bindgen]
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CleanType { pub enum CleanType {
Inside, Inside,
@@ -2,15 +2,12 @@
use std::rc::Rc; use std::rc::Rc;
use js_sys::{Float64Array, Function, Int32Array};
use uuid::Uuid; use uuid::Uuid;
use crate::{ use crate::{
Command, File, FileId, Selection, Stack, StackEntry, StatisticsBuffer, StatisticsCache, parse, Command, File, FileId, Selection, Stack, StackEntry, StatisticsBuffer, StatisticsCache, parse,
}; };
extern crate console_error_panic_hook;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Engine { pub struct Engine {
stack: Stack, stack: Stack,
@@ -20,44 +17,8 @@ pub struct Engine {
} }
impl Engine { impl Engine {
pub fn total_distance(&self) -> Float64Array { pub fn statistics(&self) -> &StatisticsBuffer {
unsafe { Float64Array::view(&self.statistics_buffer.total_distance) } &self.statistics_buffer
}
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 create_file(&mut self, name: &str) -> bool { pub fn create_file(&mut self, name: &str) -> bool {
@@ -1,7 +1,5 @@
mod core; mod core;
mod engine; mod engine;
mod wasm;
pub use core::*; pub use core::*;
pub use engine::*; pub use engine::*;
pub use wasm::*;
+14
View File
@@ -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"
@@ -11,7 +11,40 @@ use std::cell::RefCell;
use wasm_bindgen::prelude::*; 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<MergeType> for engine::MergeType {
fn from(t: MergeType) -> Self {
match t {
MergeType::Connect => Self::Connect,
MergeType::Group => Self::Group,
}
}
}
impl From<CleanType> for engine::CleanType {
fn from(t: CleanType) -> Self {
match t {
CleanType::Inside => Self::Inside,
CleanType::Outside => Self::Outside,
}
}
}
thread_local! { thread_local! {
static ENGINE: RefCell<Option<Engine>> = RefCell::new(None); static ENGINE: RefCell<Option<Engine>> = 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<T>(f: impl FnOnce(&engine::StatisticsBuffer) -> T) -> Option<T> {
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 // File commands
#[wasm_bindgen] #[wasm_bindgen]
@@ -168,7 +231,9 @@ pub fn time() -> bool {
#[wasm_bindgen] #[wasm_bindgen]
pub fn merge(type_: MergeType) -> bool { pub fn merge(type_: MergeType) -> bool {
execute(Command::Merge { type_ }) execute(Command::Merge {
type_: type_.into(),
})
} }
#[wasm_bindgen] #[wasm_bindgen]
@@ -202,7 +267,7 @@ pub fn clean(
lat: north, lat: north,
}, },
}, },
type_, type_: type_.into(),
trkpt, trkpt,
wpt, wpt,
}) })