use std::fmt;
use std::io;
#[derive(Debug)]
pub enum RingfireError {
Io(io::Error),
InvalidCapacity(u64),
InvalidMagic { expected: u64, actual: u64 },
VersionMismatch { expected: u32, actual: u32 },
ElementSizeMismatch { expected: usize, actual: usize },
ProducerAlreadyExists,
KeyOutOfRange { key: usize, capacity: usize },
ValueSizeMismatch { expected: usize, actual: usize },
SchemaMismatch {
expected: u64,
actual: u64,
type_name: &'static str,
},
ArenaPayloadTooLarge { len: usize, max_capacity: usize },
BufferTooSmall { required: usize, provided: usize },
NoAvailableReaderSlots,
NoOffsetFileConfigured,
BackpressureBufferFull,
CorruptLayout(&'static str),
WriterStalled { key: usize },
Unsupported(&'static str),
Protocol(&'static str),
}
impl fmt::Display for RingfireError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(err) => write!(f, "I/O error: {}", err),
Self::InvalidCapacity(cap) => {
write!(f, "Capacity {} is invalid: must be a power of two", cap)
}
Self::InvalidMagic { expected, actual } => write!(
f,
"Invalid magic header: expected 0x{:016X}, got 0x{:016X}",
expected, actual
),
Self::VersionMismatch { expected, actual } => {
write!(f, "Version mismatch: expected {}, got {}", expected, actual)
}
Self::ElementSizeMismatch { expected, actual } => write!(
f,
"Element size mismatch: expected {} bytes, got {} bytes",
expected, actual
),
Self::ProducerAlreadyExists => {
write!(f, "Exclusive producer already exists for this shared memory path")
}
Self::KeyOutOfRange { key, capacity } => {
write!(f, "Blackboard key {} out of range (capacity: {})", key, capacity)
}
Self::ValueSizeMismatch { expected, actual } => write!(
f,
"Blackboard value size mismatch: expected {} bytes, got {} bytes",
expected, actual
),
Self::SchemaMismatch { expected, actual, type_name } => write!(
f,
"Schema signature mismatch for type '{}': expected 0x{:016X}, got 0x{:016X}",
type_name, expected, actual
),
Self::ArenaPayloadTooLarge { len, max_capacity } => write!(
f,
"Payload size ({} bytes) exceeds arena capacity ({} bytes)",
len, max_capacity
),
Self::BufferTooSmall { required, provided } => write!(
f,
"Output buffer too small: required {} bytes, provided {} bytes",
required, provided
),
Self::NoAvailableReaderSlots => {
write!(f, "Reader registry full: no free slots available")
}
Self::NoOffsetFileConfigured => {
write!(f, "No offset file configured for this consumer")
}
Self::BackpressureBufferFull => {
write!(f, "Ring buffer is full: slowest active reader has not caught up")
}
Self::CorruptLayout(what) => write!(f, "Corrupt shared memory layout: {}", what),
Self::WriterStalled { key } => {
write!(f, "Blackboard key {} stayed mid-write: writer stalled or crashed", key)
}
Self::Unsupported(what) => write!(f, "Unsupported: {}", what),
Self::Protocol(what) => write!(f, "Replication protocol error: {}", what),
}
}
}
impl std::error::Error for RingfireError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(err) => Some(err),
_ => None,
}
}
}
impl From<io::Error> for RingfireError {
fn from(err: io::Error) -> Self {
Self::Io(err)
}
}
pub type Result<T> = std::result::Result<T, RingfireError>;