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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use crate::EncryptableKind;
use std::error::Error;
use std::fmt;
use std::io;
use std::string::FromUtf8Error;

/// High level error enum for when a single error type like [`DecryptError`] is too specific.
///
/// [`DecryptError`]: ./DecryptError.enum.html
#[derive(Debug)]
pub enum EnconError {
    Encrypt(EncryptError),
    Decrypt(DecryptError),
    ApplyIntent {
        target_kind: EncryptableKind,
        source: Box<dyn Error + Send>,
    },
    DecryptAll {
        good_keys: Vec<String>,
        bad_keys: Vec<String>,
        source: Box<dyn Error + Send>,
    },
    MapToJson(MapToJsonError),
}

impl EnconError {
    pub(crate) fn apply_intent(self, target_kind: EncryptableKind) -> Self {
        EnconError::ApplyIntent {
            target_kind,
            source: Box::new(self),
        }
    }
}

impl From<EncryptError> for EnconError {
    fn from(source: EncryptError) -> Self {
        Self::Encrypt(source)
    }
}

impl From<DecryptError> for EnconError {
    fn from(source: DecryptError) -> Self {
        Self::Decrypt(source)
    }
}

impl fmt::Display for EnconError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Encrypt(_) => write!(f, "An error ocurred while encrypting"),
            Self::Decrypt(_) => write!(f, "An error ocurred while decrypting"),
            Self::ApplyIntent { target_kind, .. } => {
                write!(f, "Failed to transition the field to {}", target_kind)
            }
            Self::DecryptAll {
                good_keys,
                bad_keys,
                ..
            } => {
                if bad_keys.is_empty() {
                    write!(
                        f,
                        "Unexpected case where decrypt-all failed, but no failed keys were found."
                    )
                } else if good_keys.is_empty() {
                    write!(f, "Decrypt-all failed for all keys: {:?}.", bad_keys)
                } else {
                    write!(
                        f,
                        "Decrypt-all failed for some keys. Good: {:?}. Bad: {:?}",
                        good_keys, bad_keys
                    )
                }
            }
            Self::MapToJson(ref err) => match err {
                MapToJsonError::ApplyRequired => write!(
                    f,
                    "Converting the Map to JSON failed due to unapplied intents"
                ),
                _ => write!(
                    f,
                    "Converting the Map to JSON failed for a reason other than unapplied intents"
                ),
            },
        }
    }
}

impl Error for EnconError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Encrypt(source) => Some(&*source),
            Self::Decrypt(source) => Some(&*source),
            Self::ApplyIntent { source, .. } => Some(source.as_ref()),
            Self::DecryptAll { source, .. } => Some(source.as_ref()),
            Self::MapToJson(source) => Some(&*source),
        }
    }

    // fn backtrace(&self) -> Option<&std::backtrace::Backtrace> {
    //     None
    // }
}

/// An error that may arise during encryption. Generally it's expected that encryption doesn't
/// fail.
///
/// If it does you can match on the error, and for the `Write` and `Serialize` variants,
/// there's an underlying source value (see the `std::error::Error` trait).
#[derive(Debug)]
pub enum EncryptError {
    Init,
    EncryptChunk,
    Write { source: io::Error },
    Serialize { source: serde_json::Error },
}

impl EncryptError {
    pub(crate) fn write(source: io::Error) -> Self {
        Self::Write { source }
    }

    pub(crate) fn serialize(source: serde_json::Error) -> Self {
        Self::Serialize { source }
    }
}

impl fmt::Display for EncryptError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Init => "Unable to initialize the encryption",
                Self::EncryptChunk => "Unable to encrypt a chunk",
                Self::Write { .. } => "An underlying IO error ocurred during encryption",
                Self::Serialize { .. } => "Failed to serialize the value to JSON",
            }
        )
    }
}

impl Error for EncryptError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Write { source } => Some(&*source),
            Self::Serialize { source } => Some(&*source),
            _ => None,
        }
    }

    // fn backtrace(&self) -> Option<&std::backtrace::Backtrace> {
    //     None
    // }
}

/// An error that may arise during decryption.
///
/// The main cases you want to handle are `LikelyWrongPassword` and `InputTooShort`. The
/// former indicates the password is wrong, and the latter indicates that the encrypted
/// blob is invalid.
///
/// For the `Write`, `Serialize`, and `Utf8` variants,
/// there's an underlying source value (see the `std::error::Error` trait).
#[derive(Debug)]
pub enum DecryptError {
    InputTooShort,
    DeriveKey,
    Init,
    LikelyWrongPassword,
    Write { source: io::Error },
    Deserialize { source: serde_json::Error },
    Utf8 { source: FromUtf8Error },
}

impl DecryptError {
    pub(crate) fn write(source: io::Error) -> Self {
        Self::Write { source }
    }

    pub(crate) fn deserialize(source: serde_json::Error) -> Self {
        Self::Deserialize { source }
    }

    pub(crate) fn utf8(source: FromUtf8Error) -> Self {
        Self::Utf8 { source }
    }
}

impl fmt::Display for DecryptError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::InputTooShort => "The provided input is too short to decrypt",
                Self::DeriveKey => "Unable to derive a decryption key from the provided password",
                Self::Init => "Unable to initialize the decryption",
                Self::LikelyWrongPassword => "Unable to decrypt, likely due to an invalid password",
                Self::Write { .. } => "An underlying IO error ocurred during decryption",
                Self::Deserialize { .. } =>
                    "Failed to deserialize the encrypted value (invalid JSON)",
                Self::Utf8 { .. } => "The decrypted data produced invalid UTF8 bytes",
            }
        )
    }
}

impl Error for DecryptError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Write { source } => Some(&*source),
            Self::Deserialize { source } => Some(&*source),
            Self::Utf8 { source } => Some(&*source),
            _ => None,
        }
    }

    // fn backtrace(&self) -> Option<&std::backtrace::Backtrace> {
    //     None
    // }
}

/// An error arising from converting a `Map` to JSON (either pretty or compact)
///
/// The `ApplyRequired` variant means you need to call `map.apply_all_intents(password)?`
/// before the `to_json` methods.
#[derive(Debug)]
pub enum MapToJsonError {
    ApplyRequired,
    Serde(serde_json::Error),
}

impl fmt::Display for MapToJsonError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ApplyRequired => write!(
                f,
                "You must apply all intents before converting a Map to JSON"
            ),
            Self::Serde(_) => write!(f, "A serialization error ocurred"),
        }
    }
}

impl Error for MapToJsonError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Serde(source) => Some(&*source),
            Self::ApplyRequired => None,
        }
    }
}