]> git.proxmox.com Git - cargo.git/blob - crates/cargo-platform/src/error.rs
Auto merge of #7375 - ehuss:extract-platform, r=alexcrichton
[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
21 #[doc(hidden)]
22 __Nonexhaustive,
23 }
24
25 impl fmt::Display for ParseError {
26 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27 write!(
28 f,
29 "failed to parse `{}` as a cfg expression: {}",
30 self.orig, self.kind
31 )
32 }
33 }
34
35 impl fmt::Display for ParseErrorKind {
36 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37 use ParseErrorKind::*;
38 match self {
39 UnterminatedString => write!(f, "unterminated string in cfg"),
40 UnexpectedChar(ch) => write!(
41 f,
42 "unexpected character `{}` in cfg, expected parens, a comma, \
43 an identifier, or a string",
44 ch
45 ),
46 UnexpectedToken { expected, found } => {
47 write!(f, "expected {}, found {}", expected, found)
48 }
49 IncompleteExpr(expected) => {
50 write!(f, "expected {}, but cfg expression ended", expected)
51 }
52 UnterminatedExpression(s) => {
53 write!(f, "unexpected content `{}` found after cfg expression", s)
54 }
55 InvalidTarget(s) => write!(f, "invalid target specifier: {}", s),
56 __Nonexhaustive => unreachable!(),
57 }
58 }
59 }
60
61 impl std::error::Error for ParseError {}
62
63 impl ParseError {
64 pub fn new(orig: &str, kind: ParseErrorKind) -> ParseError {
65 ParseError {
66 kind,
67 orig: orig.to_string(),
68 }
69 }
70 }