Skip to content

Commit

Permalink
Make kvp's Debug impls a lot more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
nightkr committed Oct 11, 2024
1 parent a7e70f1 commit 4a3316f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
34 changes: 30 additions & 4 deletions crates/stackable-operator/src/kvp/key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::{fmt::Display, ops::Deref, str::FromStr, sync::LazyLock};
use std::{
fmt::{Debug, Display},
ops::Deref,
str::FromStr,
sync::LazyLock,
};

use regex::Regex;
use snafu::{ensure, ResultExt, Snafu};
Expand Down Expand Up @@ -56,12 +61,21 @@ pub enum KeyError {
/// values.
///
/// [k8s-labels]: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Key {
prefix: Option<KeyPrefix>,
name: KeyName,
}

impl Debug for Key {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(prefix) = &self.prefix {
write!(f, "{:?}/", prefix)?;
}
write!(f, "{:?}", self.name)
}
}

impl FromStr for Key {
type Err = KeyError;

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

impl Debug for KeyPrefix {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.0)
}
}

impl FromStr for KeyPrefix {
type Err = KeyPrefixError;

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

impl Debug for KeyName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.0)
}
}

impl FromStr for KeyName {
type Err = KeyNameError;

Expand Down
15 changes: 13 additions & 2 deletions crates/stackable-operator/src/kvp/label/value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::{fmt::Display, ops::Deref, str::FromStr, sync::LazyLock};
use std::{
fmt::{Debug, Display},
ops::Deref,
str::FromStr,
sync::LazyLock,
};

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

impl Debug for LabelValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.0)
}
}

impl Value for LabelValue {
type Error = LabelValueError;
}
Expand Down
13 changes: 11 additions & 2 deletions crates/stackable-operator/src/kvp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! key/value pairs, like labels and annotations.
use std::{
collections::{BTreeMap, BTreeSet},
fmt::Display,
fmt::{Debug, Display},
ops::Deref,
str::FromStr,
};
Expand Down Expand Up @@ -90,7 +90,7 @@ where
///
/// - <https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/>
/// - <https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/>
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct KeyValuePair<T>
where
T: Value,
Expand All @@ -99,6 +99,15 @@ where
value: T,
}

impl<T> Debug for KeyValuePair<T>
where
T: Value + Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}: {:?}", self.key, self.value)
}
}

impl<K, V, T> TryFrom<(K, V)> for KeyValuePair<T>
where
K: AsRef<str>,
Expand Down

0 comments on commit 4a3316f

Please sign in to comment.