Skip to main content

render

Function render 

Source
pub fn render(value: &Value) -> String
Expand description

Render a JSON value as a YAML document in the layer’s subset wherever the value fits it, and in a conservative quoted style wherever it does not.

The reader above is the contract this writer aims at: a document it produces from a value whose keys are identifiers and whose scalars are strings, integers and booleans parses back to the same value through parse_mapping. Values outside the subset — a key like $ref or /api/v1/peers, a floating exponent, a null — are still written, quoted so that a general YAML 1.2 parser reads them back, because the generated OpenAPI and registry documents carry them and a projection that silently dropped them would be a lie.

The style is fixed so that the output is byte-deterministic: two-space indent, block mappings and block sequences, {} and [] for the empty collections, keys bare when they are identifiers and double-quoted otherwise, and scalars plain only when reading them back cannot change their type.

use majordomus_cli::metadata::yaml::{render, parse_mapping};
use serde_json::json;
let doc = json!({ "id": "peers.list", "tags": ["peers", "coordination"], "cached": true });
assert_eq!(render(&doc), "id: peers.list\ntags:\n  - peers\n  - coordination\ncached: true\n");
assert_eq!(serde_json::Value::Object(parse_mapping(&render(&doc)).unwrap()), doc);