crud_pretty_struct/impls/
hashmap.rs

1use crate::{coloring, Meta, PrettyPrint};
2use miette::Result;
3use owo_colors::OwoColorize;
4use pad::PadStr;
5
6impl<K: std::fmt::Display + PadStr, V: std::fmt::Display> PrettyPrint
7  for std::collections::HashMap<K, V>
8{
9  fn meta(&self) -> Meta {
10    Meta {
11      padding: 28,
12      separator: None,
13      fields: vec![],
14    }
15  }
16  fn pretty(&self, colored: bool, prefix: Option<String>, _profile: Option<&str>) -> Result<String> {
17    let Meta {
18      separator, padding, ..
19    } = self.meta();
20
21    let separator = separator.unwrap_or("= ");
22    let prefix_ = if let Some(prefix) = &prefix {
23      if colored {
24        prefix.truecolor(80, 80, 80).to_string()
25      } else {
26        prefix.to_owned()
27      }
28    } else {
29      "".into()
30    };
31    let prefix = &prefix.unwrap_or_default();
32
33    dbg!(prefix);
34    let pretty_hashmap: String = self
35      .iter()
36      .map(|(k, v)| {
37        if colored {
38          let v = coloring(v.to_string(), &None);
39          format!("{prefix_}{}{separator}{v}\n", k.pad_to_width(padding))
40        } else {
41          format!("{prefix}{}{separator}{v}\n", k.pad_to_width(padding))
42        }
43      })
44      .collect();
45
46    Ok(pretty_hashmap)
47  }
48}