use std::fmt;
use std::io;
use crate::{wfl, DecryptError};
#[cfg(feature = "plugin")]
use crate::wlnfl;
#[derive(Debug)]
pub enum ReadError {
EncryptedIdentities(DecryptError),
IdentityEncryptedWithoutPassphrase(String),
IdentityNotFound(String),
InvalidRecipient(String),
InvalidRecipientsFile {
filename: String,
line_number: usize,
},
Io(io::Error),
#[cfg(feature = "plugin")]
#[cfg_attr(docsrs, doc(cfg(feature = "plugin")))]
MissingPlugin {
binary_name: String,
},
MissingRecipientsFile(String),
MultipleStdin,
#[cfg(feature = "ssh")]
#[cfg_attr(docsrs, doc(cfg(feature = "ssh")))]
RsaModulusTooLarge,
#[cfg(feature = "ssh")]
#[cfg_attr(docsrs, doc(cfg(feature = "ssh")))]
RsaModulusTooSmall,
#[cfg(feature = "ssh")]
#[cfg_attr(docsrs, doc(cfg(feature = "ssh")))]
UnsupportedKey(String, crate::ssh::UnsupportedKey),
}
impl From<io::Error> for ReadError {
fn from(e: io::Error) -> Self {
ReadError::Io(e)
}
}
impl fmt::Display for ReadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ReadError::EncryptedIdentities(e) => e.fmt(f),
ReadError::IdentityEncryptedWithoutPassphrase(filename) => {
wfl!(
f,
"err-read-identity-encrypted-without-passphrase",
filename = filename.as_str(),
)
}
ReadError::IdentityNotFound(filename) => wfl!(
f,
"err-read-identity-not-found",
filename = filename.as_str(),
),
ReadError::InvalidRecipient(recipient) => wfl!(
f,
"err-read-invalid-recipient",
recipient = recipient.as_str(),
),
ReadError::InvalidRecipientsFile {
filename,
line_number,
} => wfl!(
f,
"err-read-invalid-recipients-file",
filename = filename.as_str(),
line_number = line_number,
),
ReadError::Io(e) => write!(f, "{}", e),
#[cfg(feature = "plugin")]
ReadError::MissingPlugin { binary_name } => {
wlnfl!(f, "err-missing-plugin", plugin_name = binary_name.as_str())?;
wfl!(f, "rec-missing-plugin")
}
ReadError::MissingRecipientsFile(filename) => wfl!(
f,
"err-read-missing-recipients-file",
filename = filename.as_str(),
),
ReadError::MultipleStdin => wfl!(f, "err-read-multiple-stdin"),
#[cfg(feature = "ssh")]
ReadError::RsaModulusTooLarge => {
wfl!(f, "err-read-rsa-modulus-too-large", max_size = 4096)
}
#[cfg(feature = "ssh")]
ReadError::RsaModulusTooSmall => {
wfl!(f, "err-read-rsa-modulus-too-small")
}
#[cfg(feature = "ssh")]
ReadError::UnsupportedKey(filename, k) => k.display(f, Some(filename.as_str())),
}
}
}
impl std::error::Error for ReadError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(inner) => Some(inner),
_ => None,
}
}
}