Skip to main content

logparse_pretty_print/helpers/
k_and_r_bracket.rs

1use super::*;
2use crate::{PrettyBuilder, Text};
3
4/// `K & R` style brackets
5///
6/// ```vk
7/// a {}
8/// ```
9///
10/// ```vk
11/// a {
12///
13/// }
14/// ```
15#[derive(Copy, Clone, Debug)]
16pub struct KAndRBracket {
17    /// Whether to add a space after the keyword
18    pub head_space: bool,
19    /// The left bracket
20    pub bracket_l: &'static str,
21    /// The right bracket
22    pub bracket_r: &'static str,
23}
24
25impl KAndRBracket {
26    /// Build a bracketed block
27    pub fn curly_braces() -> Self {
28        Self { head_space: true, bracket_l: "{", bracket_r: "}" }
29    }
30    /// Build a bracketed block
31    pub fn build<'b, 'a, I, T: Text<'a>>(
32        &self,
33        items: &[I],
34        allocator: &'b PrettyProvider,
35        inline_join: PrettyTree<'a, T>,
36        block_join: PrettyTree<'a, T>,
37    ) -> PrettyTree<'a, T>
38    where
39        I: PrettyPrint<'a, T>,
40    {
41        let mut output = PrettySequence::new(5);
42        if self.head_space {
43            output.push(" ");
44        }
45        output.push(self.bracket_l);
46        // inline
47        let mut inline = PrettySequence::new(3);
48        inline.push(" ");
49        inline.push(allocator.join_slice(items, inline_join));
50        inline.push(" ");
51        // block
52        let mut block = PrettySequence::new(3);
53        block.push(PrettyTree::Hardline);
54        block.push(allocator.join_slice(items, block_join).indent(4));
55        block.push(PrettyTree::Hardline);
56        //
57        output.push(block.flat_alt(inline));
58        output.push(self.bracket_r);
59        output.into()
60    }
61}