helm-rs 0.1.0

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! The string-valued enums of the release record: statuses, hook events, hook policies, hook
//! phases and the apply method.
//!
//! Every enum carries an `Other` variant so an unlisted value read from a record is preserved and
//! written back unchanged. The default is `Other("")`, which is how Go's empty string reads.

use std::fmt;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Declares an enum whose variants map to fixed strings, plus `Other(String)` for the rest.
macro_rules! string_enum {
    ($(#[$meta:meta])* $name:ident { $($(#[$vmeta:meta])* $variant:ident => $text:literal),+ $(,)? }) => {
        $(#[$meta])*
        #[derive(Debug, Clone, PartialEq, Eq, Hash)]
        pub enum $name {
            $($(#[$vmeta])* $variant,)+
            /// A value Helm's source does not list (an empty string when the field was unset).
            Other(String),
        }

        impl $name {
            /// The string Helm writes for this value.
            #[must_use]
            pub fn as_str(&self) -> &str {
                match self {
                    $(Self::$variant => $text,)+
                    Self::Other(text) => text,
                }
            }

            /// Whether the value is the empty string (the field was unset).
            #[must_use]
            pub fn is_empty(&self) -> bool {
                self.as_str().is_empty()
            }
        }

        impl Default for $name {
            fn default() -> Self {
                Self::Other(String::new())
            }
        }

        impl From<String> for $name {
            fn from(text: String) -> Self {
                match text.as_str() {
                    $($text => Self::$variant,)+
                    _ => Self::Other(text),
                }
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str(self.as_str())
            }
        }

        impl Serialize for $name {
            fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
                serializer.serialize_str(self.as_str())
            }
        }

        impl<'de> Deserialize<'de> for $name {
            fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
                Ok(Self::from(String::deserialize(deserializer)?))
            }
        }
    };
}

string_enum! {
    /// The state of a release (`pkg/release/common/status.go`, `info.status` and the `status`
    /// storage label).
    Status {
        /// `unknown`: an uncertain state.
        Unknown => "unknown",
        /// `deployed`: pushed to Kubernetes.
        Deployed => "deployed",
        /// `uninstalled`: removed from Kubernetes (history kept).
        Uninstalled => "uninstalled",
        /// `superseded`: outdated, a newer revision exists.
        Superseded => "superseded",
        /// `failed`: not successfully deployed.
        Failed => "failed",
        /// `uninstalling`: an uninstall is underway.
        Uninstalling => "uninstalling",
        /// `pending-install`: an install is underway.
        PendingInstall => "pending-install",
        /// `pending-upgrade`: an upgrade is underway.
        PendingUpgrade => "pending-upgrade",
        /// `pending-rollback`: a rollback is underway.
        PendingRollback => "pending-rollback",
    }
}

impl Status {
    /// Whether this is one of the three `pending-*` statuses (`Status.IsPending` in Helm).
    #[must_use]
    pub fn is_pending(&self) -> bool {
        matches!(
            self,
            Self::PendingInstall | Self::PendingUpgrade | Self::PendingRollback
        )
    }
}

string_enum! {
    /// The result of the last hook run (`HookPhase` in `pkg/release/v1/hook.go`). A hook that
    /// never ran has the empty phase, `Other("")`.
    HookPhase {
        /// `Unknown`.
        Unknown => "Unknown",
        /// `Running`.
        Running => "Running",
        /// `Succeeded`.
        Succeeded => "Succeeded",
        /// `Failed`.
        Failed => "Failed",
    }
}

string_enum! {
    /// When a hook fires (`HookEvent` in `pkg/release/v1/hook.go`).
    HookEvent {
        /// `pre-install`.
        PreInstall => "pre-install",
        /// `post-install`.
        PostInstall => "post-install",
        /// `pre-delete`.
        PreDelete => "pre-delete",
        /// `post-delete`.
        PostDelete => "post-delete",
        /// `pre-upgrade`.
        PreUpgrade => "pre-upgrade",
        /// `post-upgrade`.
        PostUpgrade => "post-upgrade",
        /// `pre-rollback`.
        PreRollback => "pre-rollback",
        /// `post-rollback`.
        PostRollback => "post-rollback",
        /// `test` (the legacy `test-success` annotation maps to it).
        Test => "test",
    }
}

string_enum! {
    /// When a hook resource is deleted (`HookDeletePolicy`). Helm stores any annotation value,
    /// so unlisted values arrive as `Other`.
    DeletePolicy {
        /// `hook-succeeded`.
        HookSucceeded => "hook-succeeded",
        /// `hook-failed`.
        HookFailed => "hook-failed",
        /// `before-hook-creation`.
        BeforeHookCreation => "before-hook-creation",
    }
}

string_enum! {
    /// When hook logs are copied back (`HookOutputLogPolicy`). Unlisted values arrive as `Other`.
    OutputLogPolicy {
        /// `hook-succeeded`.
        HookSucceeded => "hook-succeeded",
        /// `hook-failed`.
        HookFailed => "hook-failed",
    }
}

string_enum! {
    /// How the release was applied (`apply_method`, Helm 4 only). A record without the field
    /// was applied client-side.
    ApplyMethod {
        /// `csa`: client-side apply.
        ClientSide => "csa",
        /// `ssa`: server-side apply.
        ServerSide => "ssa",
    }
}