fapolicy_rules/parser/
error.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::trace::Trace;
10use nom::error::{ErrorKind, ParseError};
11use std::fmt::{Display, Formatter};
12use RuleParseError::*;
13
14#[derive(Clone, Copy, Debug, PartialEq)]
15pub enum RuleParseError<I> {
16    ExpectedDecision(I),
17    UnknownDecision(I, I),
18    ExpectedPermTag(I, I),
19    ExpectedPermType(I, I),
20    ExpectedPermAssignment(I),
21    ExpectedEndOfInput(I),
22    ExpectedWhitespace(I),
23    MissingSeparator(I),
24    MissingSubject(I),
25    MissingObject(I),
26    MissingBothSubjObj(I),
27
28    UnknownSubjectPart(I),
29    UnknownObjectPart(I),
30
31    SubjectPartExpected(I),
32    ObjectPartExpected(I),
33
34    ExpectedInt(I),
35
36    ExpectedDirPath(I),
37    ExpectedFilePath(I),
38    ExpectedPattern(I),
39    ExpectedBoolean(I, I),
40    ExpectedFileType(I),
41
42    Nom(I, ErrorKind),
43}
44
45impl<I> ParseError<I> for RuleParseError<I> {
46    fn from_error_kind(input: I, kind: ErrorKind) -> Self {
47        RuleParseError::Nom(input, kind)
48    }
49
50    fn append(_: I, _: ErrorKind, other: Self) -> Self {
51        other
52    }
53}
54
55impl Display for RuleParseError<Trace<&str>> {
56    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
57        match self {
58            ExpectedDecision(_) => f.write_str("Expected Decision"),
59            UnknownDecision(_, _) => f.write_str("Unknown Decision"),
60            ExpectedPermTag(_, _) => f.write_str("Expected tag 'perm'"),
61            ExpectedPermType(_, _) => f.write_str("Expected one of 'any', 'open', 'execute'"),
62            ExpectedPermAssignment(_) => f.write_str("Expected assignment (=)"),
63            ExpectedEndOfInput(_) => f.write_str("Unexpected trailing chars"),
64            ExpectedWhitespace(_) => f.write_str("Expected whitespace"),
65            MissingSeparator(_) => f.write_str("Missing colon separator"),
66            MissingSubject(_) => f.write_str("Missing Subject"),
67            MissingObject(_) => f.write_str("Expected Object"),
68            MissingBothSubjObj(_) => f.write_str("Missing Subject and Object"),
69            UnknownSubjectPart(_) | UnknownObjectPart(_) => f.write_str("Expected one of ....."),
70            SubjectPartExpected(_) => f.write_str("Expected a Subject part"),
71            ObjectPartExpected(_) => f.write_str("Expected an Object part"),
72            ExpectedInt(_) => f.write_str("Expected integer value"),
73            ExpectedDirPath(_) => f.write_str("Expected dir path"),
74            ExpectedFilePath(_) => f.write_str("Expected file path"),
75            ExpectedPattern(_) => f.write_str("Expected pattern"),
76            ExpectedBoolean(_, _) => f.write_str("Expected boolean (0, 1) value"),
77            ExpectedFileType(_) => f.write_str("Expected mime file type"),
78            e @ Nom(_, _) => f.write_fmt(format_args!("{:?}", e)),
79        }
80    }
81}