Skip to main content

logparse_pretty_print/helpers/
sequence.rs

1use std::fmt::Debug;
2
3use crate::Text;
4
5use super::*;
6
7/// The document sequence type.
8#[derive(Clone, Default)]
9pub struct PrettySequence<'a, T> {
10    items: Vec<PrettyTree<'a, T>>,
11}
12
13impl<'a, T: Text<'a> + Debug> Debug for PrettySequence<'a, T> {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        f.debug_struct("PrettySequence").field("items", &self.items).finish()
16    }
17}
18
19impl<'a, T> PrettySequence<'a, T> {
20    /// Create a new sequence with the given capacity.
21    pub fn new(capacity: usize) -> Self {
22        Self { items: Vec::with_capacity(capacity) }
23    }
24    /// Create a new sequence with the given capacity.
25    pub fn push<U>(&mut self, item: U)
26    where
27        U: Into<PrettyTree<'a, T>>,
28    {
29        self.items.push(item.into());
30    }
31    /// Create a new sequence with the given capacity.
32    pub fn extend<I, S>(&mut self, items: I)
33    where
34        I: IntoIterator<Item = S>,
35        S: Into<PrettyTree<'a, T>>,
36    {
37        self.items.extend(items.into_iter().map(|x| x.into()));
38    }
39}
40
41impl<'a, T: Text<'a>> PrettyBuilder<'a, T> for PrettySequence<'a, T> {
42    fn flat_alt<E>(self, flat: E) -> PrettyTree<'a, T>
43    where
44        E: Into<PrettyTree<'a, T>>,
45    {
46        PrettyTree::from(self).flat_alt(flat)
47    }
48    fn indent(self, indent: usize) -> PrettyTree<'a, T> {
49        PrettyTree::from(self).indent(indent)
50    }
51
52    fn nest(self, offset: isize) -> PrettyTree<'a, T> {
53        PrettyTree::from(self).nest(offset)
54    }
55}
56
57impl<'a, U, T> AddAssign<U> for PrettySequence<'a, T>
58where
59    U: Into<PrettyTree<'a, T>>,
60{
61    fn add_assign(&mut self, rhs: U) {
62        self.push(rhs);
63    }
64}
65
66impl<'a, T: Text<'a>> From<PrettySequence<'a, T>> for PrettyTree<'a, T> {
67    fn from(value: PrettySequence<'a, T>) -> Self {
68        Self::concat(value.items)
69    }
70}