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
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::types::{Identifier, OneOrMany, Operator, ValueType};
use nom::error::ErrorKind;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
    #[error("Static selector directories are expected to be flat")]
    NonFlatDirectory,

    #[error(transparent)]
    Parse(#[from] ParseError),

    #[error(transparent)]
    Io(#[from] std::io::Error),

    #[error("Selector arguments must be structured or raw")]
    InvalidSelectorArgument,

    #[error("Property selectors must have non-empty node_path vector")]
    EmptyPropertySelectorNodePath,

    #[error("TreeSelector only supports property and subtree selection.")]
    InvalidTreeSelector,

    #[error("Recursive wildcards aren't allowed in this position")]
    RecursiveWildcardNotAllowed,

    #[error("Selecter fails verification due to unmatched escape character")]
    UnmatchedEscapeCharacter,

    #[error(transparent)]
    Validation(#[from] ValidationError),
}

#[derive(Debug, Error)]
pub enum ParseError {
    #[error("Failed to parse the input. Failed at: {0:?} with: {1:?}")]
    Fast(String, ErrorKind),

    #[error("Failed to parse the input. Error: {0}")]
    Verbose(String),

    #[error(transparent)]
    Validation(#[from] ValidationError),
}

#[derive(Debug, Error)]
pub enum ValidationError {
    #[error("Component selectors require at least one segment")]
    EmptyComponentSelector,

    #[error("Subtree selectors must have non-empty node_path vector")]
    EmptySubtreeSelector,

    #[error("String selectors must be string patterns or exact matches")]
    InvalidStringSelector,

    #[error("String pattern '{0}' failed verification. Errors: {1:?}")]
    InvalidStringPattern(String, Vec<StringPatternError>),

    #[error("Selectors require a tree selector")]
    MissingTreeSelector,

    #[error("Selectors require a component selector")]
    MissingComponentSelector,

    #[error("String patterns cannot be empty.")]
    EmptyStringPattern,

    #[error("Operator {1:?} cannot be used with identifier {0:?}")]
    InvalidOperator(Identifier, Operator),

    #[error("Value {1:?} cannot be used with identifier {0:?}")]
    InvalidValueType(Identifier, OneOrMany<ValueType>),

    #[error("Value {1:?} cannot be used with operator {0:?}")]
    InvalidOperatorRhs(Operator, OneOrMany<ValueType>),
}

#[derive(Debug)]
pub enum StringPatternError {
    UnescapedGlob,
    UnescapedColon,
    UnescapedForwardSlash,
}