logparse-pretty-print 0.1.0

pretty print tree
Documentation
#[cfg(feature = "std")]
use crate::Text;
use crate::{Render, RenderAnnotated};
use alloc::rc::Rc;
use color_ansi::{AnsiAbility, AnsiStyle, AnsiWriter};
use core::fmt::{Debug, Formatter};

#[cfg(feature = "std")]
use std::io::{Error, Write};

/// Represents a terminal writer.
pub struct TerminalWriter<W> {
    color_stack: Vec<Rc<AnsiStyle>>,
    upstream: AnsiWriter<W>,
}

/// Writes to something implementing `std::io::Write`
pub struct IoWrite<W> {
    upstream: W,
}

impl<W> Debug for IoWrite<W> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("IoWrite").finish()
    }
}

impl<W> IoWrite<W> {
    /// Creates a new terminal writer.
    pub fn new(upstream: W) -> IoWrite<W> {
        IoWrite { upstream }
    }
}

#[cfg(feature = "std")]
impl<'a, W, T: Text<'a>> Render<'a, T> for IoWrite<W>
where
    W: std::io::Write,
{
    type Error = std::io::Error;

    fn write_all(&mut self, s: &[T]) -> std::io::Result<()> {
        for i in s {
            self.upstream.write_all(i.as_str().as_bytes())?;
        }

        Ok(())
    }

    fn fail_doc(&self) -> Self::Error {
        std::io::Error::other("Document failed to render")
    }
}

#[cfg(feature = "std")]
impl<'a, W, T: Text<'a>> RenderAnnotated<'a, T> for IoWrite<W>
where
    W: std::io::Write,
{
    fn push_annotation(&mut self, _: Rc<AnsiStyle>) -> Result<(), Self::Error> {
        Ok(())
    }

    fn pop_annotation(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }
}

impl<W> Debug for TerminalWriter<W> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("TerminalWriter").finish()
    }
}

impl<W: Write> TerminalWriter<W> {
    /// Creates a new terminal writer.
    pub fn new(upstream: W) -> Self {
        TerminalWriter { color_stack: Vec::new(), upstream: AnsiWriter::new(upstream) }
    }
    /// Creates a new terminal writer with a specific color.
    pub fn with_color(mut self, color: AnsiAbility) -> Self {
        self.upstream.set_ability(color);
        self
    }
}

impl<'a, W, T: Text<'a>> Render<'a, T> for TerminalWriter<W>
where
    W: Write,
{
    type Error = Error;

    fn write_all(&mut self, s: &[T]) -> std::io::Result<()> {
        for i in s {
            self.upstream.write_all(i.as_str().as_bytes())?;
        }

        Ok(())
    }

    fn fail_doc(&self) -> Self::Error {
        Error::other("Document failed to render")
    }
}

impl<'a, W: Write, T: Text<'a>> RenderAnnotated<'a, T> for TerminalWriter<W> {
    fn push_annotation(&mut self, color: Rc<AnsiStyle>) -> Result<(), Self::Error> {
        self.color_stack.push(color.clone());
        self.upstream.set_style(&color)
    }

    fn pop_annotation(&mut self) -> Result<(), Self::Error> {
        self.color_stack.pop();
        match self.color_stack.last() {
            Some(previous) => self.upstream.set_style(previous),
            None => self.upstream.reset_style(),
        }
    }
}

/// Writes to something implementing `std::io::Write`
pub struct VecWrite<'v, T> {
    upstream: &'v mut Vec<T>,
}

impl<'v, W> Debug for VecWrite<'v, W> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("VecWrite").finish()
    }
}

impl<'v, T> VecWrite<'v, T> {
    /// Creates a new terminal writer.
    pub fn new(upstream: &'v mut Vec<T>) -> VecWrite<'v, T> {
        VecWrite { upstream }
    }
}

#[cfg(feature = "std")]
impl<'a, T: Text<'a>> Render<'a, T> for VecWrite<'_, T> {
    type Error = &'static str;

    fn write_all(&mut self, s: &[T]) -> Result<(), &'static str> {
        for i in s {
            self.upstream.push(i.clone());
        }

        Ok(())
    }

    fn fail_doc(&self) -> Self::Error {
        "Document failed to render"
    }
}

#[cfg(feature = "std")]
impl<'a, T: Text<'a>> RenderAnnotated<'a, T> for VecWrite<'_, T> {
    fn push_annotation(&mut self, _: Rc<AnsiStyle>) -> Result<(), Self::Error> {
        Ok(())
    }

    fn pop_annotation(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }
}