]> git.proxmox.com Git - cargo.git/blob - crates/cargo-platform/src/error.rs
Emit error on [target.cfg(debug_assertions).dependencies]
[cargo.git] / crates / cargo-platform / src / error.rs
1 use std::fmt;
2
3 #[derive(Debug)]
4 pub struct ParseError {
5 kind: ParseErrorKind,
6 orig: String,
7 }
8
9 #[derive(Debug)]
10 pub enum ParseErrorKind {
11 UnterminatedString,
12 UnexpectedChar(char),
13 UnexpectedToken {
14 expected: &'static str,
15 found: &'static str,
16 },
17 IncompleteExpr(&'static str),
18 UnterminatedExpression(String),
19 InvalidTarget(String),
20 InvalidCfgName(String),
21 InvalidCfgKey(String),
22
23 #[doc(hidden)]
24 __Nonexhaustive,
25 }
26
27 impl fmt::Display for ParseError {
28 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29 write!(
30 f,
31 "failed to parse `{}` as a cfg expression: {}",
32 self.orig, self.kind
33 )
34 }
35 }
36
37 impl fmt::Display for ParseErrorKind {
38 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39 use ParseErrorKind::*;
40 match self {
41 UnterminatedString => write!(f, "unterminated string in cfg"),
42 UnexpectedChar(ch) => write!(
43 f,
44 "unexpected character `{}` in cfg, expected parens, a comma, \
45 an identifier, or a string",
46 ch
47 ),
48 UnexpectedToken { expected, found } => {
49 write!(f, "expected {}, found {}", expected, found)
50 }
51 IncompleteExpr(expected) => {
52 write!(f, "expected {}, but cfg expression ended", expected)
53 }
54 UnterminatedExpression(s) => {
55 write!(f, "unexpected content `{}` found after cfg expression", s)
56 }
57 InvalidTarget(s) => write!(f, "invalid target specifier: {}", s),
58 InvalidCfgName(name) => write!(f, "invalid name in target cfg: {}", name),
59 InvalidCfgKey(name) => write!(f, "invalid key in target cfg: {}", name),
60 __Nonexhaustive => unreachable!(),
61 }
62 }
63 }
64
65 impl std::error::Error for ParseError {}
66
67 impl ParseError {
68 pub fn new(orig: &str, kind: ParseErrorKind) -> ParseError {
69 ParseError {
70 kind,
71 orig: orig.to_string(),
72 }
73 }
74 }