a-b-street / osm2lanes

A common library and set of test cases for transforming OSM tags to lane specifications
https://a-b-street.github.io/osm2lanes/
Apache License 2.0
34 stars 2 forks source link

Create WASM bindings and wire up into the vanilla JS Mapbox demo #106

Closed dabreegster closed 2 years ago

dabreegster commented 2 years ago

I believe this is the minimal work needed to call the Rust library from JS. It appears to work: Screenshot from 2022-03-21 10-27-08

Still TODO: passing more options in, error handling, and an NPM package

dabreegster commented 2 years ago

Ah right, my reference was https://rustwasm.github.io/wasm-bindgen/reference/arbitrary-data-with-serde.html

dabreegster commented 2 years ago

Rebasing...

maybe, I still think the separation helps in readability and separation of concerns?

Sounds good to me. I'm still not sure what the node config looks like, or how to wire up end-to-end JS integration tests. This is just a small step in that direction

BudgieInWA commented 2 years ago

This breaks trunk serve in the rust dir for me:

~/c/osm2lanes  ➦ 098d9b7  rust  git co main
Previous HEAD position was 098d9b7 Rust benchmark (#105)
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
 ~/c/osm2lanes   main  rust  trunk serve
Mar 24 21:52:05.691  INFO 📦 starting build
Mar 24 21:52:05.692  INFO spawning asset pipelines
Mar 24 21:52:05.846  INFO copying & hashing css path="osm2lanes-web/assets/css/main.css"
Mar 24 21:52:05.846  INFO building osm2lanes-web
Mar 24 21:52:05.847  INFO finished copying & hashing css path="osm2lanes-web/assets/css/main.css"
   Compiling osm2lanes v0.1.0 (/home/ben/code/osm2lanes/rust/osm2lanes)
error[E0433]: failed to resolve: use of undeclared crate or module `wasm_bindgen`
 --> osm2lanes/src/wasm.rs:4:5
  |
4 | use wasm_bindgen::prelude::*;
  |     ^^^^^^^^^^^^ use of undeclared crate or module `wasm_bindgen`

error[E0432]: unresolved imports `crate::tags_to_lanes`, `crate::DrivingSide`, `crate::Locale`, `crate::TagsToLanesConfig`
 --> osm2lanes/src/wasm.rs:7:13
  |
7 | use crate::{tags_to_lanes, DrivingSide, Locale, TagsToLanesConfig};
  |             ^^^^^^^^^^^^^  ^^^^^^^^^^^  ^^^^^^  ^^^^^^^^^^^^^^^^^

error: cannot find attribute `wasm_bindgen` in this scope
  --> osm2lanes/src/wasm.rs:15:3
   |
15 | #[wasm_bindgen]
   |   ^^^^^^^^^^^^

error[E0433]: failed to resolve: use of undeclared crate or module `console_error_panic_hook`
  --> osm2lanes/src/wasm.rs:17:5
   |
17 |     console_error_panic_hook::set_once();
   |     ^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared crate or module `console_error_panic_hook`

error[E0433]: failed to resolve: use of undeclared type `JsValue`
  --> osm2lanes/src/wasm.rs:38:5
   |
38 |     JsValue::from_serde(&lanes).unwrap()
   |     ^^^^^^^ use of undeclared type `JsValue`

error[E0412]: cannot find type `JsValue` in this scope
  --> osm2lanes/src/wasm.rs:16:31
   |
16 | pub fn js_tags_to_lanes(val: &JsValue) -> JsValue {
   |                               ^^^^^^^ not found in this scope

error[E0412]: cannot find type `JsValue` in this scope
  --> osm2lanes/src/wasm.rs:16:43
   |
16 | pub fn js_tags_to_lanes(val: &JsValue) -> JsValue {
   |                                           ^^^^^^^ not found in this scope

Some errors have detailed explanations: E0412, E0432, E0433.
For more information about an error, try `rustc --explain E0412`.
error: could not compile `osm2lanes` due to 7 previous errors
Mar 24 21:52:06.590 ERROR ❌ error
error from HTML pipeline

Caused by:
    0: error from asset pipeline
    1: error during cargo build execution
    2: cargo call returned a bad status
Mar 24 21:52:06.591  INFO 📡 serving static assets at -> /
Mar 24 21:52:06.591  INFO 📡 server listening at 0.0.0.0:8080
dabreegster commented 2 years ago

Argh, I failed to test everything. Feel free to revert temporarily to unblock working on that, if you need to. Work and travel stuff mean I probably can't touch anything in osm2lanes until after April 6 or so. :\

droogmic commented 2 years ago

Fixed in https://github.com/a-b-street/osm2lanes/pull/110

I still don't know why it failed, probably trunk fiddling with wasm-bindgen and ony expecting it to exist in the root of the tree.