summaryrefslogtreecommitdiff
path: root/dhall/src/syntax/ast/text.rs
blob: c40f4a16819fbc3b8f4b8e18297cc7117aecd68a (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use std::iter::FromIterator;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct InterpolatedText<SubExpr> {
    head: String,
    tail: Vec<(SubExpr, String)>,
}

impl<SubExpr> From<(String, Vec<(SubExpr, String)>)>
    for InterpolatedText<SubExpr>
{
    fn from(x: (String, Vec<(SubExpr, String)>)) -> Self {
        InterpolatedText {
            head: x.0,
            tail: x.1,
        }
    }
}

impl<SubExpr> From<String> for InterpolatedText<SubExpr> {
    fn from(s: String) -> Self {
        InterpolatedText {
            head: s,
            tail: vec![],
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InterpolatedTextContents<SubExpr> {
    Text(String),
    Expr(SubExpr),
}

impl<SubExpr> InterpolatedTextContents<SubExpr> {
    pub fn is_empty(&self) -> bool {
        use InterpolatedTextContents::{Expr, Text};
        match self {
            Expr(_) => false,
            Text(s) => s.is_empty(),
        }
    }

    pub fn traverse_ref<'a, SubExpr2, E, F>(
        &'a self,
        mut f: F,
    ) -> Result<InterpolatedTextContents<SubExpr2>, E>
    where
        F: FnMut(&'a SubExpr) -> Result<SubExpr2, E>,
    {
        use InterpolatedTextContents::{Expr, Text};
        Ok(match self {
            Expr(e) => Expr(f(e)?),
            Text(s) => Text(s.clone()),
        })
    }
    pub fn map_ref<'a, SubExpr2, F>(
        &'a self,
        mut f: F,
    ) -> InterpolatedTextContents<SubExpr2>
    where
        F: FnMut(&'a SubExpr) -> SubExpr2,
    {
        use InterpolatedTextContents::{Expr, Text};
        match self {
            Expr(e) => Expr(f(e)),
            Text(s) => Text(s.clone()),
        }
    }
}

impl<SubExpr> InterpolatedText<SubExpr> {
    pub fn len(&self) -> usize {
        1 + 2 * self.tail.len()
    }

    pub fn head(&self) -> &str {
        &self.head
    }

    pub fn tail(&self) -> &Vec<(SubExpr, String)> {
        &self.tail
    }

    pub fn head_mut(&mut self) -> &mut String {
        &mut self.head
    }

    pub fn is_empty(&self) -> bool {
        self.head.is_empty() && self.tail.is_empty()
    }

    pub fn traverse_ref<'a, SubExpr2, E, F>(
        &'a self,
        mut f: F,
    ) -> Result<InterpolatedText<SubExpr2>, E>
    where
        F: FnMut(&'a SubExpr) -> Result<SubExpr2, E>,
    {
        Ok(InterpolatedText {
            head: self.head.clone(),
            tail: self
                .tail
                .iter()
                .map(|(e, s)| Ok((f(e)?, s.clone())))
                .collect::<Result<_, _>>()?,
        })
    }

    pub fn iter<'a>(
        &'a self,
    ) -> impl Iterator<Item = InterpolatedTextContents<&'a SubExpr>> + 'a {
        use std::iter::once;
        use InterpolatedTextContents::{Expr, Text};
        let exprs = self.tail.iter().map(|(e, _)| Expr(e));
        let texts = self.tail.iter().map(|(_, s)| Text(s.clone()));
        once(Text(self.head.clone())).chain(itertools::interleave(exprs, texts))
    }

    pub fn into_iter(
        self,
    ) -> impl Iterator<Item = InterpolatedTextContents<SubExpr>> {
        use std::iter::once;
        use InterpolatedTextContents::{Expr, Text};
        once(Text(self.head)).chain(
            self.tail
                .into_iter()
                .flat_map(|(e, s)| once(Expr(e)).chain(once(Text(s)))),
        )
    }
}

impl<SubExpr> FromIterator<InterpolatedTextContents<SubExpr>>
    for InterpolatedText<SubExpr>
{
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = InterpolatedTextContents<SubExpr>>,
    {
        let mut res = InterpolatedText {
            head: String::new(),
            tail: Vec::new(),
        };
        let mut crnt_str = &mut res.head;
        for x in iter.into_iter() {
            match x {
                InterpolatedTextContents::Text(s) => crnt_str.push_str(&s),
                InterpolatedTextContents::Expr(e) => {
                    res.tail.push((e, String::new()));
                    crnt_str = &mut res.tail.last_mut().unwrap().1;
                }
            }
        }
        res
    }
}