tightbeam-rs 0.8.0

A secure, high-performance messaging protocol library
Documentation
//! Cluster-specific error types

/// Static error message for no heartbeat response
pub const NO_RESPONSE_MSG: &[u8] = b"no response";

/// Static error message for transport errors
pub const TRANSPORT_ERROR_MSG: &[u8] = b"transport error";

/// Errors specific to clusters
#[cfg_attr(feature = "derive", derive(crate::Errorizable))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ClusterError {
	/// Lock poisoned
	#[cfg_attr(feature = "derive", error("Lock poisoned"))]
	LockPoisoned,
	/// Unknown servlet type
	#[cfg_attr(feature = "derive", error("Unknown servlet type: {:#?}"))]
	UnknownServletType(Vec<u8>),
	/// No hives available for servlet type
	#[cfg_attr(feature = "derive", error("No hives available for servlet type: {:#?}"))]
	NoHivesAvailable(Vec<u8>),
	/// Hive communication failed
	#[cfg_attr(feature = "derive", error("Hive communication failed: {:#?}"))]
	HiveCommunicationFailed(Vec<u8>),
	/// Registration failed
	#[cfg_attr(feature = "derive", error("Registration failed"))]
	RegistrationFailed,
	/// Frame encoding error
	#[cfg_attr(feature = "derive", error("Frame encoding error"))]
	EncodingError,
	/// Frame signing error
	#[cfg_attr(feature = "derive", error("Frame signing error"))]
	SigningError,
	/// Invalid servlet address format
	#[cfg_attr(feature = "derive", error("Invalid address: {:#?}"))]
	InvalidAddress(Vec<u8>),
}

#[cfg(not(feature = "derive"))]
impl core::fmt::Display for ClusterError {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		match self {
			ClusterError::LockPoisoned => write!(f, "Lock poisoned"),
			ClusterError::UnknownServletType(t) => {
				write!(f, "Unknown servlet type: {}", String::from_utf8_lossy(t))
			}
			ClusterError::NoHivesAvailable(t) => {
				write!(f, "No hives available for servlet type: {}", String::from_utf8_lossy(t))
			}
			ClusterError::HiveCommunicationFailed(msg) => {
				write!(f, "Hive communication failed: {}", String::from_utf8_lossy(msg))
			}
			ClusterError::RegistrationFailed => write!(f, "Registration failed"),
			ClusterError::EncodingError => write!(f, "Frame encoding error"),
			ClusterError::SigningError => write!(f, "Frame signing error"),
			ClusterError::InvalidAddress(addr) => {
				write!(f, "Invalid address: {}", String::from_utf8_lossy(addr))
			}
		}
	}
}

#[cfg(not(feature = "derive"))]
impl core::error::Error for ClusterError {}

impl<T> From<std::sync::PoisonError<T>> for ClusterError {
	fn from(_: std::sync::PoisonError<T>) -> Self {
		ClusterError::LockPoisoned
	}
}

impl From<crate::transport::error::TransportError> for ClusterError {
	fn from(_: crate::transport::error::TransportError) -> Self {
		ClusterError::HiveCommunicationFailed(TRANSPORT_ERROR_MSG.to_vec())
	}
}

impl From<crate::TightBeamError> for ClusterError {
	fn from(_: crate::TightBeamError) -> Self {
		ClusterError::EncodingError
	}
}