Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make kvp's Debug impls a lot more readable #886

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading