use wasm_bindgen::prelude::*;
use crate::graph;
use crate::profiles::ProfileRegistry;
use crate::validation;
#[wasm_bindgen(start)]
pub fn wasm_start() {
std::panic::set_hook(Box::new(|info| {
let msg = info.to_string();
log_error(&msg);
}));
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console, js_name = error)]
fn log_error(s: &str);
}
#[wasm_bindgen]
pub fn extract(html: &str) -> String {
match graph::extract_all(html) {
Ok(graph) => {
let result = serde_json::json!({
"nodes": graph.nodes,
"warnings": graph.warnings,
});
serde_json::to_string(&result)
.unwrap_or_else(|e| serde_json::json!({ "error": e.to_string() }).to_string())
}
Err(e) => serde_json::json!({ "error": e.to_string() }).to_string(),
}
}
#[wasm_bindgen]
pub fn validate_html(html: &str, profile: &str) -> String {
let graph = match graph::extract_all(html) {
Ok(g) => g,
Err(e) => return serde_json::json!({ "error": e.to_string() }).to_string(),
};
let vocab_result = validation::validate(&graph);
let registry = match profile {
"baseline" => ProfileRegistry::with_baseline(),
_ => ProfileRegistry::with_google(),
};
let profile_result = registry.evaluate(profile, &graph, &vocab_result.diagnostics);
let profile_json = match profile_result {
Ok(r) => serde_json::json!({
"eligibility": r.eligibility.to_string(),
"type_results": r.type_results,
"diagnostics": r.diagnostics,
}),
Err(e) => serde_json::json!({
"eligibility": "NotEligible",
"type_results": [],
"diagnostics": [],
"note": e.to_string(),
}),
};
let result = serde_json::json!({
"extraction": {
"nodes": graph.nodes,
"warnings": graph.warnings,
},
"validation": {
"diagnostics": vocab_result.diagnostics,
"has_errors": vocab_result.has_errors(),
},
"profile": profile_json,
});
serde_json::to_string(&result)
.unwrap_or_else(|e| serde_json::json!({ "error": e.to_string() }).to_string())
}
#[wasm_bindgen]
pub fn schema_version() -> String {
crate::vocabulary::schema_version().to_string()
}