fapolicy_rules/parser/
errat.rs

1/*
2 * Copyright Concurrent Technologies Corporation 2021
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 */
8
9use crate::parser::error::RuleParseError;
10use crate::parser::error::RuleParseError::*;
11use crate::parser::parse::StrTrace;
12use crate::parser::trace::{Position, Trace};
13
14pub type StrErrorAt<'a> = ErrorAt<StrTrace<'a>>;
15
16#[derive(Debug, PartialEq, Copy, Clone)]
17pub struct ErrorAt<I>(pub RuleParseError<I>, pub usize, pub usize);
18
19impl ErrorAt<Trace<&str>> {
20    pub fn new<'a>(e: RuleParseError<Trace<&'a str>>, t: Trace<&str>) -> ErrorAt<Trace<&'a str>> {
21        ErrorAt(e, t.position(), t.current.len())
22    }
23
24    pub fn new_with_len<'a>(
25        e: RuleParseError<Trace<&'a str>>,
26        t: Trace<&str>,
27        len: usize,
28    ) -> ErrorAt<Trace<&'a str>> {
29        ErrorAt(e, t.position(), t.position + len)
30    }
31}
32
33impl<I> ErrorAt<I> {
34    pub fn shift(self, by: usize) -> Self {
35        ErrorAt(self.0, self.1 + by, self.2 + by)
36    }
37}
38
39impl<T, I> From<ErrorAt<I>> for Result<T, nom::Err<ErrorAt<I>>> {
40    fn from(e: ErrorAt<I>) -> Self {
41        Err(nom::Err::Error(e))
42    }
43}
44
45impl<'a> From<RuleParseError<StrTrace<'a>>> for ErrorAt<StrTrace<'a>> {
46    fn from(e: RuleParseError<StrTrace<'a>>) -> Self {
47        let t = match e {
48            ExpectedDecision(t) => t,
49            UnknownDecision(t, v) => {
50                return ErrorAt::<StrTrace<'a>>::new_with_len(e, t, v.current.len())
51            }
52            ExpectedPermTag(t, v) => {
53                return ErrorAt::<StrTrace<'a>>::new_with_len(e, t, v.current.len())
54            }
55            ExpectedPermType(t, v) => {
56                return ErrorAt::<StrTrace<'a>>::new_with_len(e, t, v.current.len())
57            }
58            ExpectedPermAssignment(t) => return ErrorAt::<StrTrace<'a>>::new_with_len(e, t, 0),
59            ExpectedEndOfInput(t) => t,
60            ExpectedWhitespace(t) => t,
61            MissingSeparator(t) => t,
62            MissingSubject(t) => t,
63            MissingObject(t) => t,
64            MissingBothSubjObj(t) => t,
65            UnknownSubjectPart(t) => t,
66            SubjectPartExpected(t) => t,
67            ExpectedInt(t) => t,
68
69            UnknownObjectPart(t) => t,
70            ObjectPartExpected(t) => t,
71            ExpectedDirPath(t) => t,
72            ExpectedFilePath(t) => t,
73            ExpectedPattern(t) => t,
74            ExpectedBoolean(t, v) => {
75                return ErrorAt::<StrTrace<'a>>::new_with_len(e, t, v.current.len())
76            }
77            ExpectedFileType(t) => t,
78
79            Nom(t, _) => t,
80        };
81        ErrorAt::<StrTrace<'a>>::new(e, t)
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use crate::parser::errat::StrErrorAt;
88    use crate::parser::error::RuleParseError;
89    use crate::parser::parse::StrTrace;
90
91    #[test]
92    fn shift_test() {
93        let t = StrTrace::new("foo");
94        let e = StrErrorAt::new(RuleParseError::ExpectedInt(t), t);
95        assert_eq!(t.position, e.1);
96        assert_eq!(t.current.len(), e.2);
97        assert_eq!(3, e.2);
98
99        let by = 1001;
100        let e = e.shift(by);
101        assert_eq!(t.position + by, e.1);
102        assert_eq!(t.current.len() + by, e.2);
103        assert_eq!(3 + by, e.2);
104    }
105}