anubis-age 1.4.0

Post-quantum secure encryption library with hybrid X25519+ML-KEM-1024 mode (internal dependency for anubis-rage)
Documentation
use std::fmt;
use std::io;

use crate::{wfl, DecryptError};

#[cfg(feature = "plugin")]
use crate::wlnfl;

/// Errors that can occur while reading recipients or identities.
#[derive(Debug)]
pub enum ReadError {
    /// An error occured while decrypting passphrase-encrypted identities.
    EncryptedIdentities(DecryptError),
    /// The given identity file could not be found.
    IdentityNotFound(String),
    /// The given recipient string is invalid.
    InvalidRecipient(String),
    /// A recipients file contains non-recipient data.
    InvalidRecipientsFile {
        /// The given recipients file.
        filename: String,
        /// The first line containing non-recipient data.
        line_number: usize,
    },
    /// A recipient uses a legacy (non-Level-5) algorithm.
    NonLevel5Recipient(String),
    /// An identity file contains legacy (non-Level-5) identities.
    NonLevel5Identity {
        /// The given identity file.
        filename: Option<String>,
    },
    /// An I/O error occurred while reading.
    Io(io::Error),
    /// The given recipients file could not be found.
    MissingRecipientsFile(String),
    /// No recipients were provided for encryption.
    MissingRecipients,
    /// Standard input was used by multiple files.
    MultipleStdin,
}

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::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::NonLevel5Recipient(recipient) => wfl!(
                f,
                "err-read-non-level5-recipient",
                recipient = recipient.as_str(),
            ),
            ReadError::NonLevel5Identity { filename } => match filename {
                Some(filename) => wfl!(
                    f,
                    "err-read-non-level5-identity",
                    filename = filename.as_str(),
                ),
                None => wfl!(f, "err-read-non-level5-identity-stdin"),
            },
            ReadError::Io(e) => write!(f, "{}", e),
            ReadError::MissingRecipientsFile(filename) => wfl!(
                f,
                "err-read-missing-recipients-file",
                filename = filename.as_str(),
            ),
            ReadError::MissingRecipients => wfl!(f, "err-missing-recipients"),
            ReadError::MultipleStdin => wfl!(f, "err-read-multiple-stdin"),
        }
    }
}

impl std::error::Error for ReadError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(inner) => Some(inner),
            _ => None,
        }
    }
}