Skip to content

Commit 4a3316f

Browse files
committed
Make kvp's Debug impls a lot more readable
1 parent a7e70f1 commit 4a3316f

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use std::{fmt::Display, ops::Deref, str::FromStr, sync::LazyLock};
1+
use std::{
2+
fmt::{Debug, Display},
3+
ops::Deref,
4+
str::FromStr,
5+
sync::LazyLock,
6+
};
27

38
use regex::Regex;
49
use snafu::{ensure, ResultExt, Snafu};
@@ -56,12 +61,21 @@ pub enum KeyError {
5661
/// values.
5762
///
5863
/// [k8s-labels]: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
59-
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
64+
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
6065
pub struct Key {
6166
prefix: Option<KeyPrefix>,
6267
name: KeyName,
6368
}
6469

70+
impl Debug for Key {
71+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72+
if let Some(prefix) = &self.prefix {
73+
write!(f, "{:?}/", prefix)?;
74+
}
75+
write!(f, "{:?}", self.name)
76+
}
77+
}
78+
6579
impl FromStr for Key {
6680
type Err = KeyError;
6781

@@ -203,9 +217,15 @@ pub enum KeyPrefixError {
203217
/// [`Deref`], which enables read-only access to the inner value (a [`String`]).
204218
/// It, however, does not implement [`DerefMut`](std::ops::DerefMut) which would
205219
/// enable unvalidated mutable access to inner values.
206-
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
220+
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
207221
pub struct KeyPrefix(String);
208222

223+
impl Debug for KeyPrefix {
224+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
225+
write!(f, "{:?}", self.0)
226+
}
227+
}
228+
209229
impl FromStr for KeyPrefix {
210230
type Err = KeyPrefixError;
211231

@@ -285,9 +305,15 @@ pub enum KeyNameError {
285305
/// which enables read-only access to the inner value (a [`String`]). It,
286306
/// however, does not implement [`DerefMut`](std::ops::DerefMut) which would
287307
/// enable unvalidated mutable access to inner values.
288-
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
308+
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
289309
pub struct KeyName(String);
290310

311+
impl Debug for KeyName {
312+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
313+
write!(f, "{:?}", self.0)
314+
}
315+
}
316+
291317
impl FromStr for KeyName {
292318
type Err = KeyNameError;
293319

crates/stackable-operator/src/kvp/label/value.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use std::{fmt::Display, ops::Deref, str::FromStr, sync::LazyLock};
1+
use std::{
2+
fmt::{Debug, Display},
3+
ops::Deref,
4+
str::FromStr,
5+
sync::LazyLock,
6+
};
27

38
use regex::Regex;
49
use snafu::{ensure, Snafu};
@@ -43,9 +48,15 @@ pub enum LabelValueError {
4348
/// unvalidated mutable access to inner values.
4449
///
4550
/// [k8s-labels]: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
46-
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
51+
#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
4752
pub struct LabelValue(String);
4853

54+
impl Debug for LabelValue {
55+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56+
write!(f, "{:?}", self.0)
57+
}
58+
}
59+
4960
impl Value for LabelValue {
5061
type Error = LabelValueError;
5162
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! key/value pairs, like labels and annotations.
33
use std::{
44
collections::{BTreeMap, BTreeSet},
5-
fmt::Display,
5+
fmt::{Debug, Display},
66
ops::Deref,
77
str::FromStr,
88
};
@@ -90,7 +90,7 @@ where
9090
///
9191
/// - <https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/>
9292
/// - <https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/>
93-
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
93+
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
9494
pub struct KeyValuePair<T>
9595
where
9696
T: Value,
@@ -99,6 +99,15 @@ where
9999
value: T,
100100
}
101101

102+
impl<T> Debug for KeyValuePair<T>
103+
where
104+
T: Value + Debug,
105+
{
106+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
107+
write!(f, "{:?}: {:?}", self.key, self.value)
108+
}
109+
}
110+
102111
impl<K, V, T> TryFrom<(K, V)> for KeyValuePair<T>
103112
where
104113
K: AsRef<str>,

0 commit comments

Comments
 (0)