1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/// Marker type for indented text
#[derive(Clone, Copy, Debug)]
#[cfg_attr(any(feature = "extra-traits", test), derive(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
    #[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)
    }
}