diff --git a/CHANGELOG.md b/CHANGELOG.md index 95ae7c97a..aabff077e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ All notable changes to this project will be documented in this file. [#724]: https://github.com/stackabletech/operator-rs/pull/724 +### Changed + +- Change Deref target of `KeyPrefix` and `KeyName` from `String` to `str` ([#725]). + +[#725]: https://github.com/stackabletech/operator-rs/pull/725 + ## [0.62.0] - 2024-01-19 ### Added diff --git a/src/kvp/key.rs b/src/kvp/key.rs index 924373401..3defc0cfb 100644 --- a/src/kvp/key.rs +++ b/src/kvp/key.rs @@ -229,7 +229,7 @@ impl FromStr for KeyPrefix { } impl Deref for KeyPrefix { - type Target = String; + type Target = str; fn deref(&self) -> &Self::Target { &self.0 @@ -247,7 +247,7 @@ where T: AsRef, { fn eq(&self, other: &T) -> bool { - self.as_str() == other.as_ref() + self.deref() == other.as_ref() } } @@ -311,7 +311,7 @@ impl FromStr for KeyName { } impl Deref for KeyName { - type Target = String; + type Target = str; fn deref(&self) -> &Self::Target { &self.0 @@ -329,15 +329,18 @@ where T: AsRef, { fn eq(&self, other: &T) -> bool { - self.as_str() == other.as_ref() + self.deref() == other.as_ref() } } #[cfg(test)] mod test { - use super::*; use rstest::rstest; + use crate::kvp::Label; + + use super::*; + #[test] fn key_with_prefix() { let key = Key::from_str("stackable.tech/vendor").unwrap(); @@ -399,4 +402,30 @@ mod test { let err = KeyName::from_str(&input).unwrap_err(); assert_eq!(err, error); } + + #[rstest] + #[case("app.kubernetes.io/name", true)] + #[case("name", false)] + fn key_prefix_deref(#[case] key: &str, #[case] expected: bool) { + let label = Label::try_from((key, "zookeeper")).unwrap(); + + let is_valid = label + .key() + .prefix() + .is_some_and(|prefix| *prefix == "app.kubernetes.io"); + + assert_eq!(is_valid, expected) + } + + #[rstest] + #[case("app.kubernetes.io/name", true)] + #[case("app.kubernetes.io/foo", false)] + #[case("name", true)] + #[case("foo", false)] + fn key_name_deref(#[case] key: &str, #[case] expected: bool) { + let label = Label::try_from((key, "zookeeper")).unwrap(); + let is_valid = *label.key().name() == "name"; + + assert_eq!(is_valid, expected); + } }