phalanx_crypto/
error.rs

1//! Error types for Phalanx Protocol
2
3use thiserror::Error;
4
5/// Result type alias for Phalanx operations
6pub type Result<T> = std::result::Result<T, PhalanxError>;
7
8/// Comprehensive error types for all Phalanx operations
9#[derive(Error, Debug)]
10pub enum PhalanxError {
11    /// Cryptographic operation failed
12    #[error("Cryptographic error: {0}")]
13    Crypto(String),
14    
15    /// Invalid protocol message or format
16    #[error("Protocol error: {0}")]
17    Protocol(String),
18    
19    /// Group operation failed
20    #[error("Group error: {0}")]
21    Group(String),
22    
23    /// Authentication or signature verification failed
24    #[error("Authentication failed: {0}")]
25    Authentication(String),
26    
27    /// Key exchange or derivation failed
28    #[error("Key derivation error: {0}")]
29    KeyDerivation(String),
30    
31    /// Message encryption/decryption failed
32    #[error("Encryption error: {0}")]
33    Encryption(String),
34    
35    /// Invalid group membership or permissions
36    #[error("Membership error: {0}")]
37    Membership(String),
38    
39    /// Protocol version mismatch or unsupported
40    #[error("Version error: {0}")]
41    Version(String),
42    
43    /// Serialization/deserialization failed
44    #[cfg(feature = "serde")]
45    #[error("Serialization error: {0}")]
46    Serialization(#[from] serde_json::Error),
47    
48    /// Generic I/O error
49    #[error("IO error: {0}")]
50    Io(#[from] std::io::Error),
51}
52
53impl PhalanxError {
54    /// Create a new crypto error
55    pub fn crypto(msg: impl Into<String>) -> Self {
56        Self::Crypto(msg.into())
57    }
58    
59    /// Create a new protocol error
60    pub fn protocol(msg: impl Into<String>) -> Self {
61        Self::Protocol(msg.into())
62    }
63    
64    /// Create a new group error
65    pub fn group(msg: impl Into<String>) -> Self {
66        Self::Group(msg.into())
67    }
68    
69    /// Create a new authentication error
70    pub fn auth(msg: impl Into<String>) -> Self {
71        Self::Authentication(msg.into())
72    }
73    
74    /// Create a new key derivation error
75    pub fn key_derivation(msg: impl Into<String>) -> Self {
76        Self::KeyDerivation(msg.into())
77    }
78    
79    /// Create a new encryption error
80    pub fn encryption(msg: impl Into<String>) -> Self {
81        Self::Encryption(msg.into())
82    }
83    
84    /// Create a new membership error
85    pub fn membership(msg: impl Into<String>) -> Self {
86        Self::Membership(msg.into())
87    }
88    
89    /// Create a new version error
90    pub fn version(msg: impl Into<String>) -> Self {
91        Self::Version(msg.into())
92    }
93}
94
95/// Convert from various cryptographic library errors
96impl From<chacha20poly1305::Error> for PhalanxError {
97    fn from(err: chacha20poly1305::Error) -> Self {
98        PhalanxError::crypto(format!("ChaCha20Poly1305 error: {}", err))
99    }
100}
101
102impl From<ed25519_dalek::SignatureError> for PhalanxError {
103    fn from(err: ed25519_dalek::SignatureError) -> Self {
104        PhalanxError::auth(format!("Ed25519 signature error: {}", err))
105    }
106}
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111    
112    #[test]
113    fn test_error_creation() {
114        let err = PhalanxError::crypto("Test crypto error");
115        assert_eq!(err.to_string(), "Cryptographic error: Test crypto error");
116        
117        let err = PhalanxError::protocol("Test protocol error");
118        assert_eq!(err.to_string(), "Protocol error: Test protocol error");
119    }
120}