1use crate::rules::RuleIndex;
7use std::{error, fmt};
8
9#[derive(Debug)]
11pub struct RulesError {
12 rule_index: RuleIndex,
13 kind: RulesErrorKind,
14}
15
16impl RulesError {
17 pub fn rule_index(&self) -> RuleIndex {
19 self.rule_index
20 }
21
22 pub fn kind(&self) -> &RulesErrorKind {
24 &self.kind
25 }
26
27 pub(crate) fn resolve_ref(rule_index: RuleIndex, err: guppy::Error) -> Self {
32 Self {
33 rule_index,
34 kind: RulesErrorKind::ResolveRef(err),
35 }
36 }
37
38 pub(crate) fn glob_parse(rule_index: RuleIndex, err: globset::Error) -> Self {
39 let kind = RulesErrorKind::GlobParse {
40 glob: err.glob().map(|s| s.to_owned()),
41 err: Box::new(err),
42 };
43 Self { rule_index, kind }
44 }
45}
46
47impl fmt::Display for RulesError {
48 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49 write!(
50 f,
51 "error while resolving determinator rules: {}: {}",
52 self.rule_index, self.kind
53 )
54 }
55}
56
57impl error::Error for RulesError {
58 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
59 match &self.kind {
60 RulesErrorKind::ResolveRef(err) => Some(err),
61 RulesErrorKind::GlobParse { err, .. } => Some(&**err),
62 }
63 }
64}
65
66#[derive(Debug)]
68#[non_exhaustive]
69pub enum RulesErrorKind {
70 ResolveRef(guppy::Error),
72
73 GlobParse {
75 glob: Option<String>,
77 err: Box<dyn error::Error + Send + Sync>,
79 },
80}
81
82impl fmt::Display for RulesErrorKind {
83 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
84 match self {
85 RulesErrorKind::ResolveRef(err) => write!(f, "{}", err),
86 RulesErrorKind::GlobParse {
87 glob: Some(glob),
88 err,
89 } => write!(f, "while parsing glob '{}': {}", glob, err),
90 RulesErrorKind::GlobParse { glob: None, err } => {
91 write!(f, "while parsing a glob: {}", err)
92 }
93 }
94 }
95}