helm-rs 0.1.1

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
use std::collections::BTreeMap;

use jiff::Timestamp;
use serde::Deserialize;
use serde_json::Value;

use crate::backend::Command;
use crate::chart::Dependency;
use crate::go_json::omit_zero;
use crate::manifest::Manifest;
use crate::release::{ApplyMethod, Hook, Release, Revision, Status as ReleaseState};

/// Declares a read command: a release name, an optional revision and an output type.
macro_rules! read_command {
    ($(#[$meta:meta])* $name:ident => $output:ty) => {
        $(#[$meta])*
        #[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
        #[builder(on(String, into))]
        pub struct $name {
            /// The `RELEASE_NAME` argument.
            #[builder(start_fn)]
            pub release: String,
            /// Flag `--revision`: read this revision instead of the latest.
            pub revision: Option<Revision>,
        }

        impl $name {
            /// Reads the latest revision of `release`.
            #[must_use]
            pub fn new(release: impl Into<String>) -> Self {
                Self::builder(release).build()
            }
        }

        impl Command for $name {
            type Output = $output;
        }
    };
}

read_command! {
    /// `helm status -o json` (`pkg/cmd/status.go`): the release without its chart (Helm strips it).
    /// The resources of `info.resources` are only filled in for this machine output.
    Status => Release
}

read_command! {
    /// `helm get manifest` (`pkg/cmd/get_manifest.go`): the rendered, non-hook manifests of a
    /// release, parsed.
    GetManifest => Manifest
}

read_command! {
    /// `helm get hooks` (`pkg/cmd/get_hooks.go`): the hooks of a release, parsed from the hook
    /// manifests Helm prints. Helm prints no execution state, so `last_run` is empty; read it from
    /// [`Status`] instead.
    GetHooks => Vec<Hook>
}

read_command! {
    /// `helm get notes` (`pkg/cmd/get_notes.go`): the rendered `NOTES.txt`, empty when the chart has
    /// none.
    GetNotes => String
}

read_command! {
    /// `helm get metadata -o json` (`pkg/cmd/get_metadata.go`).
    GetMetadata => ReleaseMetadata
}

/// `helm get all` (`pkg/cmd/get_all.go`): the text `helm status --debug` prints, with the chart
/// metadata, values, hooks, manifest and notes. Helm has no JSON for it and its `--template` only
/// offers plain Go template access to `.Release` (no functions, so no JSON encoder), which makes
/// a typed result impossible; the output is the text. For structured data use [`Status`] plus
/// [`GetManifest`], [`GetHooks`], [`GetValues`] and [`GetNotes`].
#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
#[builder(on(String, into))]
pub struct GetAll {
    /// The `RELEASE_NAME` argument.
    #[builder(start_fn)]
    pub release: String,
    /// Flag `--revision`: read this revision instead of the latest.
    pub revision: Option<Revision>,
    /// Flag `--template`: a Go template evaluated with the release as `.Release`, replacing the
    /// default text.
    pub template: Option<String>,
}

impl GetAll {
    /// Reads the latest revision of `release` as Helm's default text.
    #[must_use]
    pub fn new(release: impl Into<String>) -> Self {
        Self::builder(release).build()
    }
}

impl Command for GetAll {
    type Output = String;
}

/// `helm get values -o json` (`pkg/cmd/get_values.go`): the user-supplied values, or with `all`
/// the computed ones. Helm prints `null` when no values were supplied.
#[derive(Debug, Clone, PartialEq, Eq, bon::Builder)]
#[builder(on(String, into), on(bool, default))]
pub struct GetValues {
    /// The `RELEASE_NAME` argument.
    #[builder(start_fn)]
    pub release: String,
    /// Flag `--all`: all computed values, not only the user-supplied ones.
    pub all: bool,
    /// Flag `--revision`: read this revision instead of the latest.
    pub revision: Option<Revision>,
}

impl GetValues {
    /// Reads the user-supplied values of the latest revision of `release`.
    #[must_use]
    pub fn new(release: impl Into<String>) -> Self {
        Self::builder(release).build()
    }
}

impl Command for GetValues {
    type Output = Value;
}

/// The result of `helm get metadata -o json` (`action.Metadata`, `pkg/action/get_metadata.go`).
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct ReleaseMetadata {
    /// The release name.
    pub name: String,
    /// The chart name.
    pub chart: String,
    /// The chart version.
    pub version: String,
    /// The application version of the chart.
    #[serde(rename = "appVersion", default)]
    pub app_version: String,
    /// The annotations of `Chart.yaml`.
    #[serde(default)]
    pub annotations: BTreeMap<String, String>,
    /// The labels of the release record.
    #[serde(default)]
    pub labels: BTreeMap<String, String>,
    /// The chart's dependencies.
    #[serde(default)]
    pub dependencies: Vec<Dependency>,
    /// The release namespace.
    pub namespace: String,
    /// The revision.
    pub revision: Revision,
    /// The release status.
    pub status: ReleaseState,
    /// When the release was last deployed (RFC 3339, second precision).
    #[serde(rename = "deployedAt", default, with = "omit_zero")]
    pub deployed_at: Option<Timestamp>,
    /// How the release was applied; `None` when the record has no field (client-side apply).
    #[serde(rename = "applyMethod", default)]
    pub apply_method: Option<ApplyMethod>,
}