use thiserror::Error;
pub type Result<T> = std::result::Result<T, PhalanxError>;
#[derive(Error, Debug)]
pub enum PhalanxError {
#[error("Cryptographic error: {0}")]
Crypto(String),
#[error("Protocol error: {0}")]
Protocol(String),
#[error("Group error: {0}")]
Group(String),
#[error("Authentication failed: {0}")]
Authentication(String),
#[error("Key derivation error: {0}")]
KeyDerivation(String),
#[error("Encryption error: {0}")]
Encryption(String),
#[error("Membership error: {0}")]
Membership(String),
#[error("Version error: {0}")]
Version(String),
#[cfg(feature = "serde")]
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
}
impl PhalanxError {
pub fn crypto(msg: impl Into<String>) -> Self {
Self::Crypto(msg.into())
}
pub fn protocol(msg: impl Into<String>) -> Self {
Self::Protocol(msg.into())
}
pub fn group(msg: impl Into<String>) -> Self {
Self::Group(msg.into())
}
pub fn auth(msg: impl Into<String>) -> Self {
Self::Authentication(msg.into())
}
pub fn key_derivation(msg: impl Into<String>) -> Self {
Self::KeyDerivation(msg.into())
}
pub fn encryption(msg: impl Into<String>) -> Self {
Self::Encryption(msg.into())
}
pub fn membership(msg: impl Into<String>) -> Self {
Self::Membership(msg.into())
}
pub fn version(msg: impl Into<String>) -> Self {
Self::Version(msg.into())
}
}
impl From<chacha20poly1305::Error> for PhalanxError {
fn from(err: chacha20poly1305::Error) -> Self {
PhalanxError::crypto(format!("ChaCha20Poly1305 error: {}", err))
}
}
impl From<ed25519_dalek::SignatureError> for PhalanxError {
fn from(err: ed25519_dalek::SignatureError) -> Self {
PhalanxError::auth(format!("Ed25519 signature error: {}", err))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_creation() {
let err = PhalanxError::crypto("Test crypto error");
assert_eq!(err.to_string(), "Cryptographic error: Test crypto error");
let err = PhalanxError::protocol("Test protocol error");
assert_eq!(err.to_string(), "Protocol error: Test protocol error");
}
}