1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use std::fmt;
use failure::*;
use failure_derive::Fail;
use std::result;
use ascii;

use super::*;

/// Result that encapsulates the Error type of this library
pub type Result<T> = result::Result<T, Error>;

/// Error in ASN1-DER decode/encode operations
#[derive(Debug)]
pub struct Error {
    inner: Context<ErrorKind>
}

/// Type of error
#[derive(Clone, Debug, PartialEq, Fail)]
pub enum ErrorKind {
    /// Error decoding tag
    #[fail (display = "{}", _0)]
    InvalidTag(Box<TagErrorKind>),
    
    /// Error decoding length
    #[fail (display = "{}", _0)]
    InvalidLength(Box<LengthErrorKind>),

    /// Error decoding value
    #[fail (display = "{}", _0)]
    InvalidValue(Box<ValueErrorKind>),

    /// No value was provided to encode
    #[fail (display = "No value provided")]
    NoValue,

    /// No found component with the identifier specified
    #[fail (display = "No component with such identifier")]
    NoComponent,

    /// Error in a field of a sequence
    #[fail (display = "{}::{} => {}", _0,_1,_2)]
    SequenceFieldError(String, String, Box<ErrorKind>),

    /// Error while processing a sequence
    #[fail (display = "{} => {}", _0,_1)]
    SequenceError(String, Box<ErrorKind>)
}

impl From<ValueErrorKind> for ErrorKind {
    fn from(kind: ValueErrorKind) -> Self {
        return ErrorKind::InvalidValue(Box::new(kind));
    }
}


impl Error {

    pub fn kind(&self) -> &ErrorKind {
        return self.inner.get_context();
    }

}

impl Fail for Error {
    fn cause(&self) -> Option<&Fail> {
        self.inner.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.inner.backtrace()
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&self.inner, f)
    }
}

impl std::convert::From<ErrorKind> for Error {
    fn from(kind: ErrorKind) -> Self {
        return Self {
            inner: Context::new(kind)
        };
    }
}


impl From<TagErrorKind> for Error {
    fn from(kind: TagErrorKind) -> Self {
        return Self {
            inner: Context::new(ErrorKind::InvalidTag(Box::new(kind)))
        };
    }
}

impl From<ValueErrorKind> for Error {
    fn from(kind: ValueErrorKind) -> Self {
        return Self {
            inner: Context::new(ErrorKind::InvalidValue(Box::new(kind)))
        };
    }
}

impl From<LengthErrorKind> for Error {
    fn from(kind: LengthErrorKind) -> Self {
        return Self {
            inner: Context::new(ErrorKind::InvalidLength(Box::new(kind)))
        };
    }
}

impl std::convert::From<Context<ErrorKind>> for Error {
    fn from(inner: Context<ErrorKind>) -> Self {
        return Self { inner };
    }
}

impl From<std::str::Utf8Error> for Error {
    fn from(_inner: std::str::Utf8Error) -> Self {
        return Self::from(ValueErrorKind::Utf8Error);
    }
}

impl From<std::string::FromUtf8Error> for Error {
    fn from(_inner: std::string::FromUtf8Error) -> Self {
        return Self::from(ValueErrorKind::Utf8Error);
    }
}

impl From<std::num::ParseIntError> for Error {
    fn from(_inner: std::num::ParseIntError) -> Self {
        return Self::from(ValueErrorKind::ParseIntError);
    }
}

impl From<ascii::ToAsciiCharError> for Error {
    fn from(_inner: ascii::ToAsciiCharError) -> Self {
        return Self::from(ValueErrorKind::AsciiError);
    }
}