// SPDX-License-Identifier: MIT-0 use std::{fmt, io}; use chacha20poly1305::aead; #[derive(Debug)] pub enum FcryError { Io(io::Error), Crypto(aead::Error), Rng(getrandom::Error), Format(String), Kdf(String), Passphrase(String), WrongKey, } impl fmt::Display for FcryError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Io(e) => write!(f, "I/O error: {e}"), Self::Crypto(_) => write!(f, "cryptographic authentication failed"), Self::Rng(e) => write!(f, "randomness error: {e}"), Self::Format(msg) => write!(f, "format error: {msg}"), Self::Kdf(msg) => write!(f, "KDF error: {msg}"), Self::Passphrase(msg) => write!(f, "passphrase error: {msg}"), Self::WrongKey => write!(f, "wrong key or passphrase"), } } } impl std::error::Error for FcryError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { Self::Io(e) => Some(e), Self::Rng(e) => Some(e), Self::Crypto(_) | Self::Format(_) | Self::Kdf(_) | Self::Passphrase(_) | Self::WrongKey => None, } } } impl From for FcryError { fn from(e: io::Error) -> Self { FcryError::Io(e) } } impl From for FcryError { fn from(e: aead::Error) -> Self { FcryError::Crypto(e) } } impl From for FcryError { fn from(e: getrandom::Error) -> Self { FcryError::Rng(e) } } impl From for FcryError { fn from(e: argon2::Error) -> Self { FcryError::Kdf(e.to_string()) } }