indented_blocks/
indention.rs

1/// Marker type for indented text
2#[derive(Clone, Copy, Debug, PartialEq)]
3#[cfg_attr(feature = "extra-traits", derive(PartialOrd, Eq, Ord, Hash))]
4pub struct Indented<T> {
5    pub indent: T,
6    pub data: T,
7}
8
9impl<'a> Indented<&'a str> {
10    /// This function splits a string into the indention (spaces before text) and the real text
11    ///
12    /// # Example
13    /// ```
14    /// use indented_blocks::indention::Indented;
15    ///
16    /// assert_eq!(
17    ///    Indented::new("  	  abc"),
18    ///    Indented {
19    ///        indent: "  	  ",
20    ///        data: "abc"
21    ///    }
22    /// );
23    /// ```
24    #[inline]
25    pub fn new(s: &'a str) -> Self {
26        let (indent, data) = s.split_at(
27            s.chars()
28                .take_while(|x| x.is_whitespace())
29                .map(|x| x.len_utf8())
30                .sum(),
31        );
32        Indented { indent, data }
33    }
34}
35
36/// Helper trait for "stripping indention" from an object reference
37pub trait IntoUnindented {
38    type Output;
39
40    /// Returns a reference to the unindented part of `Self`
41    fn into_unindented(self) -> Self::Output;
42}
43
44impl<'ast, T: 'ast> IntoUnindented for &'ast Indented<T> {
45    type Output = &'ast T;
46
47    #[inline]
48    fn into_unindented(self) -> &'ast T {
49        &self.data
50    }
51}
52
53impl<'ast, T: 'ast> IntoUnindented for &'ast crate::Block<T>
54where
55    &'ast T: IntoUnindented,
56{
57    type Output = crate::View<'ast, T, fn(&'ast T) -> <&'ast T as IntoUnindented>::Output>;
58
59    #[inline]
60    fn into_unindented(self) -> Self::Output {
61        crate::View::new(self, <&'ast T>::into_unindented)
62    }
63}
64
65impl<'ast, T, F, O> IntoUnindented for crate::View<'ast, T, F>
66where
67    T: 'ast,
68    F: crate::view::ViewFn<&'ast T, Output = O>,
69    O: IntoUnindented + 'ast,
70{
71    type Output = crate::View<'ast, T, crate::view::MapViewFn<F, fn(F::Output) -> O::Output>>;
72
73    #[inline]
74    fn into_unindented(self) -> Self::Output {
75        self.map(O::into_unindented)
76    }
77}