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
+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"
+311
View File
@@ -0,0 +1,311 @@
//! Frontier between the SvelteKit frontend and the engine.
//!
//! Conventions, chosen to keep calls cheap:
//! - Coordinates and elevations cross as `Float64Array` (`&[f64]`): one memcpy into linear
//! memory, no per-point calls. `lng`/`lat`/`ele` arrays must have the same length.
//! - File bytes cross as `Uint8Array` (`&[u8]`), strings as `&str`.
//! - File ids cross as one flat `Uint8Array` of concatenated 16-byte UUIDs (no `Array<string>`).
//! - Rectangles cross as four numbers instead of an object.
//! - Every function returns `false` when its arguments are invalid or the command did nothing.
use std::cell::RefCell;
use wasm_bindgen::prelude::*;
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! {
static ENGINE: RefCell<Option<Engine>> = RefCell::new(None);
}
fn execute(command: Command) -> bool {
ENGINE.with(|engine| match engine.borrow_mut().as_mut() {
Some(engine) => engine.execute(command),
None => false,
})
}
/// Decodes concatenated 16-byte UUIDs.
fn file_ids(bytes: &[u8]) -> Option<Vec<FileId>> {
if bytes.len() % 16 != 0 {
return None;
}
bytes
.chunks_exact(16)
.map(|chunk| uuid::Uuid::from_slice(chunk).ok().map(FileId))
.collect()
}
fn same_len(a: &[f64], b: &[f64], c: &[f64]) -> bool {
a.len() == b.len() && b.len() == c.len()
}
#[wasm_bindgen]
pub fn start() {
console_error_panic_hook::set_once();
ENGINE.with(|engine| {
*engine.borrow_mut() = Some(Engine::default());
});
}
// 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
#[wasm_bindgen]
pub fn new_file(name: &str) -> bool {
execute(Command::New { name })
}
#[wasm_bindgen]
pub fn load_file(data: &[u8]) -> bool {
execute(Command::Load { data })
}
#[wasm_bindgen]
pub fn duplicate() -> bool {
execute(Command::Duplicate)
}
#[wasm_bindgen]
pub fn delete() -> bool {
execute(Command::Delete)
}
#[wasm_bindgen]
pub fn delete_all() -> bool {
execute(Command::DeleteAll)
}
// Edit commands
#[wasm_bindgen]
pub fn metadata(name: &str, desc: &str) -> bool {
execute(Command::Metadata { name, desc })
}
#[wasm_bindgen]
pub fn style(color: Option<String>, opacity: Option<f64>, width: Option<f64>) -> bool {
execute(Command::Style {
color: color.as_deref(),
opacity,
width,
})
}
#[wasm_bindgen]
pub fn new_track() -> bool {
execute(Command::NewTrack)
}
#[wasm_bindgen]
pub fn new_track_segment() -> bool {
execute(Command::NewTrackSegment)
}
// Tools
#[wasm_bindgen]
pub fn reverse() -> bool {
execute(Command::Reverse)
}
#[wasm_bindgen]
pub fn append(lng: &[f64], lat: &[f64], ele: &[f64]) -> bool {
same_len(lng, lat, ele) && execute(Command::Append { lng, lat, ele })
}
#[wasm_bindgen]
pub fn replace(start: u32, end: u32, lng: &[f64], lat: &[f64], ele: &[f64]) -> bool {
start <= end
&& same_len(lng, lat, ele)
&& execute(Command::Replace {
start,
end,
lng,
lat,
ele,
})
}
#[wasm_bindgen]
pub fn new_waypoint(
lng: f64,
lat: f64,
ele: f64,
name: &str,
desc: &str,
icon: &str,
link: &str,
) -> bool {
execute(Command::NewWaypoint {
lng,
lat,
ele,
name,
desc,
icon,
link,
})
}
#[wasm_bindgen]
pub fn move_waypoint(lng: f64, lat: f64, ele: f64) -> bool {
execute(Command::MoveWaypoint { lng, lat, ele })
}
#[wasm_bindgen]
pub fn crop(start: u32, end: u32) -> bool {
start <= end && execute(Command::Crop { start, end })
}
#[wasm_bindgen]
pub fn split(at: u32) -> bool {
execute(Command::Split { at })
}
#[wasm_bindgen]
pub fn time() -> bool {
execute(Command::Time)
}
#[wasm_bindgen]
pub fn merge(type_: MergeType) -> bool {
execute(Command::Merge {
type_: type_.into(),
})
}
#[wasm_bindgen]
pub fn extract() -> bool {
execute(Command::Extract)
}
#[wasm_bindgen]
pub fn elevation(ele: &[f64]) -> bool {
execute(Command::Elevation { ele })
}
#[wasm_bindgen]
pub fn clean(
west: f64,
south: f64,
east: f64,
north: f64,
type_: CleanType,
trkpt: bool,
wpt: bool,
) -> bool {
execute(Command::Clean {
bounds: LngLatBounds {
sw: LngLat {
lng: west,
lat: south,
},
ne: LngLat {
lng: east,
lat: north,
},
},
type_: type_.into(),
trkpt,
wpt,
})
}
// Undo-redo
#[wasm_bindgen]
pub fn undo() -> bool {
execute(Command::Undo)
}
#[wasm_bindgen]
pub fn redo() -> bool {
execute(Command::Redo)
}
// Selection
/// `file_ids`: concatenated 16-byte UUIDs.
#[wasm_bindgen]
pub fn select(file_ids_bytes: &[u8]) -> bool {
match file_ids(file_ids_bytes) {
Some(file_ids) => execute(Command::Select { file_ids }),
None => false,
}
}
/// `file_ids`: concatenated 16-byte UUIDs.
#[wasm_bindgen]
pub fn add_select(file_ids_bytes: &[u8]) -> bool {
match file_ids(file_ids_bytes) {
Some(file_ids) => execute(Command::AddSelect { file_ids }),
None => false,
}
}
#[wasm_bindgen]
pub fn select_all() -> bool {
execute(Command::SelectAll)
}