Skip to content

Commit 95be0fa

Browse files
chore: Use local error type
1 parent b4779a2 commit 95be0fa

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

crates/stackable-operator/src/v2/builder/pod/container.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ use std::{
66
};
77

88
use regex::Regex;
9-
use snafu::Snafu;
9+
use snafu::{ResultExt, Snafu};
1010
use strum::{EnumDiscriminants, IntoStaticStr};
1111

1212
use crate::{
1313
attributed_string_type,
1414
builder::pod::container::{ContainerBuilder, FieldPathEnvVar},
1515
k8s_openapi::api::core::v1::{ConfigMapKeySelector, EnvVar, EnvVarSource, ObjectFieldSelector},
16-
v2::{
17-
macros::attributed_string_type,
18-
types::kubernetes::{ConfigMapKey, ConfigMapName, ContainerName},
19-
},
16+
v2::types::kubernetes::{ConfigMapKey, ConfigMapName, ContainerName},
2017
};
2118

2219
/// Pattern for an escaped dollar sign, e.g. `$$`
@@ -27,14 +24,21 @@ static ESCAPED_DOLLAR_SIGN_PATTERN: LazyLock<Regex> =
2724
static REFERENCED_ENV_VARS_PATTERN: LazyLock<Regex> =
2825
LazyLock::new(|| Regex::new(r"\$\(([^\)]+)\)").expect("should be a valid regular expression"));
2926

27+
/// Maximum recursion depth until references in environment variables are followed
28+
const ENV_VAR_DEPENDENCY_RESOLVER_MAX_RECURSION_DEPTH: usize = 10;
29+
30+
type Result<T, E = Error> = std::result::Result<T, E>;
31+
3032
#[derive(Snafu, Debug, EnumDiscriminants)]
3133
#[strum_discriminants(derive(IntoStaticStr))]
3234
pub enum Error {
3335
#[snafu(display(
3436
"invalid environment variable name: a valid environment variable name must not be empty \
3537
and must consist only of printable ASCII characters other than '='"
3638
))]
37-
ParseEnvVarName { env_var_name: String },
39+
ParseEnvVarName {
40+
source: crate::v2::macros::attributed_string_type::Error,
41+
},
3842
}
3943

4044
/// Infallible variant of [`crate::builder::pod::container::ContainerBuilder::new`]
@@ -80,8 +84,11 @@ impl EnvVarSet {
8084
/// Adds the given [`EnvVar`] to this set
8185
///
8286
/// An [`EnvVar`] with the same name is overridden.
83-
pub fn with_env_var(mut self, env_var: EnvVar) -> Result<Self, attributed_string_type::Error> {
84-
self.0.insert(EnvVarName::from_str(&env_var.name)?, env_var);
87+
pub fn with_env_var(mut self, env_var: EnvVar) -> Result<Self> {
88+
self.0.insert(
89+
EnvVarName::from_str(&env_var.name).context(ParseEnvVarNameSnafu)?,
90+
env_var,
91+
);
8592

8693
Ok(self)
8794
}
@@ -170,7 +177,8 @@ impl EnvVarSet {
170177

171178
impl From<EnvVarSet> for Vec<EnvVar> {
172179
fn from(value: EnvVarSet) -> Self {
173-
let env_var_closure = EnvVarDependencyResolver::new(&value, 10);
180+
let env_var_closure =
181+
EnvVarDependencyResolver::new(&value, ENV_VAR_DEPENDENCY_RESOLVER_MAX_RECURSION_DEPTH);
174182

175183
let mut vec: Self = value.0.values().cloned().collect();
176184
vec.sort_by_key(|env_var| env_var_closure.sort_key(env_var));

0 commit comments

Comments
 (0)