Skip to main content

helm_rs/release/
kinds.rs

1//! The string-valued enums of the release record: statuses, hook events, hook policies, hook
2//! phases and the apply method.
3//!
4//! Every enum carries an `Other` variant so an unlisted value read from a record is preserved and
5//! written back unchanged. The default is `Other("")`, which is how Go's empty string reads.
6
7use std::fmt;
8
9use serde::{Deserialize, Deserializer, Serialize, Serializer};
10
11/// Declares an enum whose variants map to fixed strings, plus `Other(String)` for the rest.
12macro_rules! string_enum {
13    ($(#[$meta:meta])* $name:ident { $($(#[$vmeta:meta])* $variant:ident => $text:literal),+ $(,)? }) => {
14        $(#[$meta])*
15        #[derive(Debug, Clone, PartialEq, Eq, Hash)]
16        pub enum $name {
17            $($(#[$vmeta])* $variant,)+
18            /// A value Helm's source does not list (an empty string when the field was unset).
19            Other(String),
20        }
21
22        impl $name {
23            /// The string Helm writes for this value.
24            #[must_use]
25            pub fn as_str(&self) -> &str {
26                match self {
27                    $(Self::$variant => $text,)+
28                    Self::Other(text) => text,
29                }
30            }
31
32            /// Whether the value is the empty string (the field was unset).
33            #[must_use]
34            pub fn is_empty(&self) -> bool {
35                self.as_str().is_empty()
36            }
37        }
38
39        impl Default for $name {
40            fn default() -> Self {
41                Self::Other(String::new())
42            }
43        }
44
45        impl From<String> for $name {
46            fn from(text: String) -> Self {
47                match text.as_str() {
48                    $($text => Self::$variant,)+
49                    _ => Self::Other(text),
50                }
51            }
52        }
53
54        impl fmt::Display for $name {
55            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56                f.write_str(self.as_str())
57            }
58        }
59
60        impl Serialize for $name {
61            fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
62                serializer.serialize_str(self.as_str())
63            }
64        }
65
66        impl<'de> Deserialize<'de> for $name {
67            fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
68                Ok(Self::from(String::deserialize(deserializer)?))
69            }
70        }
71    };
72}
73
74string_enum! {
75    /// The state of a release (`pkg/release/common/status.go`, `info.status` and the `status`
76    /// storage label).
77    Status {
78        /// `unknown`: an uncertain state.
79        Unknown => "unknown",
80        /// `deployed`: pushed to Kubernetes.
81        Deployed => "deployed",
82        /// `uninstalled`: removed from Kubernetes (history kept).
83        Uninstalled => "uninstalled",
84        /// `superseded`: outdated, a newer revision exists.
85        Superseded => "superseded",
86        /// `failed`: not successfully deployed.
87        Failed => "failed",
88        /// `uninstalling`: an uninstall is underway.
89        Uninstalling => "uninstalling",
90        /// `pending-install`: an install is underway.
91        PendingInstall => "pending-install",
92        /// `pending-upgrade`: an upgrade is underway.
93        PendingUpgrade => "pending-upgrade",
94        /// `pending-rollback`: a rollback is underway.
95        PendingRollback => "pending-rollback",
96    }
97}
98
99impl Status {
100    /// Whether this is one of the three `pending-*` statuses (`Status.IsPending` in Helm).
101    #[must_use]
102    pub fn is_pending(&self) -> bool {
103        matches!(
104            self,
105            Self::PendingInstall | Self::PendingUpgrade | Self::PendingRollback
106        )
107    }
108}
109
110string_enum! {
111    /// The result of the last hook run (`HookPhase` in `pkg/release/v1/hook.go`). A hook that
112    /// never ran has the empty phase, `Other("")`.
113    HookPhase {
114        /// `Unknown`.
115        Unknown => "Unknown",
116        /// `Running`.
117        Running => "Running",
118        /// `Succeeded`.
119        Succeeded => "Succeeded",
120        /// `Failed`.
121        Failed => "Failed",
122    }
123}
124
125string_enum! {
126    /// When a hook fires (`HookEvent` in `pkg/release/v1/hook.go`).
127    HookEvent {
128        /// `pre-install`.
129        PreInstall => "pre-install",
130        /// `post-install`.
131        PostInstall => "post-install",
132        /// `pre-delete`.
133        PreDelete => "pre-delete",
134        /// `post-delete`.
135        PostDelete => "post-delete",
136        /// `pre-upgrade`.
137        PreUpgrade => "pre-upgrade",
138        /// `post-upgrade`.
139        PostUpgrade => "post-upgrade",
140        /// `pre-rollback`.
141        PreRollback => "pre-rollback",
142        /// `post-rollback`.
143        PostRollback => "post-rollback",
144        /// `test` (the legacy `test-success` annotation maps to it).
145        Test => "test",
146    }
147}
148
149string_enum! {
150    /// When a hook resource is deleted (`HookDeletePolicy`). Helm stores any annotation value,
151    /// so unlisted values arrive as `Other`.
152    DeletePolicy {
153        /// `hook-succeeded`.
154        HookSucceeded => "hook-succeeded",
155        /// `hook-failed`.
156        HookFailed => "hook-failed",
157        /// `before-hook-creation`.
158        BeforeHookCreation => "before-hook-creation",
159    }
160}
161
162string_enum! {
163    /// When hook logs are copied back (`HookOutputLogPolicy`). Unlisted values arrive as `Other`.
164    OutputLogPolicy {
165        /// `hook-succeeded`.
166        HookSucceeded => "hook-succeeded",
167        /// `hook-failed`.
168        HookFailed => "hook-failed",
169    }
170}
171
172string_enum! {
173    /// How the release was applied (`apply_method`, Helm 4 only). A record without the field
174    /// was applied client-side.
175    ApplyMethod {
176        /// `csa`: client-side apply.
177        ClientSide => "csa",
178        /// `ssa`: server-side apply.
179        ServerSide => "ssa",
180    }
181}