Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 79 additions & 18 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ members = [
"gaia-tmtc",
"gaia-ccsds-c2a",
"tmtc-c2a",
"tmtc-c2a/devtools_frontend/crates/wasm-interpolate"
]
5 changes: 3 additions & 2 deletions gaia-ccsds-c2a/src/access/tlm/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub struct FieldIter<'a> {
}

impl<'a> Iterator for FieldIter<'a> {
type Item = Result<(&'a str, FieldSchema)>;
type Item = Result<(&'a str, &'a tlmdb::DisplayInfo, FieldSchema)>;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [clippy] reported by reviewdog 🐶
cannot find type DisplayInfo in module tlmdb


fn next(&mut self) -> Option<Self::Item> {
let (obs, field, bit_range) = self.fields.next()?;
Expand All @@ -131,10 +131,11 @@ fn build_field_schema(
obs: tlmdb::OnboardSoftwareInfo,
field: &tlmdb::Field,
bit_range: Range<usize>,
) -> Result<(&str, FieldSchema)> {
) -> Result<(&str, &tlmdb::DisplayInfo, FieldSchema)> {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [clippy] reported by reviewdog 🐶
cannot find type DisplayInfo in module tlmdb

let converter = build_integral_converter(&field.conversion_info);
Ok((
&field.name,
&field.display_info,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [clippy] reported by reviewdog 🐶
no field display_info on type &tlmcmddb::tlm::Field

match obs.variable_type {
tlmdb::VariableType::Int8 => FieldSchema::Integral(IntegralFieldSchema {
converter,
Expand Down
24 changes: 24 additions & 0 deletions tmtc-c2a/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
use std::process::Command;
use std::{env, path::PathBuf};

fn wasm_packages_root() -> PathBuf {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let wasm_outdir = out_dir.join("wasm_packages");
wasm_outdir
}

fn wasm_pack(name: &str) {
let pkg_outdir = wasm_packages_root().join(name).join("pkg");
let status = Command::new("yarn")
.current_dir("devtools_frontend")
.arg("run")
.arg("crate")
.arg(name)
.arg("--out-dir")
.arg(&pkg_outdir)
.status()
.expect("failed to build frontend");
assert!(status.success());
}

fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
tonic_build::configure()
Expand All @@ -15,8 +35,12 @@ fn main() {
.status()
.expect("failed to build frontend");
assert!(status.success());

wasm_pack("wasm-interpolate");
let devtools_out_dir = out_dir.join("devtools_dist");
let status = Command::new("yarn")
// vite.config.ts にwasmのビルド場所を教えるために環境変数を渡す
.envs([("DEVTOOLS_CRATE_ROOT", wasm_packages_root())])
.current_dir("devtools_frontend")
.arg("run")
.arg("build:vite")
Expand Down
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 tmtc-c2a/devtools_frontend/crates/wasm-interpolate/Cargo.toml
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"

[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 tmtc-c2a/devtools_frontend/crates/wasm-interpolate/src/lib.rs
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(js_name = formatValue)]
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 tmtc-c2a/devtools_frontend/crates/wasm-interpolate/src/utils.rs
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();
}
8 changes: 7 additions & 1 deletion tmtc-c2a/devtools_frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
"codegen:proto:tmtc_generic_c2a": "protoc --ts_out src/proto --proto_path ../../tmtc-c2a/proto ../../tmtc-c2a/proto/tmtc_generic_c2a.proto",
"codegen:proto": "run-p codegen:proto:*",
"codegen": "run-s codegen:proto",
"crate:build": "cd crates && wasm-pack build --target web --release",
"crate:dev": "cd crates && cargo watch -s 'wasm-pack build --target web --dev' -C",
"crate": "yarn crate:${MODE:-build}",
"crates:wasm-interpolate": "yarn crate wasm-interpolate",
"dev:crates": "MODE=dev run-p crates:*",
"dev:vite": "vite --host",
"dev": "run-p dev:*",
"build:crates": "run-s crates:*",
"build:vite": "vite build",
"build": "run-s build:vite",
"build": "run-s build:crates build:vite",
"typecheck": "tsc",
"lint:prettier": "prettier . --check",
"lint:eslint": "eslint . --format stylish",
Expand Down
Loading