Skip to content

Commit

Permalink
WASM API for LTS. #10
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Sep 8, 2023
1 parent bc1bbbf commit fcdd265
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 13 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ jobs:
cache: 'npm'
cache-dependency-path: viewer/package-lock.json

- name: Install wasm-pack
uses: jetli/[email protected]

- name: Cache WASM build
uses: actions/cache@v3
with:
path: target
key: doesnt-matter-share-everything

- name: Build web app
run: |
cd viewer
npm ci
npm run wasm
npm run build --if-present
- name: Publish
Expand Down
85 changes: 85 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion lts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ name = "lts"
version = "0.1.0"
edition = "2021"

[dependencies]
[lib]
crate-type = ["cdylib", "rlib"]

[target.'cfg(target_arch = "wasm32")'.dependencies]
serde = { version = "1.0.188", features = ["derive"] }
serde-wasm-bindgen = "0.5.0"
wasm-bindgen = "0.2.87"
2 changes: 2 additions & 0 deletions lts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ mod bike_ottawa;
mod parse;
mod speed_limit_only;
mod tags;
#[cfg(target_arch = "wasm32")]
mod wasm;

pub use bike_ottawa::bike_ottawa;
pub use speed_limit_only::speed_limit_only;
Expand Down
43 changes: 43 additions & 0 deletions lts/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use wasm_bindgen::prelude::*;

use crate::{bike_ottawa, speed_limit_only, Tags, LTS};

#[derive(Deserialize)]
struct Input {
// TODO Improve this API
method: String,
tags: HashMap<String, String>,
}

#[derive(Serialize)]
struct Output {
lts: usize,
messages: Vec<String>,
}

#[wasm_bindgen()]
pub fn calculate(input: JsValue) -> Result<JsValue, JsValue> {
let input: Input = serde_wasm_bindgen::from_value(input)?;
let mut tags = Tags::new();
for (k, v) in input.tags {
tags.insert(k, v);
}

let (lts, messages) = if input.method == "speed_limit_only" {
speed_limit_only::speed_limit_only(tags)
} else if input.method == "bike_ottawa" {
bike_ottawa::bike_ottawa(tags)
} else {
(
LTS::NotAllowed,
vec![format!("Unknown method {}", input.method)],
)
};
let result = serde_wasm_bindgen::to_value(&Output {
lts: lts.into_json(),
messages,
})?;
Ok(result)
}
125 changes: 124 additions & 1 deletion viewer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"dev": "vite",
"build": "vite build",
"wasm": "wasm-pack build --release --target web ../lts",
"preview": "vite preview",
"check": "svelte-check --tsconfig ./tsconfig.json",
"fmt": "npx prettier --write *.html src/**"
Expand All @@ -21,7 +22,8 @@
"svelte-check": "^3.4.4",
"tslib": "^2.6.0",
"typescript": "^5.1.3",
"vite": "^4.3.9"
"vite": "^4.3.9",
"vite-plugin-wasm-pack": "0.1.11"
},
"dependencies": {
"@turf/bbox": "^6.5.0",
Expand Down
Loading

0 comments on commit fcdd265

Please sign in to comment.