wpscan_analyze/
errors.rs

1use failure::{Backtrace, Context, Fail};
2use std::fmt;
3
4/// The error kind for errors that get returned in the crate
5#[derive(Eq, PartialEq, Debug, Fail)]
6pub enum ErrorKind {
7    #[fail(display = "data has wrong format")]
8    InvalidFormat,
9    #[fail(display = "invalid file '{}'", _0)]
10    InvalidFile(String),
11    #[fail(display = "invalid output format '{}'", _0)]
12    InvalidOutputFormat(String),
13    #[fail(display = "invalid output detail type '{}'", _0)]
14    InvalidOutputDetail(String),
15    #[fail(display = "output failed")]
16    OutputFailed,
17    #[fail(display = "WpScan is not sane, because {}", _0)]
18    InsaneWpScan(String),
19}
20
21impl Clone for ErrorKind {
22    fn clone(&self) -> Self {
23        use self::ErrorKind::*;
24        match *self {
25            InvalidFormat => InvalidFormat,
26            InvalidFile(ref s) => InvalidFile(s.clone()),
27            InvalidOutputFormat(ref s) => InvalidOutputFormat(s.clone()),
28            InvalidOutputDetail(ref s) => InvalidOutputDetail(s.clone()),
29            OutputFailed => OutputFailed,
30            InsaneWpScan(ref s) => InsaneWpScan(s.clone()),
31        }
32    }
33}
34
35/// The error type for errors that get returned in the lookup module
36#[derive(Debug)]
37pub struct Error {
38    pub(crate) inner: Context<ErrorKind>,
39}
40
41impl Error {
42    /// Get the kind of the error
43    pub fn kind(&self) -> &ErrorKind { self.inner.get_context() }
44}
45
46impl Clone for Error {
47    fn clone(&self) -> Self {
48        Error {
49            inner: Context::new(self.inner.get_context().clone()),
50        }
51    }
52}
53
54impl Fail for Error {
55    fn cause(&self) -> Option<&dyn Fail> { self.inner.cause() }
56
57    fn backtrace(&self) -> Option<&Backtrace> { self.inner.backtrace() }
58}
59
60impl fmt::Display for Error {
61    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.inner, f) }
62}
63
64impl From<ErrorKind> for Error {
65    fn from(kind: ErrorKind) -> Error {
66        Error {
67            inner: Context::new(kind),
68        }
69    }
70}
71
72impl From<Context<ErrorKind>> for Error {
73    fn from(inner: Context<ErrorKind>) -> Error { Error { inner } }
74}
75
76pub type Result<T> = ::std::result::Result<T, Error>;