Skip to content

Commit 9620b45

Browse files
authored
feat(operator)!: Implement Deref for kvp::Key (#1182)
* chore(operator)!: Remove unused kvp::Key function to mutate inner values * feat(operator): Implement Deref for kvp::Key * fix: Bump rustls-webpki to 0.103.10 to negate RUSTSEC-2026-0049 * chore: Add changelog entries
1 parent a000311 commit 9620b45

File tree

3 files changed

+43
-41
lines changed

3 files changed

+43
-41
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/stackable-operator/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ All notable changes to this project will be documented in this file.
66

77
### Added
88

9+
- Implement `Deref` for `kvp::Key` to be more ergonomic to use ([#1182]).
910
- Add support for specifying a `clientAuthenticationMethod` for OIDC ([#1178]).
1011
This was originally done in [#1158] and had been reverted in [#1170].
1112

13+
### Removed
14+
15+
- BREAKING: Remove unused `add_prefix`, `try_add_prefix`, `set_name`, and `try_set_name` associated
16+
functions from `kvp::Key` to disallow mutable access to inner values ([#1182]).
17+
1218
[#1178]: https://github.com/stackabletech/operator-rs/pull/1178
19+
[#1182]: https://github.com/stackabletech/operator-rs/pull/1182
1320

1421
## [0.108.0] - 2026-03-10
1522

crates/stackable-operator/src/kvp/key.rs

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ pub enum KeyError {
5858
/// [k8s-labels]: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
5959
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
6060
pub struct Key {
61+
/// A cached and formatted representation of a [`Key`] as a [`String`], which enables the
62+
/// implementation of [`Deref`], instead of constructing a new [`String`] every time the string
63+
/// representation of a key is needed.
64+
///
65+
/// ### Safety
66+
///
67+
/// This is safe to do (and cache it), because [`Key`] doesn't provide any **mutable** access
68+
/// to the inner values.
69+
string: String,
70+
6171
prefix: Option<KeyPrefix>,
6272
name: KeyName,
6373
}
@@ -80,12 +90,22 @@ impl FromStr for Key {
8090
_ => return NestedPrefixSnafu.fail(),
8191
};
8292

93+
let prefix = prefix
94+
.map(KeyPrefix::from_str)
95+
.transpose()
96+
.context(KeyPrefixSnafu)?;
97+
98+
let name = KeyName::from_str(name).context(KeyNameSnafu)?;
99+
100+
let string = match prefix {
101+
Some(ref prefix) => format!("{prefix}/{name}"),
102+
None => format!("{name}"),
103+
};
104+
83105
let key = Self {
84-
prefix: prefix
85-
.map(KeyPrefix::from_str)
86-
.transpose()
87-
.context(KeyPrefixSnafu)?,
88-
name: KeyName::from_str(name).context(KeyNameSnafu)?,
106+
string,
107+
prefix,
108+
name,
89109
};
90110

91111
Ok(key)
@@ -102,10 +122,15 @@ impl TryFrom<&str> for Key {
102122

103123
impl Display for Key {
104124
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105-
match &self.prefix {
106-
Some(prefix) => write!(f, "{}/{}", prefix, self.name),
107-
None => write!(f, "{}", self.name),
108-
}
125+
write!(f, "{key}", key = self.string)
126+
}
127+
}
128+
129+
impl Deref for Key {
130+
type Target = str;
131+
132+
fn deref(&self) -> &Self::Target {
133+
self.string.as_str()
109134
}
110135
}
111136

@@ -126,21 +151,6 @@ impl Key {
126151
self.prefix.as_ref()
127152
}
128153

129-
/// Adds or replaces the key prefix. This takes a parsed and validated
130-
/// [`KeyPrefix`] as a parameter. If instead you want to use a raw value,
131-
/// use the [`Key::try_add_prefix()`] function instead.
132-
pub fn add_prefix(&mut self, prefix: KeyPrefix) {
133-
self.prefix = Some(prefix)
134-
}
135-
136-
/// Adds or replaces the key prefix by parsing and validation raw input. If
137-
/// instead you already have a parsed and validated [`KeyPrefix`], use the
138-
/// [`Key::add_prefix()`] function instead.
139-
pub fn try_add_prefix(&mut self, prefix: impl AsRef<str>) -> Result<&mut Self, KeyError> {
140-
self.prefix = Some(KeyPrefix::from_str(prefix.as_ref()).context(KeyPrefixSnafu)?);
141-
Ok(self)
142-
}
143-
144154
/// Retrieves the key's name.
145155
///
146156
/// ```
@@ -156,21 +166,6 @@ impl Key {
156166
pub fn name(&self) -> &KeyName {
157167
&self.name
158168
}
159-
160-
/// Sets the key name. This takes a parsed and validated [`KeyName`] as a
161-
/// parameter. If instead you want to use a raw value, use the
162-
/// [`Key::try_set_name()`] function instead.
163-
pub fn set_name(&mut self, name: KeyName) {
164-
self.name = name
165-
}
166-
167-
/// Sets the key name by parsing and validation raw input. If instead you
168-
/// already have a parsed and validated [`KeyName`], use the
169-
/// [`Key::set_name()`] function instead.
170-
pub fn try_set_name(&mut self, name: impl AsRef<str>) -> Result<&mut Self, KeyError> {
171-
self.name = KeyName::from_str(name.as_ref()).context(KeyNameSnafu)?;
172-
Ok(self)
173-
}
174169
}
175170

176171
/// The error type for key prefix parsing/validation operations.

0 commit comments

Comments
 (0)