Debian Codegraph @

src/cfg_expr/error.rs

raw
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 use alloc::boxed::Box;
4 use core::fmt;
5
6 /// An error related to parsing of a cfg expression
7 #[derive(Debug, PartialEq, Eq)]
8 pub(crate) struct ParseError {
9 /// The string that was parsed
10 pub(crate) original: Box<str>,
11 /// The range of characters in the original string that result
12 /// in this error
13 pub(crate) span: core::ops::Range<usize>,
14 /// The specific reason for the error
15 pub(crate) reason: Reason,
16 }
17
18 /// The particular reason for a `ParseError`
19 #[derive(Debug, PartialEq, Eq)]
20 pub(crate) enum Reason {
21 /// not() takes exactly 1 predicate, unlike all() and any()
22 InvalidNot(usize),
23 /// An opening parens was unmatched with a closing parens
24 UnclosedParens,
25 /// A closing parens was unmatched with an opening parens
26 UnopenedParens,
27 /// An opening quotes was unmatched with a closing quotes
28 UnclosedQuotes,
29 /// The expression does not contain any valid terms
30 Empty,
31 /// Found an unexpected term, which wasn't one of the expected terms that
32 /// is listed
33 Unexpected(&'static [&'static str]),
34 /// The root cfg() may only contain a single predicate
35 MultipleRootPredicates,
36 }
37
38 impl fmt::Display for ParseError {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 f.write_str(&self.original)?;
41 f.write_str("\n")?;
42
43 for _ in 0..self.span.start {
44 f.write_str(" ")?;
45 }
46
47 // Mismatched parens/quotes have a slightly different output
48 // than the other errors
49 match &self.reason {
50 r @ (Reason::UnclosedParens | Reason::UnclosedQuotes) => {
51 f.write_fmt(format_args!("- {r}"))
52 }
53 r @ Reason::UnopenedParens => f.write_fmt(format_args!("^ {r}")),
54 other => {
55 for _ in self.span.start..self.span.end {
56 f.write_str("^")?;
57 }
58
59 f.write_fmt(format_args!(" {other}"))
60 }
61 }
62 }
63 }
64
65 impl fmt::Display for Reason {
66 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 use Reason::{
68 Empty, InvalidNot, MultipleRootPredicates, UnclosedParens, UnclosedQuotes, Unexpected,
69 UnopenedParens,
70 };
71
72 match self {
73 UnclosedParens => f.write_str("unclosed parens"),
74 UnopenedParens => f.write_str("unopened parens"),
75 UnclosedQuotes => f.write_str("unclosed quotes"),
76 Empty => f.write_str("empty expression"),
77 Unexpected(expected) => {
78 if expected.len() > 1 {
79 f.write_str("expected one of ")?;
80
81 for (i, exp) in expected.iter().enumerate() {
82 f.write_fmt(format_args!("{}`{exp}`", if i > 0 { ", " } else { "" }))?;
83 }
84 f.write_str(" here")
85 } else if !expected.is_empty() {
86 f.write_fmt(format_args!("expected a `{}` here", expected[0]))
87 } else {
88 f.write_str("the term was not expected here")
89 }
90 }
91 InvalidNot(np) => f.write_fmt(format_args!("not() takes 1 predicate, found {np}")),
92 MultipleRootPredicates => f.write_str("multiple root predicates"),
93 }
94 }
95 }
96
97 impl core::error::Error for ParseError {}