1use aes_ctr::cipher::stream::InvalidKeyNonceLength;
2use block_padding::{PadError, UnpadError};
3use hmac::crypto_mac::MacError;
4use std::error::Error;
5use std::fmt;
6
7pub type Result<T> = std::result::Result<T, VaultError>;
9
10#[derive(Debug)]
12pub struct VaultError {
13 pub kind: ErrorKind,
14 pub message: String,
15}
16
17#[derive(Debug, PartialEq)]
18pub enum ErrorKind {
19 Error,
20 IoError,
21 NotAVault,
22 InvalidFormat,
23 IncorrectSecret,
24}
25
26fn kind_message(kind: &ErrorKind) -> &str {
27 match kind {
28 ErrorKind::Error => "Error !",
29 ErrorKind::IoError => "Io error",
30 ErrorKind::NotAVault => "Input is not a vault",
31 ErrorKind::InvalidFormat => "Invalid data format : vault is incorrect",
32 ErrorKind::IncorrectSecret => "Invalid secret",
33 }
34}
35
36impl VaultError {
37 pub fn new(kind: ErrorKind, message: &str) -> Self {
38 VaultError {
39 kind,
40 message: message.to_string(),
41 }
42 }
43
44 pub fn from_kind(kind: ErrorKind) -> Self {
45 let msg = kind_message(&kind).to_string();
46
47 VaultError { kind, message: msg }
48 }
49
50 pub fn from_string(message: &str) -> Self {
51 VaultError {
52 kind: ErrorKind::Error,
53 message: message.to_string(),
54 }
55 }
56}
57
58impl fmt::Display for VaultError {
59 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60 write!(f, "{}", self.message)
61 }
62}
63
64impl Error for VaultError {
65 fn description(&self) -> &str {
66 &self.message
67 }
68}
69
70impl From<&str> for VaultError {
71 fn from(s: &str) -> Self {
72 VaultError::from_string(s)
73 }
74}
75
76impl std::cmp::PartialEq for VaultError {
77 fn eq(&self, other: &VaultError) -> bool {
78 self.kind.eq(&other.kind)
79 }
80}
81
82impl From<std::io::Error> for VaultError {
83 fn from(error: std::io::Error) -> Self {
84 VaultError::new(ErrorKind::IoError, &error.to_string())
85 }
86}
87
88impl From<std::string::FromUtf8Error> for VaultError {
89 fn from(error: std::string::FromUtf8Error) -> Self {
90 VaultError::new(ErrorKind::InvalidFormat, &error.to_string())
91 }
92}
93
94impl From<hex::FromHexError> for VaultError {
95 fn from(error: hex::FromHexError) -> Self {
96 VaultError::new(ErrorKind::InvalidFormat, &error.to_string())
97 }
98}
99
100impl From<hmac::crypto_mac::InvalidKeyLength> for VaultError {
101 fn from(error: hmac::crypto_mac::InvalidKeyLength) -> Self {
102 VaultError::new(ErrorKind::InvalidFormat, &error.to_string())
103 }
104}
105
106impl From<MacError> for VaultError {
107 fn from(error: MacError) -> Self {
108 VaultError::new(ErrorKind::IncorrectSecret, &error.to_string())
109 }
110}
111
112impl From<PadError> for VaultError {
113 fn from(_error: PadError) -> Self {
114 VaultError::new(ErrorKind::InvalidFormat, "Padding error")
115 }
116}
117
118impl From<UnpadError> for VaultError {
119 fn from(_error: UnpadError) -> Self {
120 VaultError::new(ErrorKind::InvalidFormat, "Padding error")
121 }
122}
123
124impl From<InvalidKeyNonceLength> for VaultError {
125 fn from(error: InvalidKeyNonceLength) -> Self {
126 VaultError::new(ErrorKind::InvalidFormat, &error.to_string())
127 }
128}