#
cargo_platform/
error.rs

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