1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// SPDX-License-Identifier: AGPL-3.0-only
//! WebAssembly bindings (wasm-bindgen), built with the `wasm` feature.
//!
//! ```js
//! import init, { run, version } from "./pkg/kshana.js";
//! await init();
//! const result = JSON.parse(run(tomlText));
//! console.log(version(), result.quantum.fom.integrity);
//! ```
use wasm_bindgen::prelude::*;
/// Run a scenario given as a TOML string; returns the result document as a JSON
/// string. Throws a JS error if the scenario is invalid.
#[wasm_bindgen]
pub fn run(toml: &str) -> Result<String, JsValue> {
crate::api::run_toml(toml)
.map(|o| o.json)
.map_err(|e| JsValue::from_str(&e))
}
/// Run a scenario and return its SVG chart.
#[wasm_bindgen]
pub fn chart_svg(toml: &str) -> Result<String, JsValue> {
crate::api::run_toml(toml)
.map(|o| o.svg)
.map_err(|e| JsValue::from_str(&e))
}
/// Run a scenario and return its one-line human-readable summary.
#[wasm_bindgen]
pub fn summary(toml: &str) -> Result<String, JsValue> {
crate::api::run_toml(toml)
.map(|o| o.summary)
.map_err(|e| JsValue::from_str(&e))
}
/// Run a scenario and return its reproducibility table as CSV text — the same bytes the
/// CLI writes as `<scenario>.table.csv`. Returns `undefined` for kinds that publish no
/// table (only `realtime-frame-eop`, `lunar-time-budget`, `lunar-jamming`, and
/// `moonlight-service-volume` with an export site configured emit one). Throws a JS error
/// if the scenario is invalid.
#[wasm_bindgen]
pub fn table_csv(toml: &str) -> Result<Option<String>, JsValue> {
crate::api::run_toml(toml)
.map(|o| o.csv)
.map_err(|e| JsValue::from_str(&e))
}
/// Run a scenario ONCE and return every output of that run as a JSON object string:
/// `{"json": <result document string>, "svg": <chart>, "summary": <one line>,
/// "csv": <table or null>}`. `run`, `chart_svg`, `summary` and `table_csv` each execute
/// the scenario from scratch, so a page that wants all four paid for four full engine
/// runs — about 12 s instead of 3 s for the heaviest kind in a browser. Throws a JS
/// error if the scenario is invalid.
#[wasm_bindgen]
pub fn run_all(toml: &str) -> Result<String, JsValue> {
let o = crate::api::run_toml(toml).map_err(|e| JsValue::from_str(&e))?;
Ok(serde_json::json!({
"json": o.json,
"svg": o.svg,
"summary": o.summary,
"csv": o.csv,
})
.to_string())
}
/// List the available scenario kinds and their metadata as a JSON array (name,
/// description, required and optional fields), for programmatic introspection.
#[wasm_bindgen]
pub fn list_kinds() -> String {
crate::api::list_scenario_kinds_json()
}
/// Run a scenario; on failure return the structured error *kind* tag
/// (`invalid_input`, `unsupported`, …) so the caller can branch on the failure
/// category rather than parse the message. Returns an empty string on success.
#[wasm_bindgen]
pub fn error_kind(toml: &str) -> String {
crate::api::run_scenario(toml)
.err()
.map(|e| e.kind_tag().to_string())
.unwrap_or_default()
}
/// Engine version (the crate version).
#[wasm_bindgen]
pub fn version() -> String {
env!("CARGO_PKG_VERSION").to_string()
}
/// Encode a scenario TOML into a URL-safe permalink token for a `?s=` query parameter.
#[wasm_bindgen]
pub fn encode_permalink(toml: &str) -> String {
crate::permalink::encode_scenario(toml)
}
/// Decode a permalink token back into the scenario TOML; returns an empty string if the
/// token is not valid Base64 or not valid UTF-8.
#[wasm_bindgen]
pub fn decode_permalink(token: &str) -> String {
crate::permalink::decode_scenario(token).unwrap_or_default()
}
/// Export a propagated constellation scenario as an **SP3-c** precise-ephemeris string
/// (the same artifact the CLI `--export-sp3` flag writes). Pure client-side; nothing is
/// uploaded. Throws a JS error if the scenario cannot produce an SP3 (e.g. a non-orbit kind).
#[wasm_bindgen]
pub fn export_sp3(toml: &str) -> Result<String, JsValue> {
crate::api::export_sp3(toml).map_err(|e| JsValue::from_str(&e))
}
/// Export a constellation's mean elements as a **CCSDS OMM** catalogue string (one OMM
/// message per TLE-defined satellite; the CLI `--export-omm` artifact). Pure client-side.
#[wasm_bindgen]
pub fn export_omm(toml: &str) -> Result<String, JsValue> {
crate::api::export_omm(toml).map_err(|e| JsValue::from_str(&e))
}
/// Export the velocity-carrying state as a **CCSDS OEM 2.0** ephemeris string for
/// flight-dynamics tools (GMAT / Orekit / STK; the CLI `--export-oem` artifact). Pure
/// client-side.
#[wasm_bindgen]
pub fn export_oem(toml: &str) -> Result<String, JsValue> {
crate::api::export_oem(toml).map_err(|e| JsValue::from_str(&e))
}
/// Assess a real receiver log described by a `receiver-trust` scenario (TOML text). The
/// browser has no file system, so the log (and any navigation file) must be inline as
/// `text` or `base64`. Returns a JSON object `{json, csv, svg, summary}`.
#[wasm_bindgen]
pub fn receiver_trust(toml: &str) -> Result<String, JsValue> {
let o = crate::receiver_trust::scenario::run_toml(toml).map_err(|e| JsValue::from_str(&e))?;
Ok(serde_json::json!({
"json": o.json,
"csv": o.csv,
"svg": o.svg,
"summary": o.summary,
})
.to_string())
}