#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(feature = "std")]
use thiserror::Error;
#[cfg_attr(feature = "std", derive(Error))]
#[cfg_attr(feature = "bindings", derive(uniffi::Error))]
#[derive(Debug)]
#[non_exhaustive]
pub enum CoreError {
#[cfg_attr(feature = "std", error("Network I/O Error: {0}"))]
NetworkError(String),
#[cfg_attr(feature = "std", error("Serialization Error: {0}"))]
SerializationError(String),
#[cfg_attr(feature = "std", error("System Busy"))]
Busy,
#[cfg_attr(feature = "std", error("Invalid Configuration: {0}"))]
ConfigError(String),
#[cfg_attr(feature = "std", error("Cryptography Error: {0}"))]
CryptoError(String),
#[cfg_attr(feature = "std", error("Validation Error: {0}"))]
ValidationError(String),
#[cfg_attr(feature = "std", error("Runtime initialization failed: {0}"))]
RuntimeError(String),
#[cfg_attr(feature = "std", error("Key derivation failed"))]
KeyDerivationError,
#[cfg_attr(feature = "std", error("Random number generation failed: {0}"))]
RngError(String),
#[cfg_attr(feature = "std", error("Internal concurrency error: {0}"))]
InternalError(String),
#[cfg_attr(feature = "std", error("Handshake failed: {0}"))]
HandshakeError(String),
#[cfg_attr(feature = "std", error("Stream error: {0}"))]
StreamError(String),
#[cfg_attr(feature = "std", error("Session not found: {0}"))]
SessionNotFound(String),
#[cfg_attr(feature = "std", error("Connection closed"))]
ConnectionClosed,
#[cfg_attr(feature = "std", error("Timeout"))]
Timeout,
#[cfg_attr(feature = "std", error("replay protection rejected packet: {0}"))]
ReplayDetected(String),
#[cfg_attr(feature = "std", error("cipher suite unavailable: {0}"))]
CipherSuiteUnavailable(String),
#[cfg(feature = "fips")]
#[error("FIPS POST self-test failed: {0}")]
FipsSelfTestFailure(String),
}
#[cfg(feature = "std")]
impl core::convert::From<std::io::Error> for CoreError {
fn from(e: std::io::Error) -> Self {
CoreError::NetworkError(e.to_string())
}
}
#[cfg(feature = "std")]
impl From<getrandom::Error> for CoreError {
fn from(e: getrandom::Error) -> Self {
CoreError::RngError(e.to_string())
}
}
#[cfg(feature = "std")]
impl From<anyhow::Error> for CoreError {
fn from(e: anyhow::Error) -> Self {
CoreError::InternalError(e.to_string())
}
}
#[cfg(not(feature = "std"))]
impl core::fmt::Display for CoreError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::NetworkError(s) => write!(f, "Network I/O Error: {s}"),
Self::SerializationError(s) => write!(f, "Serialization Error: {s}"),
Self::Busy => write!(f, "System Busy"),
Self::ConfigError(s) => write!(f, "Invalid Configuration: {s}"),
Self::CryptoError(s) => write!(f, "Cryptography Error: {s}"),
Self::ValidationError(s) => write!(f, "Validation Error: {s}"),
Self::RuntimeError(s) => write!(f, "Runtime initialization failed: {s}"),
Self::KeyDerivationError => write!(f, "Key derivation failed"),
Self::RngError(s) => write!(f, "Random number generation failed: {s}"),
Self::InternalError(s) => write!(f, "Internal concurrency error: {s}"),
Self::HandshakeError(s) => write!(f, "Handshake failed: {s}"),
Self::StreamError(s) => write!(f, "Stream error: {s}"),
Self::SessionNotFound(s) => write!(f, "Session not found: {s}"),
Self::ConnectionClosed => write!(f, "Connection closed"),
Self::Timeout => write!(f, "Timeout"),
Self::ReplayDetected(s) => write!(f, "replay protection rejected packet: {s}"),
Self::CipherSuiteUnavailable(s) => write!(f, "cipher suite unavailable: {s}"),
}
}
}