indented-blocks 0.5.2

parser for indented blocks
Documentation
/// Marker type for indented text
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "extra-traits", derive(PartialOrd, Eq, Ord, Hash))]
pub struct Indented<T> {
    pub indent: T,
    pub data: T,
}

impl<'a> Indented<&'a str> {
    /// This function splits a string into the indention (spaces before text) and the real text
    ///
    /// # Example
    /// ```
    /// use indented_blocks::indention::Indented;
    ///
    /// assert_eq!(
    ///    Indented::new("  	  abc"),
    ///    Indented {
    ///        indent: "  	  ",
    ///        data: "abc"
    ///    }
    /// );
    /// ```
    #[inline]
    pub fn new(s: &'a str) -> Self {
        let (indent, data) = s.split_at(
            s.chars()
                .take_while(|x| x.is_whitespace())
                .map(|x| x.len_utf8())
                .sum(),
        );
        Indented { indent, data }
    }
}

/// Helper trait for "stripping indention" from an object reference
pub trait AsUnindented<'ast> {
    type Output;

    /// Returns a reference to the unindented part of `Self`
    fn as_unindented(&'ast self) -> Self::Output;
}

impl<'ast, T: 'ast> AsUnindented<'ast> for Indented<T> {
    type Output = &'ast T;

    #[inline]
    fn as_unindented(&'ast self) -> &'ast T {
        &self.data
    }
}

impl<'ast, T: 'ast> AsUnindented<'ast> for crate::Block<T>
where
    T: AsUnindented<'ast> + 'ast,
{
    type Output = crate::View<'ast, T, fn(&'ast T) -> T::Output>;

    #[inline]
    fn as_unindented(&'ast self) -> Self::Output {
        crate::View::new(self, T::as_unindented)
    }
}

impl<'ast, T: 'ast, F, O> AsUnindented<'ast> for crate::View<'ast, T, F>
where
    Self: Clone,
    F: crate::view::ViewFn<'ast, T, Output = &'ast O>,
    O: AsUnindented<'ast> + 'ast,
{
    type Output = crate::View<'ast, T, crate::view::MapViewFn<F, fn(F::Output) -> O::Output>>;

    #[inline]
    fn as_unindented(&'ast self) -> Self::Output {
        self.clone().map(O::as_unindented)
    }
}