Skip to main content

active_core/convert/
mod.rs

1//! Conversion between Active Value and JSON / simple map representations
2
3use crate::ir::Value;
4use serde_json::{json, Value as JsonValue};
5
6pub mod doc;
7pub mod export;
8pub mod gen;
9pub mod import;
10
11pub use doc::{collect_items, doc_to_html, doc_to_man, doc_to_markdown, DocItem};
12
13pub use export::{value_to_toml_string, value_to_yaml_string};
14pub use gen::{
15    schema_to_go, schema_to_json_schema, schema_to_openapi, schema_to_rust, schema_to_typescript,
16};
17pub use import::{
18    json_text_to_active, json_value_to_active, jsonschema_to_active, toml_text_to_active,
19    yaml_text_to_active,
20};
21
22pub fn value_to_json(v: &Value) -> JsonValue {
23    match v {
24        Value::Null => JsonValue::Null,
25        Value::Bool(b) => json!(b),
26        Value::Int(i) => json!(i),
27        Value::Float(f) => json!(f),
28        Value::String(s) => json!(s),
29        Value::DurationMs(ms) => json!({ "ms": ms }),
30        Value::Quantity(n, unit) => json!({ unit.suffix(): n }),
31        Value::Array(arr) => JsonValue::Array(arr.iter().map(value_to_json).collect()),
32        Value::Table(map) => {
33            let mut obj = serde_json::Map::new();
34            for (k, val) in map {
35                obj.insert(k.clone(), value_to_json(val));
36            }
37            JsonValue::Object(obj)
38        }
39        Value::Secret(_) => json!("***SECRET***"),
40    }
41}
42
43pub fn value_to_pretty_json(v: &Value) -> String {
44    serde_json::to_string_pretty(&value_to_json(v)).unwrap_or_else(|_| "{}".into())
45}
46
47/// Active brace-style emission (`key = value` lines plus `[table]`
48/// headers): doubles as the unit round-trip path (every suffix from
49/// `Unit::suffix()` re-parses — see `tests/unit_roundtrip.rs`), so it
50/// stays exactly as is. Real TOML interchange lives in
51/// `export::value_to_toml_string`.
52pub fn value_to_simple_toml(v: &Value, prefix: &str) -> String {
53    let mut out = String::new();
54    if let Value::Table(map) = v {
55        for (k, val) in map {
56            let path = if prefix.is_empty() {
57                k.clone()
58            } else {
59                format!("{}.{}", prefix, k)
60            };
61            match val {
62                Value::Table(_) => {
63                    out.push_str(&format!("\n[{}]\n", path));
64                    out.push_str(&value_to_simple_toml(val, &path));
65                }
66                _ => {
67                    out.push_str(&format!("{} = {}\n", k, value_to_toml_literal(val)));
68                }
69            }
70        }
71    }
72    out
73}
74
75fn value_to_toml_literal(v: &Value) -> String {
76    match v {
77        Value::Null => "null".into(),
78        Value::Bool(b) => b.to_string(),
79        Value::Int(i) => i.to_string(),
80        Value::Float(f) => f.to_string(),
81        Value::String(s) => format!("\"{}\"", s.replace('"', "\\\"")),
82        Value::DurationMs(ms) => format!("{}ms", ms),
83        Value::Quantity(n, unit) => unit.format_amount(*n),
84        Value::Secret(_) => "\"***SECRET***\"".into(),
85        Value::Array(arr) => {
86            let parts: Vec<String> = arr.iter().map(value_to_toml_literal).collect();
87            format!("[{}]", parts.join(", "))
88        }
89        Value::Table(_) => "{...}".into(),
90    }
91}