use super::types::ErrorBody;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum LibrageError {
#[error("Key generation failed: {0}")]
KeyGenFailed(String),
#[error("Serialization failed: {0}")]
SerializationFailed(String),
#[error("Invalid public key: {0}")]
InvalidPublicKey(String),
#[error("Invalid secret key: {0}")]
InvalidSecretKey(String),
#[error("Invalid SSH key: {0}")]
InvalidSshKey(String),
#[error("Encryption failed: {0}")]
EncryptionFailed(String),
#[error("Decryption failed: {0}")]
DecryptionFailed(String),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Invalid input: {0}")]
InvalidInput(String),
#[error("Armor error: {0}")]
ArmorError(String),
#[error("Excessive work factor: required {required}, target {target}")]
ExcessiveWork { required: u8, target: u8 },
#[error("No matching keys")]
NoMatchingKeys,
#[error("Missing recipients")]
MissingRecipients,
#[error("Mixed recipient and passphrase")]
MixedRecipientAndPassphrase,
#[error("Unknown format")]
UnknownFormat,
}
impl From<LibrageError> for ErrorBody {
fn from(e: LibrageError) -> Self {
match e {
LibrageError::KeyGenFailed(m) => ErrorBody::new("KEYGEN_FAILED", m),
LibrageError::SerializationFailed(m) => ErrorBody::new("SERIALIZATION_FAILED", m),
LibrageError::InvalidPublicKey(m) => ErrorBody::new("INVALID_PUBLIC_KEY", m),
LibrageError::InvalidSecretKey(m) => ErrorBody::new("INVALID_SECRET_KEY", m),
LibrageError::InvalidSshKey(m) => ErrorBody::new("INVALID_SSH_KEY", m),
LibrageError::EncryptionFailed(m) => ErrorBody::new("ENCRYPTION_FAILED", m),
LibrageError::DecryptionFailed(m) => ErrorBody::new("DECRYPTION_FAILED", m),
LibrageError::Io(e) => ErrorBody::new("IO_ERROR", e.to_string()),
LibrageError::InvalidInput(m) => ErrorBody::new("INVALID_INPUT", m),
LibrageError::ArmorError(m) => ErrorBody::new("ARMOR_ERROR", m),
LibrageError::ExcessiveWork { required, target } => ErrorBody::new(
"EXCESSIVE_WORK",
format!("required: {}, target: {}", required, target),
),
LibrageError::NoMatchingKeys => {
ErrorBody::new("NO_MATCHING_KEYS", "No matching keys found")
}
LibrageError::MissingRecipients => {
ErrorBody::new("MISSING_RECIPIENTS", "Missing recipients")
}
LibrageError::MixedRecipientAndPassphrase => ErrorBody::new(
"MIXED_RECIPIENT_PASSPHRASE",
"Mixed recipient and passphrase",
),
LibrageError::UnknownFormat => ErrorBody::new("UNKNOWN_FORMAT", "Unknown format"),
}
}
}
impl From<age::EncryptError> for LibrageError {
fn from(e: age::EncryptError) -> Self {
match e {
age::EncryptError::EncryptedIdentities(de) => {
LibrageError::DecryptionFailed(de.to_string())
}
age::EncryptError::IncompatibleRecipients { .. } => {
LibrageError::EncryptionFailed(e.to_string())
}
age::EncryptError::InvalidRecipientLabels(_) => {
LibrageError::EncryptionFailed(e.to_string())
}
age::EncryptError::Io(io) => LibrageError::Io(io),
age::EncryptError::MissingRecipients => LibrageError::MissingRecipients,
age::EncryptError::MixedRecipientAndPassphrase => {
LibrageError::MixedRecipientAndPassphrase
}
_ => LibrageError::EncryptionFailed(e.to_string()),
}
}
}
impl From<age::DecryptError> for LibrageError {
fn from(e: age::DecryptError) -> Self {
match e {
age::DecryptError::DecryptionFailed => {
LibrageError::DecryptionFailed("Decryption failed".into())
}
age::DecryptError::ExcessiveWork { required, target } => {
LibrageError::ExcessiveWork { required, target }
}
age::DecryptError::InvalidHeader => {
LibrageError::DecryptionFailed("Invalid header".into())
}
age::DecryptError::InvalidMac => LibrageError::DecryptionFailed("Invalid MAC".into()),
age::DecryptError::Io(io) => LibrageError::Io(io),
age::DecryptError::KeyDecryptionFailed => {
LibrageError::DecryptionFailed("Key decryption failed".into())
}
age::DecryptError::NoMatchingKeys => LibrageError::NoMatchingKeys,
age::DecryptError::UnknownFormat => LibrageError::UnknownFormat,
_ => LibrageError::DecryptionFailed(e.to_string()),
}
}
}
impl From<age::IdentityFileConvertError> for LibrageError {
fn from(e: age::IdentityFileConvertError) -> Self {
LibrageError::InvalidInput(e.to_string())
}
}