Skip to main content

openmoat_core/policy/
error.rs

1//! Why a policy file cannot be used.
2
3use thiserror::Error;
4
5use super::{SUPPORTED_VERSION, schema};
6
7/// Why a policy file cannot be used. Any of these keeps `guard` from deciding.
8///
9/// Messages include their cause, so no variant also exposes it as `source`:
10/// a chained report (`{:#}`) would print it twice.
11#[derive(Debug, Error)]
12#[non_exhaustive]
13pub enum PolicyError {
14    /// The file is not valid YAML or does not fit the schema (of
15    /// [`SUPPORTED_VERSION`], the one it declares: another is
16    /// [`PolicyError::Version`]). The message names the field.
17    #[error("policy parse error (policy schema version {SUPPORTED_VERSION}): {0}")]
18    Parse(crate::yaml::Error),
19    /// The `version` key names a schema this build does not know (ADR-103).
20    #[error("{}", schema::unsupported(*found, *supported))]
21    Version {
22        /// The version in the file.
23        found: u32,
24        /// The version this build supports.
25        supported: u32,
26    },
27    /// The document has no `version` key (ADR-103).
28    #[error(
29        "the policy has no `version:`; add `version: {supported}` as its first key, the policy \
30         schema version this build reads (`openmoat policy migrate` adds it)"
31    )]
32    MissingVersion {
33        /// The version this build supports.
34        supported: u32,
35    },
36    /// Two rule groups share an id.
37    #[error("duplicate rule id `{0}`")]
38    DuplicateId(String),
39    /// A rule group lists no patterns.
40    #[error("rule `{0}` has no patterns")]
41    EmptyRule(String),
42    /// A pattern is empty (or only `!`/`$`).
43    #[error("empty pattern")]
44    EmptyPattern,
45    /// A glob does not compile.
46    #[error("bad glob `{pattern}`: {error}")]
47    BadGlob {
48        /// The pattern as written.
49        pattern: String,
50        /// The glob compiler's explanation.
51        error: globset::Error,
52    },
53    /// A shell pattern does not tokenise (unbalanced quotes, a here-document).
54    #[error("bad shell pattern `{pattern}` (unbalanced quotes?)")]
55    BadShellPattern {
56        /// The pattern as written.
57        pattern: String,
58    },
59    /// The file has both `ask:` and `review:` (ADR-053): `ask` is the old name
60    /// of `review`, and which list was meant cannot be told.
61    #[error(
62        "the policy has both `{old}:` and `{new}:`; `{old}` is the old name of `{new}`: \
63         move the `{old}:` rules into `{new}:` (`openmoat policy upgrade` renames a policy \
64         that has only `{old}:`)"
65    )]
66    OldAndNewName {
67        /// The old key.
68        old: &'static str,
69        /// The current key.
70        new: &'static str,
71    },
72    /// A rule group or `executables` entry is invalid; `problem` says why.
73    #[error("rule `{rule}`: {problem}")]
74    Rule {
75        /// Rule id, or `executables.<name>`.
76        rule: String,
77        /// What is wrong.
78        problem: String,
79    },
80}