Skip to main content

harn_rules/
error.rs

1//! Error type for the rule engine.
2
3use thiserror::Error;
4
5/// Anything that can go wrong loading, compiling, or running a rule.
6#[derive(Debug, Error)]
7pub enum RulesError {
8    /// A rule file could not be read.
9    #[error("read rule `{path}`: {source}")]
10    Read {
11        /// The path that failed to read.
12        path: String,
13        /// The underlying I/O error.
14        source: std::io::Error,
15    },
16
17    /// A rule file could not be parsed as TOML.
18    #[error("parse rule `{path}`: {source}")]
19    Parse {
20        /// The path that failed to parse.
21        path: String,
22        /// The underlying TOML error.
23        source: Box<toml::de::Error>,
24    },
25
26    /// A rule referenced a language the engine does not know.
27    #[error("rule `{rule}`: unknown language `{language}`")]
28    UnknownLanguage {
29        /// The offending rule id.
30        rule: String,
31        /// The language string that did not resolve.
32        language: String,
33    },
34
35    /// A rule's language is known but its grammar was not compiled into
36    /// this build.
37    #[error("rule `{rule}`: grammar for `{language}` is not compiled into this build")]
38    GrammarUnavailable {
39        /// The offending rule id.
40        rule: String,
41        /// The language whose grammar is missing.
42        language: String,
43    },
44
45    /// A `pattern` snippet failed to compile into a tree-sitter query.
46    #[error("rule `{rule}`: {message}")]
47    PatternCompile {
48        /// The offending rule id.
49        rule: String,
50        /// What went wrong compiling the snippet.
51        message: String,
52    },
53
54    /// The compiled query was rejected by tree-sitter. This is an engine
55    /// bug if it happens for a snippet that parsed cleanly, so it carries
56    /// the generated query for debugging.
57    #[error("rule `{rule}`: generated query rejected by tree-sitter: {message}\nquery:\n{query}")]
58    QueryRejected {
59        /// The offending rule id.
60        rule: String,
61        /// The tree-sitter query error.
62        message: String,
63        /// The generated S-expression query.
64        query: String,
65    },
66
67    /// Source text could not be parsed in the rule's grammar.
68    #[error("rule `{rule}`: parse source: {message}")]
69    SourceParse {
70        /// The offending rule id.
71        rule: String,
72        /// The parse failure detail.
73        message: String,
74    },
75
76    /// `auto_apply` was called on a rule whose `safety` is above the
77    /// machine-applicable threshold; it must be applied with explicit opt-in.
78    #[error(
79        "rule `{rule}`: refusing to auto-apply a `{safety}` fix (machine-applicable required)"
80    )]
81    NotAutoApplicable {
82        /// The offending rule id.
83        rule: String,
84        /// The rule's declared safety tier.
85        safety: String,
86    },
87
88    /// A fix did not reach a fixed point: re-running it after applying still
89    /// produced changes.
90    #[error("rule `{rule}`: fix is not idempotent (re-running it produced further changes)")]
91    NotIdempotent {
92        /// The offending rule id.
93        rule: String,
94    },
95}