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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use alloc::fmt;
use core::ops;

/// 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,
}

/// Marker type for text stripped off any indention
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "extra-traits", derive(PartialEq, PartialOrd, Eq, Ord, Hash))]
#[repr(transparent)]
pub struct Unindented<T: ?Sized>(pub T);

impl<T: ?Sized> ops::Deref for Unindented<T> {
    type Target = T;
    #[inline(always)]
    fn deref(&self) -> &T {
        &self.0
    }
}

impl<T: ?Sized> ops::DerefMut for Unindented<T> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut T {
        &mut self.0
    }
}

impl<T: fmt::Display + ?Sized> fmt::Display for Unindented<T> {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// Helper trait for "stripping indention" from an object
pub trait Unindent {
    type Item;

    /// Strips any indention off `self` and returns the unindented version of itself.
    fn unindent(self) -> Unindented<Self::Item>;
}

impl<T> Unindent for Indented<T> {
    type Item = T;

    #[inline]
    fn unindent(self) -> Unindented<T> {
        Unindented(self.data)
    }
}

impl<T> Unindent for Unindented<T> {
    type Item = T;

    #[inline(always)]
    fn unindent(self) -> Self {
        self
    }
}

impl<T: Unindent> Unindent for crate::Block<T> {
    type Item = crate::Block<<T as Unindent>::Item>;

    #[inline]
    fn unindent(self) -> Unindented<Self::Item> {
        Unindented(self.map(|i| i.unindent().0))
    }
}

#[cfg(feature = "extra-traits")]
impl<T: Unindent> Unindent for alloc::vec::Vec<T> {
    type Item = alloc::vec::Vec<<T as Unindent>::Item>;

    #[inline]
    fn unindent(self) -> Unindented<Self::Item> {
        Unindented(self.into_iter().map(|i| i.unindent().0).collect())
    }
}