mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2026-10-04 12:58:23 +00:00
refactoring
This commit is contained in:
Generated
+21
-2
@@ -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",
|
||||
]
|
||||
|
||||
|
||||
+5
-13
@@ -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
|
||||
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
```
|
||||
wasm-pack build --target bundler
|
||||
wasm-pack build wasm --target bundler --out-dir ../pkg
|
||||
```
|
||||
|
||||
@@ -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};
|
||||
|
||||
/// 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,
|
||||
@@ -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 {
|
||||
@@ -1,7 +1,5 @@
|
||||
mod core;
|
||||
mod engine;
|
||||
mod wasm;
|
||||
|
||||
pub use core::*;
|
||||
pub use engine::*;
|
||||
pub use wasm::*;
|
||||
@@ -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 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! {
|
||||
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
|
||||
|
||||
#[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,
|
||||
})
|
||||
Reference in New Issue
Block a user