logparse-pretty-print 0.1.0

pretty print tree
Documentation
use std::borrow::Cow;

/// Pretty printing can work with arbitrary text-like objects,
/// as long as they implement this trait.
#[allow(clippy::len_without_is_empty)]
pub trait Text<'a>: Sized + Clone + 'a {
    /// Turn a &'static str into Self, without any further knowledge about it.
    ///
    /// Used for a bunch of functions to give a default.
    /// Often not the most optimal or even desired implementation.
    fn from_static_str(s: &'static str) -> Self;

    /// We foten insert spaces as a static str.
    ///
    /// By default this is implemented with [`from_static_str`].
    fn from_static_spaces(s: &'static str) -> Self {
        Self::from_static_str(s)
    }

    /// Turn custom text into a `Cow<'a, str>`.
    fn as_str(&self) -> Cow<'_, str>;

    /// Get the length of the custom text.
    fn len(&self) -> usize {
        str::len(&self.as_str())
    }

    /// A space character
    ///
    /// By default this is implemented with [`from_static_str`].
    fn space() -> Self {
        Self::from_static_str(" ")
    }

    /// A newline character
    ///
    /// By default this is implemented with [`from_static_str`].
    fn newline() -> Self {
        Self::from_static_str("\n")
    }

    /// A comma and space character
    ///
    /// By default this is implemented with [`from_static_str`].
    fn comma_space() -> Self {
        Self::from_static_str(", ")
    }
}

impl<'a> Text<'a> for Cow<'a, str> {
    fn from_static_str(s: &'static str) -> Self {
        Cow::Borrowed(s)
    }

    fn as_str(&self) -> Cow<'a, str> {
        self.clone()
    }
}