getopt/
error.rs

1use std::{error, fmt};
2
3use crate::ErrorKind::{self, *};
4
5/// A basic error type for [`Parser`](struct.Parser.html)
6#[derive(Debug, Eq, PartialEq)]
7pub struct Error {
8    culprit: char,
9    kind: ErrorKind,
10}
11
12impl Error {
13    /// Creates a new error using a known kind and the character that caused the issue.
14    pub fn new(kind: ErrorKind, culprit: char) -> Self {
15        Self { culprit, kind }
16    }
17
18    /// Returns the [`ErrorKind`](enum.ErrorKind.html) for this error.
19    pub fn kind(&self) -> ErrorKind {
20        self.kind
21    }
22}
23
24impl fmt::Display for Error {
25    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26        match self.kind {
27            MissingArgument => write!(f, "option requires an argument -- {:?}", self.culprit),
28            UnknownOption => write!(f, "unknown option -- {:?}", self.culprit),
29        }
30    }
31}
32
33impl error::Error for Error {
34    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
35        None
36    }
37}