gioser

tawasuyu/brahman

Rust 534TOML 203Markdown 58JSON 197690 Definitions

⎇ b3a99f38dcbcd3819b2358b3c54732f61baaf7a4 crates/modules/nakui/core/src/kcl_wrapper.rs Rust · 43 lines · 1.2 KB History Who touched what raw

chore: absorbe nakui (ERP matemático) en modules/nakui 4d50bfc58 · Sergio ·

1use std::path::Path;
2use std::process::Command;
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum KclError {
7    #[error("kcl binary not found on PATH (install: https://kcl-lang.io)")]
8    BinaryMissing,
9    #[error("kcl validation failed:\n{0}")]
10    ValidationFailed(String),
11    #[error("io invoking kcl: {0}")]
12    Io(#[from] std::io::Error),
13}
14
15/// Validate `state_path` (json) against a schema defined in `schema_path` (.k),
16/// targeting the named schema.
17pub fn vet(schema_path: &Path, state_path: &Path, schema_name: &str) -> Result<(), KclError> {
18    let out = match Command::new("kcl")
19        .arg("vet")
20        .arg(state_path)
21        .arg(schema_path)
22        .arg("-s")
23        .arg(schema_name)
24        .output()
25    {
26        Ok(o) => o,
27        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Err(KclError::BinaryMissing),
28        Err(e) => return Err(e.into()),
29    };
30
31    if out.status.success() {
32        Ok(())
33    } else {
34        let stderr = String::from_utf8_lossy(&out.stderr).into_owned();
35        let stdout = String::from_utf8_lossy(&out.stdout).into_owned();
36        let msg = if stderr.trim().is_empty() {
37            stdout
38        } else {
39            stderr
40        };
41        Err(KclError::ValidationFailed(msg))
42    }
43}