helm-rs 0.1.1

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
use jiff::Timestamp;
use serde::{Deserialize, Deserializer, de::Error};

use crate::backend::Command;
use crate::release::{Revision, Status};

/// `helm list -o json` (`pkg/cmd/list.go`). Helm's `--short`, `--no-headers` and `--time-format`
/// only change the table or the time text, which the typed output replaces, so they are not
/// fields (`--short` is the names of the result).
#[derive(Debug, Clone, Default, PartialEq, Eq, bon::Builder)]
#[builder(on(String, into), on(bool, default))]
#[allow(clippy::struct_excessive_bools, reason = "one field per Helm flag")]
pub struct List {
    /// Flag `--all-namespaces`: list across all namespaces.
    pub all_namespaces: bool,
    /// Flag `--date`: sort by release date instead of name.
    pub date: bool,
    /// Flag `--deployed`: show deployed releases.
    pub deployed: bool,
    /// Flag `--failed`: show failed releases.
    pub failed: bool,
    /// Flag `--filter`: a regular expression the release name must match.
    pub filter: Option<String>,
    /// Flag `--max`: how many releases to fetch (Helm's default is 256; 0 means the server's
    /// default).
    pub max: Option<u32>,
    /// Flag `--offset`: skip this many releases.
    pub offset: Option<u32>,
    /// Flag `--pending`: show pending releases.
    pub pending: bool,
    /// Flag `--reverse`: reverse the sort order.
    pub reverse: bool,
    /// Flag `--selector`: a label query (`k=v`, `k==v`, `k!=v`); only for the secret and configmap
    /// drivers.
    pub selector: Option<String>,
    /// Flag `--superseded`: show superseded releases.
    pub superseded: bool,
    /// Flag `--uninstalled`: show uninstalled releases (kept by `--keep-history`).
    pub uninstalled: bool,
    /// Flag `--uninstalling`: show releases being uninstalled.
    pub uninstalling: bool,
}

impl List {
    /// Lists the releases of the current namespace with every setting left to Helm.
    #[must_use]
    pub fn new() -> Self {
        Self::builder().build()
    }
}

impl Command for List {
    type Output = Vec<ListItem>;
}

/// One release of `helm list -o json` (`releaseElement`, `pkg/cmd/list.go`). Helm writes the
/// revision and the time as strings; both are parsed here.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct ListItem {
    /// The release name.
    pub name: String,
    /// The release namespace.
    pub namespace: String,
    /// The revision.
    #[serde(deserialize_with = "revision")]
    pub revision: Revision,
    /// When the release was last deployed (Go's `time.Time.String()`, written as `-` when never).
    #[serde(deserialize_with = "go_time")]
    pub updated: Option<Timestamp>,
    /// The release status.
    pub status: Status,
    /// `name-version` of the chart, or `MISSING` when the record has no chart.
    pub chart: String,
    /// The chart's application version, or `MISSING`.
    pub app_version: String,
}

/// Reads a revision written as a decimal string.
fn revision<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Revision, D::Error> {
    let text = String::deserialize(deserializer)?;

    text.parse().map(Revision).map_err(D::Error::custom)
}

/// Reads `2006-01-02 15:04:05.999999999 -0700 MST` (Go's `time.Time.String()`); the zone name is
/// dropped because the numeric offset already fixes the instant. `-` is `None`.
fn go_time<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Option<Timestamp>, D::Error> {
    let text = String::deserialize(deserializer)?;
    let mut parts = text.split_whitespace();

    if text == "-" {
        return Ok(None);
    }

    let (Some(date), Some(time), Some(offset)) = (parts.next(), parts.next(), parts.next()) else {
        return Err(D::Error::custom(format!("not a Go time: {text:?}")));
    };
    let (sign_hours, minutes) = offset.split_at(offset.len().saturating_sub(2));

    format!("{date}T{time}{sign_hours}:{minutes}")
        .parse()
        .map(Some)
        .map_err(D::Error::custom)
}