helm_schema/output_pipeline/format.rs
1use std::io::Write;
2
3use serde_json::Value;
4
5use crate::error::EngineResult;
6use crate::output_pipeline::JsonOutputFormat;
7
8/// Helm refuses to load any chart file larger than 5 MiB, and a chart's
9/// `values.schema.json` counts against that limit.
10pub const HELM_MAX_CHART_FILE_BYTES: usize = 5 * 1024 * 1024;
11
12/// Serializes a schema in the requested JSON format and appends a newline.
13///
14/// Pretty output automatically falls back to compact JSON before crossing
15/// Helm's per-file size limit.
16///
17/// # Errors
18///
19/// Returns an error when JSON serialization or writing to `out` fails.
20#[tracing::instrument(skip_all, fields(format = ?format))]
21pub fn write_schema_json(
22 out: &mut impl Write,
23 schema: &Value,
24 format: JsonOutputFormat,
25) -> EngineResult<()> {
26 match format {
27 JsonOutputFormat::Compact => serde_json::to_writer(&mut *out, schema)?,
28 JsonOutputFormat::Pretty => {
29 // A schema whose pretty serialization crosses Helm's chart-file
30 // limit still fits comfortably in compact form (whitespace is
31 // most of the size at that scale), so pretty degrades to
32 // compact rather than emitting a schema the chart cannot ship.
33 let bytes = serde_json::to_vec_pretty(schema)?;
34 if bytes.len() >= HELM_MAX_CHART_FILE_BYTES {
35 serde_json::to_writer(&mut *out, schema)?;
36 } else {
37 out.write_all(&bytes)?;
38 }
39 }
40 }
41 out.write_all(b"\n")?;
42 Ok(())
43}
44
45#[cfg(test)]
46#[path = "tests/format.rs"]
47mod tests;