cubob/
field.rs

1use core::{
2    fmt::{Debug, Display, Formatter, Result as FmtResult},
3    format_args,
4};
5
6/// Lets to output key-value pair regarding the propagated value of output alternativeness.
7#[cfg_attr(docsrs, doc(cfg(feature = "field")))]
8pub struct Field<'a, K: ?Sized, V: ?Sized> {
9    key: &'a K,
10    val: &'a V,
11}
12
13impl<'a, K: ?Sized, V: ?Sized> Field<'a, K, V> {
14    /// Creates one [Field] examplar ready to be outputted.
15    pub fn new(key: &'a K, val: &'a V) -> Self {
16        Self { key, val }
17    }
18}
19
20impl<'a, K: Display + ?Sized, V: Display + ?Sized> Display for Field<'a, K, V> {
21    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
22        match f.alternate() {
23            true => f.write_fmt(format_args!("{}: {:#}", self.key, self.val)),
24            false => f.write_fmt(format_args!("{}: {}", self.key, self.val)),
25        }
26    }
27}
28
29impl<'a, K: Debug + ?Sized, V: Debug + ?Sized> Debug for Field<'a, K, V> {
30    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
31        match f.alternate() {
32            true => f.write_fmt(format_args!("{:?}: {:#?}", self.key, self.val)),
33            false => f.write_fmt(format_args!("{:?}: {:?}", self.key, self.val)),
34        }
35    }
36}