summaryrefslogtreecommitdiff
path: root/dhall_syntax/src/text.rs
blob: 83643d9f3702996090c8ed013bc23e2c7eb8a387 (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
use std::iter::FromIterator;

#[derive(Debug, Clone, PartialEq, Eq)]
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(),
        }
    }
}

impl<SubExpr> InterpolatedText<SubExpr> {
    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<SubExpr>> + 'a
    where
        SubExpr: Clone,
    {
        use std::iter::once;
        use InterpolatedTextContents::{Expr, Text};
        once(Text(self.head.clone()))
            .chain(self.tail.iter().flat_map(|(e, s)| {
                once(Expr(SubExpr::clone(e))).chain(once(Text(s.clone())))
            }))
            .filter(|c| !c.is_empty())
    }

    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)))),
            )
            .filter(|c| !c.is_empty())
    }
}

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