1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, PhalanxError>;
7
8#[derive(Error, Debug)]
10pub enum PhalanxError {
11 #[error("Cryptographic error: {0}")]
13 Crypto(String),
14
15 #[error("Protocol error: {0}")]
17 Protocol(String),
18
19 #[error("Group error: {0}")]
21 Group(String),
22
23 #[error("Authentication failed: {0}")]
25 Authentication(String),
26
27 #[error("Key derivation error: {0}")]
29 KeyDerivation(String),
30
31 #[error("Encryption error: {0}")]
33 Encryption(String),
34
35 #[error("Membership error: {0}")]
37 Membership(String),
38
39 #[error("Version error: {0}")]
41 Version(String),
42
43 #[cfg(feature = "serde")]
45 #[error("Serialization error: {0}")]
46 Serialization(#[from] serde_json::Error),
47
48 #[error("IO error: {0}")]
50 Io(#[from] std::io::Error),
51}
52
53impl PhalanxError {
54 pub fn crypto(msg: impl Into<String>) -> Self {
56 Self::Crypto(msg.into())
57 }
58
59 pub fn protocol(msg: impl Into<String>) -> Self {
61 Self::Protocol(msg.into())
62 }
63
64 pub fn group(msg: impl Into<String>) -> Self {
66 Self::Group(msg.into())
67 }
68
69 pub fn auth(msg: impl Into<String>) -> Self {
71 Self::Authentication(msg.into())
72 }
73
74 pub fn key_derivation(msg: impl Into<String>) -> Self {
76 Self::KeyDerivation(msg.into())
77 }
78
79 pub fn encryption(msg: impl Into<String>) -> Self {
81 Self::Encryption(msg.into())
82 }
83
84 pub fn membership(msg: impl Into<String>) -> Self {
86 Self::Membership(msg.into())
87 }
88
89 pub fn version(msg: impl Into<String>) -> Self {
91 Self::Version(msg.into())
92 }
93}
94
95impl 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}