dd_lib/opts/
error.rs

1pub type Result<T> = ::std::result::Result<T, Error>;
2use opts::Kind;
3use std::{error, fmt};
4
5#[derive(Debug, PartialEq, Eq, Clone)]
6/// An error dealing with an option
7pub enum Error {
8    /// An unimplemented but valid option
9    Unimplemented(Kind, &'static str),
10    /// An unknown (and unsupported) option
11    Unknown(Kind, String),
12    /// An error parsing a numeric option
13    ParseInt(std::num::ParseIntError),
14    /// The directory flag is specified, but the target is not a directory.
15    NotDirectory(String),
16    /// Two or more mutually exclusive options have been selected.
17    ConflictingOption(&'static str),
18}
19
20impl From<std::num::ParseIntError> for Error {
21    fn from(err: std::num::ParseIntError) -> Self { Error::ParseInt(err) }
22}
23impl error::Error for Error {}
24
25impl fmt::Display for Kind {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        match *self {
28            Kind::BasicOption => write!(f, "basic option"),
29            Kind::CFlag => write!(f, "conversion flag"),
30            Kind::IFlag => write!(f, "iflag"),
31            Kind::OFlag => write!(f, "oflag"),
32        }
33    }
34}
35
36impl fmt::Display for Error {
37    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38        match self {
39            Error::Unknown(kind, arg) => write!(f, "unknown {} option \"{}\"", kind, arg),
40            Error::Unimplemented(kind, arg) => write!(f, "unimplemented {} option \"{}\"", kind, arg),
41            Error::ParseInt(err) => err.fmt(f),
42            Error::NotDirectory(s) => write!(
43                f,
44                "flag 'directory' was specified for path {}, but it was not a directory",
45                s
46            ),
47            Error::ConflictingOption(s) => write!(f, "conflicting options: {}", s),
48        }
49    }
50}