pretty_print/helpers/
hard_block.rs

1use super::*;
2
3/// A soft block is a block that is not required to be on a new line.
4///
5/// ```vk
6/// {a, b, c}
7///
8/// {
9///     a,
10///     b,
11/// }
12/// ```
13#[derive(Clone, Debug)]
14pub struct HardBlock {
15    /// The indentation of the soft block
16    pub indent: usize,
17    /// The left hand side of the soft block
18    pub lhs: &'static str,
19    /// The right hand side of the soft block
20    pub rhs: &'static str,
21    /// The joint node of the soft block
22    pub joint: PrettyTree,
23}
24
25impl HardBlock {
26    /// Build a new soft block
27    pub fn new(lhs: &'static str, rhs: &'static str) -> Self {
28        Self { lhs, rhs, indent: 4, joint: PrettyTree::line_or_space() }
29    }
30    /// Build a new soft block with the parentheses syntax
31    pub fn parentheses() -> Self {
32        Self::new("(", ")")
33    }
34    /// Build a new soft block with the brackets syntax
35    pub fn brackets() -> Self {
36        Self::new("[", "]")
37    }
38    /// Build a new soft block with the curly braces syntax
39    pub fn curly_braces() -> Self {
40        Self::new("{", "}")
41    }
42    /// Set the joint node of the soft block
43    pub fn with_joint(self, joint: PrettyTree) -> Self {
44        Self { joint, ..self }
45    }
46}
47
48impl HardBlock {
49    /// Join a slice of pretty printables with the soft block
50    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}