pretty_print/helpers/
sequence.rs

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