-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.rs
43 lines (36 loc) · 1021 Bytes
/
util.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::fmt::{self, Display};
/// Pretty printing of things
pub mod pretty_print;
/// Ordered floats—scandalous!
pub mod ord_f64;
/// Completely flatten the given JSON into a list.
pub mod flatten;
/// The inverse of flatten.
pub mod fatten;
/// Pretty print an object of pretty-printable things.
pub fn fmt_object<S: Display, T: Display>(
ob: impl IntoIterator<Item = (S, T)>,
f: &mut fmt::Formatter,
) -> fmt::Result {
write!(
f,
"{{{}}}",
ob.into_iter()
.map(|(k, v)| k.to_string() + ": " + &v.to_string())
.intersperse(", ".to_string())
.collect::<String>(),
)
}
/// Pretty print an array of pretty-printable things.
pub fn fmt_array<T: Display>(xs: &[T], f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"[{}]",
xs.iter()
.map(|x| x.to_string())
.intersperse(", ".to_string())
.collect::<String>()
)
}
/// Style the given item with an off-gold like colour.
pub fn style(t: impl Display) -> String { format!("\x1b[33m{t}\x1b[0m") }