feat: did some stuff

This commit is contained in:
2024-04-05 18:03:23 +02:00
parent 8035b26750
commit b3780fdf96
34 changed files with 355 additions and 54 deletions

View File

@@ -0,0 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log

View File

@@ -0,0 +1,30 @@
[package]
name = "input-float"
version = "0.1.0"
authors = ["Max Richter <jim-x@web.de>"]
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.84"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
plantarium = { version = "0.1.0", path = "../../../../packages/plantarium" }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true }
[dev-dependencies]
wasm-bindgen-test = "0.3.34"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

View File

@@ -0,0 +1,5 @@
{
"scripts": {
"build": "wasm-pack build --release --out-name index --no-default-features"
}
}

View File

@@ -0,0 +1,29 @@
mod utils;
use plantarium::unwrap_float;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn get_outputs() -> Vec<String> {
vec!["float".to_string()]
}
#[wasm_bindgen]
pub fn get_id() -> String {
"float".to_string()
}
#[wasm_bindgen]
pub fn get_input_types() -> String {
utils::set_panic_hook();
r#"{
"value": { "type": "float", "value": 0.1, "internal": true }
}"#
.to_string()
}
#[wasm_bindgen]
pub fn execute(var_value: JsValue) -> f64 {
utils::set_panic_hook();
return unwrap_float(var_value);
}

View File

@@ -0,0 +1,10 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

View File

@@ -0,0 +1,13 @@
//! Test suite for the Web and headless browsers.
#![cfg(target_arch = "wasm32")]
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1 + 1, 2);
}

View File

@@ -24,13 +24,28 @@ pub fn get_input_types() -> String {
}
#[wasm_bindgen]
pub fn execute(var_op_type: u8, var_a: String, var_b: String) -> String {
pub fn execute(var_op_type: u8, var_a: JsValue, var_b: JsValue) -> String {
utils::set_panic_hook();
let a: String;
let b: String;
if var_a.is_string() {
a = unwrap_string(var_a);
} else {
a = unwrap_float(var_a).to_string();
}
if var_b.is_string() {
b = unwrap_string(var_b);
} else {
b = unwrap_float(var_b).to_string();
}
// Interpolate strings into JSON format
let json_string = format!(
r#"{{"parameter": "math", "op_type": {}, "a": {}, "b": {}}}"#,
var_op_type, var_a, var_b
var_op_type, a, b
);
json_string

View File

@@ -0,0 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log

View File

@@ -0,0 +1,30 @@
[package]
name = "output"
version = "0.1.0"
authors = ["Max Richter <jim-x@web.de>"]
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.84"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
plantarium = { version = "0.1.0", path = "../../../../packages/plantarium" }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true }
[dev-dependencies]
wasm-bindgen-test = "0.3.34"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

View File

@@ -0,0 +1,5 @@
{
"scripts": {
"build": "wasm-pack build --release --out-name index --no-default-features"
}
}

View File

@@ -0,0 +1,40 @@
mod utils;
use plantarium::unwrap_string;
use wasm_bindgen::prelude::*;
// lifted from the `console_log` example
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[wasm_bindgen]
pub fn get_outputs() -> Vec<String> {
vec![]
}
#[wasm_bindgen]
pub fn get_id() -> String {
"float".to_string()
}
#[wasm_bindgen]
pub fn get_input_types() -> String {
utils::set_panic_hook();
r#"{
"input": { "type": "float", "value": 0.0, "external": true }
}"#
.to_string()
}
#[wasm_bindgen]
pub fn execute(var_value: JsValue) -> String {
utils::set_panic_hook();
let str = unwrap_string(var_value);
log(&format!("str: {}", str));
return str;
}

View File

@@ -0,0 +1,10 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

View File

@@ -0,0 +1,13 @@
//! Test suite for the Web and headless browsers.
#![cfg(target_arch = "wasm32")]
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1 + 1, 2);
}