logparse-pretty-print 0.1.0

pretty print tree
Documentation
use std::fmt::Debug;

use crate::Text;

use super::*;

/// The document sequence type.
#[derive(Clone, Default)]
pub struct PrettySequence<'a, T> {
    items: Vec<PrettyTree<'a, T>>,
}

impl<'a, T: Text<'a> + Debug> Debug for PrettySequence<'a, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PrettySequence").field("items", &self.items).finish()
    }
}

impl<'a, T> PrettySequence<'a, T> {
    /// Create a new sequence with the given capacity.
    pub fn new(capacity: usize) -> Self {
        Self { items: Vec::with_capacity(capacity) }
    }
    /// Create a new sequence with the given capacity.
    pub fn push<U>(&mut self, item: U)
    where
        U: Into<PrettyTree<'a, T>>,
    {
        self.items.push(item.into());
    }
    /// Create a new sequence with the given capacity.
    pub fn extend<I, S>(&mut self, items: I)
    where
        I: IntoIterator<Item = S>,
        S: Into<PrettyTree<'a, T>>,
    {
        self.items.extend(items.into_iter().map(|x| x.into()));
    }
}

impl<'a, T: Text<'a>> PrettyBuilder<'a, T> for PrettySequence<'a, T> {
    fn flat_alt<E>(self, flat: E) -> PrettyTree<'a, T>
    where
        E: Into<PrettyTree<'a, T>>,
    {
        PrettyTree::from(self).flat_alt(flat)
    }
    fn indent(self, indent: usize) -> PrettyTree<'a, T> {
        PrettyTree::from(self).indent(indent)
    }

    fn nest(self, offset: isize) -> PrettyTree<'a, T> {
        PrettyTree::from(self).nest(offset)
    }
}

impl<'a, U, T> AddAssign<U> for PrettySequence<'a, T>
where
    U: Into<PrettyTree<'a, T>>,
{
    fn add_assign(&mut self, rhs: U) {
        self.push(rhs);
    }
}

impl<'a, T: Text<'a>> From<PrettySequence<'a, T>> for PrettyTree<'a, T> {
    fn from(value: PrettySequence<'a, T>) -> Self {
        Self::concat(value.items)
    }
}