use crate::pki::EpfPkiCertificateValidationError; use std::error::Error; use std::fmt::{Display, Formatter}; #[derive(Debug)] pub enum EpfHandshakeError { AlreadyTunnelled, UnsupportedProtocolVersion(usize), InvalidCertificate(EpfPkiCertificateValidationError), UntrustedCertificate, EncryptionError, MissingKeyProof, } impl Display for EpfHandshakeError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { EpfHandshakeError::AlreadyTunnelled => write!(f, "Already tunneled"), EpfHandshakeError::UnsupportedProtocolVersion(v) => { write!(f, "Unsupported protocol version {}", v) } EpfHandshakeError::InvalidCertificate(e) => write!(f, "Invalid certificate: {}", e), EpfHandshakeError::UntrustedCertificate => { write!(f, "Certificate valid but not trusted") } EpfHandshakeError::EncryptionError => write!(f, "Encryption error"), EpfHandshakeError::MissingKeyProof => write!(f, "Missing key proof"), } } } impl Error for EpfHandshakeError {} #[cfg(test)] mod tests { use crate::error::EpfHandshakeError; use crate::pki::EpfPkiCertificateValidationError; #[test] pub fn error_display_test() { println!("{}", EpfHandshakeError::AlreadyTunnelled); println!("{}", EpfHandshakeError::UnsupportedProtocolVersion(0)); println!("{}", EpfHandshakeError::InvalidCertificate(EpfPkiCertificateValidationError::ValidAfterSigner)); println!("{}", EpfHandshakeError::UntrustedCertificate); println!("{}", EpfHandshakeError::EncryptionError); println!("{}", EpfHandshakeError::MissingKeyProof); } }