-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
177 additions
and
18 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
tmtc-c2a/devtools_frontend/crates/wasm-interpolate/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/target | ||
**/*.rs.bk | ||
Cargo.lock | ||
bin/ | ||
pkg/ | ||
wasm-pack.log |
29 changes: 29 additions & 0 deletions
29
tmtc-c2a/devtools_frontend/crates/wasm-interpolate/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[package] | ||
name = "wasm-interpolate" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[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. | ||
console_error_panic_hook = { version = "0.1.7", optional = true } | ||
interpolator = { version = "0.5.0", features = ["number"] } | ||
js-sys = "0.3.66" | ||
anyhow = "1.0.79" | ||
|
||
[dev-dependencies] | ||
wasm-bindgen-test = "0.3.34" | ||
|
||
[profile.release] | ||
# Tell `rustc` to optimize for small code size. | ||
opt-level = "s" |
56 changes: 56 additions & 0 deletions
56
tmtc-c2a/devtools_frontend/crates/wasm-interpolate/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
mod utils; | ||
|
||
use interpolator::{format, Formattable}; | ||
use wasm_bindgen::prelude::*; | ||
|
||
use anyhow::{anyhow, Result}; | ||
use js_sys::BigInt; | ||
use std::collections::HashMap; | ||
|
||
enum Value { | ||
I64(i64), | ||
F64(f64), | ||
String(String), | ||
} | ||
|
||
impl Value { | ||
pub fn formattable(&self) -> Formattable { | ||
use Value::*; | ||
match self { | ||
I64(v) => Formattable::integer(v), | ||
F64(v) => Formattable::float(v), | ||
String(v) => Formattable::display(v), | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<&JsValue> for Value { | ||
type Error = anyhow::Error; | ||
fn try_from(value: &JsValue) -> Result<Self> { | ||
if value.is_bigint() { | ||
let value = BigInt::new(&value) | ||
.map_err(|_| anyhow!("not a bigint"))? | ||
.try_into() | ||
.map_err(|_| anyhow!("couldn't convert bigint to i64"))?; | ||
Ok(Value::I64(value)) | ||
} else if let Some(v) = value.as_f64() { | ||
Ok(Value::F64(v)) | ||
} else if let Some(s) = value.as_string() { | ||
Ok(Value::String(s)) | ||
} else { | ||
Err(anyhow!("not a string, f64, or bigint")) | ||
} | ||
} | ||
} | ||
|
||
pub fn format_value_inner(format_string: &str, arg: &JsValue) -> Result<String> { | ||
let arg = Value::try_from(arg)?; | ||
let arg = arg.formattable(); | ||
let args = HashMap::from([("value", arg)]); | ||
format(format_string, &args).map_err(Into::into) | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn format_value(format_string: &str, arg: &JsValue) -> Result<String, String> { | ||
format_value_inner(format_string, arg).map_err(|e| e.to_string()) | ||
} |
10 changes: 10 additions & 0 deletions
10
tmtc-c2a/devtools_frontend/crates/wasm-interpolate/src/utils.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters