pear/
expected.rs

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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use std::fmt;

use inlinable_string::InlinableString;

use crate::input::Show;

#[derive(Clone)]
pub enum CowInlineString {
    Borrowed(&'static str),
    Inline(InlinableString)
}

impl std::ops::Deref for CowInlineString {
    type Target = str;
    fn deref(&self) -> &Self::Target {
        match self {
            CowInlineString::Borrowed(s) => s,
            CowInlineString::Inline(s) => s,
        }
    }
}

impl std::fmt::Display for CowInlineString {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        str::fmt(self, f)
    }
}

impl std::fmt::Debug for CowInlineString {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        str::fmt(self, f)
    }
}

pub enum Expected<Token, Slice> {
    Token(Option<InlinableString>, Option<Token>),
    Slice(Option<InlinableString>, Option<Slice>),
    Eof(Option<Token>),
    Other(CowInlineString),
    Elided
}

impl<Token, Slice> Expected<Token, Slice> {
    pub fn token<T: Show>(expected: Option<&T>, found: Option<Token>) -> Self {
        let expected = expected.map(|t| iformat!("{}", t as &dyn Show));
        Expected::Token(expected, found)
    }

    pub fn eof(found: Option<Token>) -> Self {
        Expected::Eof(found)
    }
}

impl<Token, Slice> Expected<Token, Slice> {
    pub fn slice<S: Show>(expected: Option<&S>, found: Option<Slice>) -> Self {
        let expected = expected.map(|t| iformat!("{}", t as &dyn Show));
        Expected::Slice(expected, found)
    }
}

impl<Token, Slice> Expected<Token, Slice> {
    pub fn map<FT, FS, T, S>(self, t: FT, s: FS) -> Expected<T, S>
        where FT: Fn(Token) -> T, FS: Fn(Slice) -> S
    {
        use Expected::*;

        match self {
            Token(e, v) => Token(e, v.map(t)),
            Slice(e, v) => Slice(e, v.map(s)),
            Eof(v) => Eof(v.map(t)),
            Other(v) => Other(v),
            Expected::Elided => Expected::Elided,
        }
    }
}

impl<T: ToOwned, S: ?Sized + ToOwned> Expected<T, &S> {
    pub fn into_owned(self) -> Expected<T::Owned, S::Owned> {
        self.map(|t| t.to_owned(), |s| s.to_owned())
    }
}

impl<T, S> From<String> for Expected<T, S> {
    #[inline(always)]
    fn from(string: String) -> Expected<T, S> {
        Expected::Other(CowInlineString::Inline(InlinableString::from(string)))
    }
}

#[doc(hidden)]
impl<T, S> From<InlinableString> for Expected<T, S> {
    #[inline(always)]
    fn from(string: InlinableString) -> Expected<T, S> {
        Expected::Other(CowInlineString::Inline(string))
    }
}

impl<T, S> From<&'static str> for Expected<T, S> {
    #[inline(always)]
    fn from(string: &'static str) -> Expected<T, S> {
        Expected::Other(CowInlineString::Borrowed(string))
    }
}

impl<T: fmt::Debug, S: fmt::Debug> fmt::Debug for Expected<T, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Expected::Token(e, v) => {
                f.debug_tuple("Expected::Token").field(&e).field(&v).finish()
            }
            Expected::Slice(e, v) => {
                f.debug_tuple("Expected::Slice").field(&e).field(&v).finish()
            }
            Expected::Eof(v) => {
                f.debug_tuple("Expected::Eof").field(&v).finish()
            }
            Expected::Other(v) => {
                f.debug_tuple("Expected::Other").field(&v).finish()
            }
            Expected::Elided => f.debug_tuple("Expected::Elided").finish()
        }
    }
}

impl<T: Clone, S: Clone> Clone for Expected<T, S> {
    fn clone(&self) -> Self {
        match self {
            Expected::Token(e, f) => Expected::Token(e.clone(), f.clone()),
            Expected::Slice(e, f) => Expected::Slice(e.clone(), f.clone()),
            Expected::Eof(f) => Expected::Eof(f.clone()),
            Expected::Other(v) => Expected::Other(v.clone()),
            Expected::Elided => Expected::Elided,
        }
    }
}

impl<T: Show, S: Show> fmt::Display for Expected<T, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Expected::Token(Some(ref expected), Some(ref found)) => {
                let found = found as &dyn Show;
                write!(f, "expected token {} but found {}", expected, found)
            }
            Expected::Token(None, Some(ref found)) => {
                let found = found as &dyn Show;
                write!(f, "unexpected token: {}", found)
            }
            Expected::Token(Some(ref expected), None) => {
                write!(f, "unexpected EOF: expected token {}", expected)
            }
            Expected::Token(None, None) => {
                write!(f, "unexpected EOF: expected some token")
            }
            Expected::Slice(Some(ref expected), Some(ref found)) => {
                let found = found as &dyn Show;
                write!(f, "expected slice {} but found {}", expected, found)
            }
            Expected::Slice(None, Some(ref found)) => {
                let found = found as &dyn Show;
                write!(f, "unexpected slice: {}", found)
            }
            Expected::Slice(Some(ref expected), None) => {
                write!(f, "unexpected EOF: expected slice {}", expected)
            }
            Expected::Slice(None, None) => {
                write!(f, "unexpected EOF: expected some slice")
            }
            Expected::Eof(None) => {
                write!(f, "expected EOF but input remains")
            }
            Expected::Eof(Some(ref found)) => {
                let found = found as &dyn Show;
                write!(f, "unexpected token {}", found)
            }
            Expected::Other(ref other) => write!(f, "{}", other),
            Expected::Elided => write!(f, "[ERROR ELIDED]")
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Expected;

    #[test]
    fn test_into_owned() {
        let expected: Expected<char, &str> = Expected::Slice(None, Some("hi"));
        let _owned: Expected<char, String> = expected.into_owned();
    }
}