pretty_print/helpers/
hard_block.rs1use super::*;
2
3#[derive(Clone, Debug)]
14pub struct HardBlock {
15 pub indent: usize,
17 pub lhs: &'static str,
19 pub rhs: &'static str,
21 pub joint: PrettyTree,
23}
24
25impl HardBlock {
26 pub fn new(lhs: &'static str, rhs: &'static str) -> Self {
28 Self { lhs, rhs, indent: 4, joint: PrettyTree::line_or_space() }
29 }
30 pub fn parentheses() -> Self {
32 Self::new("(", ")")
33 }
34 pub fn brackets() -> Self {
36 Self::new("[", "]")
37 }
38 pub fn curly_braces() -> Self {
40 Self::new("{", "}")
41 }
42 pub fn with_joint(self, joint: PrettyTree) -> Self {
44 Self { joint, ..self }
45 }
46}
47
48impl HardBlock {
49 pub fn join_slice<T: PrettyPrint>(&self, slice: &[T], theme: &PrettyProvider) -> PrettyTree {
51 let mut outer = PrettySequence::new(5);
52 outer += self.lhs;
53 outer += PrettyTree::Hardline;
54 let mut inner = PrettySequence::new(slice.len() * 2);
55 for (idx, term) in slice.iter().enumerate() {
56 if idx != 0 {
57 inner += self.joint.clone();
58 }
59 inner += term.pretty(theme);
60 }
61 outer += inner.indent(self.indent);
62 outer += PrettyTree::Hardline;
63 outer += self.rhs;
64 outer.into()
65 }
66}