config: add canonical Decodal source core

This commit is contained in:
2026-08-13 22:13:33 +09:00
parent e47eca53a2
commit 7aa4d3067e
8 changed files with 1062 additions and 14 deletions
+15
View File
@@ -0,0 +1,15 @@
[package]
name = "config-source-wasm"
version = "0.1.0"
edition.workspace = true
license.workspace = true
publish = false
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
config-source.workspace = true
serde.workspace = true
serde-wasm-bindgen = "0.6.5"
wasm-bindgen = "0.2.105"
+51
View File
@@ -0,0 +1,51 @@
use config_source::{
ConfigTreeSnapshot, EvaluationResult, SnapshotEnvironment, ToolchainContract, VirtualPath,
};
use serde_wasm_bindgen::{from_value, to_value};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn evaluate_snapshot(snapshot: JsValue, contract: JsValue) -> Result<JsValue, JsValue> {
let snapshot: ConfigTreeSnapshot = decode(snapshot)?;
let contract: ToolchainContract = decode(contract)?;
encode(
SnapshotEnvironment::new(snapshot)
.evaluate_contract(&contract)
.map_err(|diagnostics| {
to_value(&diagnostics).unwrap_or_else(|_| JsValue::from_str("evaluation failed"))
})?,
)
}
#[wasm_bindgen]
pub fn analyze_snapshot(
snapshot: JsValue,
entrypoint: String,
source_override: Option<String>,
) -> Result<JsValue, JsValue> {
let snapshot: ConfigTreeSnapshot = decode(snapshot)?;
let entrypoint = VirtualPath::parse(entrypoint).map_err(js_error)?;
encode(SnapshotEnvironment::new(snapshot).analyze(&entrypoint, source_override.as_deref()))
}
#[wasm_bindgen]
pub fn format_source(source: String) -> Result<String, JsValue> {
SnapshotEnvironment::new(ConfigTreeSnapshot::empty())
.format(&source)
.map_err(|error| JsValue::from_str(&error))
}
fn decode<T: serde::de::DeserializeOwned>(value: JsValue) -> Result<T, JsValue> {
from_value(value).map_err(|error| JsValue::from_str(&error.to_string()))
}
fn encode<T: serde::Serialize>(value: T) -> Result<JsValue, JsValue> {
to_value(&value).map_err(|error| JsValue::from_str(&error.to_string()))
}
fn js_error(error: impl std::fmt::Display) -> JsValue {
JsValue::from_str(&error.to_string())
}
#[allow(dead_code)]
fn _assert_serializable(_: EvaluationResult) {}