logparse_pretty_print/helpers/
soft_block.rs1use std::fmt::Debug;
2
3use crate::Text;
4
5use super::*;
6
7#[derive(Clone)]
18pub struct SoftBlock<'a, T> {
19 pub indent: usize,
21 pub lhs: &'static str,
23 pub rhs: &'static str,
25 pub joint: PrettyTree<'a, T>,
27 pub tail: PrettyTree<'a, T>,
29}
30
31impl<'a, T: Text<'a> + Debug> Debug for SoftBlock<'a, T> {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 f.debug_struct("SoftBlock")
34 .field("indent", &self.indent)
35 .field("lhs", &self.lhs)
36 .field("rhs", &self.rhs)
37 .field("joint", &self.joint)
38 .field("tail", &self.tail)
39 .finish()
40 }
41}
42
43impl<'a, T: Text<'a>> SoftBlock<'a, T> {
44 pub fn new(lhs: &'static str, rhs: &'static str) -> Self {
46 Self { lhs, rhs, indent: 4, joint: PrettyTree::line_or_space(), tail: PrettyTree::Nil }
47 }
48 pub fn tuple() -> Self {
50 Self::new("(", ")")
51 }
52 pub fn parentheses() -> Self {
54 Self::new("(", ")")
55 }
56 pub fn brackets() -> Self {
58 Self::new("[", "]")
59 }
60 pub fn curly_braces() -> Self {
62 Self::new("{", "}")
63 }
64 pub fn with_joint(self, joint: PrettyTree<'a, T>) -> Self {
66 Self { joint, ..self }
67 }
68}
69
70impl<'a, T: Text<'a> + Clone> SoftBlock<'a, T> {
71 pub fn join_slice<U: PrettyPrint<'a, T>>(&self, slice: &[U], theme: &PrettyProvider) -> PrettyTree<'a, T> {
73 let mut outer = PrettySequence::new(5);
74 outer += self.lhs;
75 outer += PrettyTree::line_or_space();
76 let mut inner = PrettySequence::new(slice.len() * 2);
77 for (idx, term) in slice.iter().enumerate() {
78 if idx != 0 {
79 inner += self.joint.clone();
80 }
81 inner += term.pretty(theme);
82 }
83 outer += inner.indent(self.indent);
84 outer += PrettyTree::line_or_space();
85 outer += self.rhs;
86 outer.into()
87 }
88}