helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
use serde_json::Value;

use super::*;

mod changes;
mod fixtures;
mod masking;
mod options;
mod secrets;

/// Joins documents into one manifest stream.
fn manifest(documents: &[&str]) -> Manifest {
    Manifest::parse(&documents.iter().fold(String::new(), |text, document| {
        format!("{text}---\n{document}\n")
    }))
    .expect("parses")
}

/// A `ConfigMap` in the default namespace carrying `data`.
fn config_map(name: &str, data: &str) -> String {
    format!("apiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: {name}\ndata:\n  key: {data}")
}

/// The diff of two lists of documents in namespace `shop`.
fn diff(before: &[&str], after: &[&str], options: &DiffOptions) -> Diff {
    Diff::between(&manifest(before), &manifest(after), "shop", options).expect("diffs")
}

/// What changed, as `(kind, key)` pairs.
fn summary(diff: &Diff) -> Vec<(ChangeKind, String)> {
    diff.changes
        .iter()
        .map(|change| (change.kind, change.id.to_string()))
        .collect()
}

/// `(path, before, after)` of every field change.
fn field_summary(change: &ObjectChange) -> Vec<(String, Option<Value>, Option<Value>)> {
    change
        .fields
        .iter()
        .map(|field| {
            (
                field.path.to_string(),
                field.before.clone(),
                field.after.clone(),
            )
        })
        .collect()
}

/// A named case: documents before, documents after, and the expected `(kind, key)` pairs.
type Case<'a> = (
    &'a str,
    &'a [&'a str],
    &'a [&'a str],
    Vec<(ChangeKind, &'a str)>,
);

/// Checks every case's change set.
fn check(table: Vec<Case<'_>>) {
    for (name, before, after, expected) in table {
        let expected: Vec<_> = expected
            .into_iter()
            .map(|(kind, key)| (kind, key.to_owned()))
            .collect();

        assert_eq!(
            summary(&diff(before, after, &DiffOptions::default())),
            expected,
            "{name}"
        );
    }
}