sett 0.4.0

Rust port of sett (data compression, encryption and transfer tool).
Documentation
//! Error namespace

use super::cert;

/// Wrapper for sequoia_openpgp's opaque error type
#[derive(Debug)]
pub enum PgpError {
    /// General error
    Error(String),
    /// Missing key error
    VerificationError(VerificationError),
    /// Error during certificate handling.
    CertError(cert::error::CertError),
}

/// Error for verification errors from sequoia
#[derive(Debug)]
#[non_exhaustive]
pub enum VerificationError {
    /// General error
    Error(String),
    /// Missing key
    MissingKey {
        /// The fingerprints of the missing keys.
        fingerprint: Vec<crate::openpgp::cert::Fingerprint>,
    },
}

impl std::error::Error for VerificationError {}

impl std::fmt::Display for VerificationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            VerificationError::MissingKey { fingerprint } => match fingerprint.len() {
                0 => write!(f, "missing key for signature"),
                1 => write!(f, "missing key for signature: {}", fingerprint[0]),
                2.. => write!(f, "missing keys for signature: {fingerprint:?}"),
            },
            VerificationError::Error(e) => write!(f, "{e}"),
        }
    }
}

impl From<sequoia_openpgp::parse::stream::VerificationError<'_>> for VerificationError {
    fn from(value: sequoia_openpgp::parse::stream::VerificationError<'_>) -> Self {
        match value {
            sequoia_openpgp::parse::stream::VerificationError::MissingKey { sig } => {
                Self::MissingKey {
                    fingerprint: sig
                        .issuer_fingerprints()
                        .cloned()
                        .map(crate::openpgp::cert::Fingerprint)
                        .collect(),
                }
            }
            _ => Self::Error(value.to_string()),
        }
    }
}

impl std::fmt::Display for PgpError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PgpError::Error(e) => write!(f, "{e}"),
            PgpError::VerificationError(ve) => write!(f, "verification error - {ve}"),
            PgpError::CertError(e) => write!(f, "{e}"),
        }
    }
}

impl std::error::Error for PgpError {}

impl PgpError {
    pub(crate) fn from(error: impl std::fmt::Display) -> Self {
        Self::Error(error.to_string())
    }
}

impl From<cert::error::RevocationError> for PgpError {
    fn from(error: cert::error::RevocationError) -> Self {
        Self::CertError(cert::error::CertError::RevocationError(error))
    }
}

/// Error for OpenPGP related code.
#[derive(Debug)]
pub enum Error {
    /// pgp error
    Pgp(PgpError),
    /// Async task join error
    AsyncTask(tokio::task::JoinError),
    /// IO error
    IO(std::io::Error),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Pgp(e) => e.fmt(f),
            Self::AsyncTask(e) => e.fmt(f),
            Self::IO(e) => e.fmt(f),
        }
    }
}
impl std::error::Error for Error {}

impl From<PgpError> for Error {
    fn from(value: PgpError) -> Self {
        Self::Pgp(value)
    }
}

impl From<tokio::task::JoinError> for Error {
    fn from(value: tokio::task::JoinError) -> Self {
        Self::AsyncTask(value)
    }
}

impl From<std::io::Error> for Error {
    fn from(value: std::io::Error) -> Self {
        Self::IO(value)
    }
}