indented-blocks 0.6.0

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 IntoUnindented {
    type Output;

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

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

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

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

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

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

    #[inline]
    fn into_unindented(self) -> Self::Output {
        self.map(O::into_unindented)
    }
}