use std::error::Error;
use std::fmt::{
self,
Display,
Formatter,
};
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum IdError {
HostOutOfRange {
host: u64,
max: u64,
},
NodeOutOfRange {
node_id: u64,
max: u64,
},
MachineIdOutOfRange {
machine_id: u64,
max: u64,
},
TimestampOverflow {
timestamp: u64,
max: u64,
},
SequenceOverflow {
sequence: u64,
max: u64,
},
ClockMovedBackwards {
last_timestamp: u64,
current_timestamp: u64,
skew_millis: u64,
max_skew_millis: u64,
},
TimeBeforeEpoch,
StartTimeAhead,
InvalidBitLength {
name: &'static str,
bits: u8,
reason: &'static str,
},
InvalidTimeUnit {
nanos: u128,
min_nanos: u128,
},
RandomSourceUnavailable,
StatePoisoned,
}
impl Display for IdError {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::HostOutOfRange { host, max } => {
write!(formatter, "host id {host} is out of range 0..={max}")
}
Self::NodeOutOfRange { node_id, max } => {
write!(formatter, "node id {node_id} is out of range 0..={max}")
}
Self::MachineIdOutOfRange { machine_id, max } => {
write!(
formatter,
"machine id {machine_id} is out of range 0..={max}"
)
}
Self::TimestampOverflow { timestamp, max } => {
write!(formatter, "timestamp {timestamp} exceeds maximum {max}")
}
Self::SequenceOverflow { sequence, max } => {
write!(formatter, "sequence {sequence} exceeds maximum {max}")
}
Self::ClockMovedBackwards {
last_timestamp,
current_timestamp,
skew_millis,
max_skew_millis,
} => write!(
formatter,
"clock moved backwards from {last_timestamp} to {current_timestamp}; \
skew {skew_millis} ms exceeds maximum {max_skew_millis} ms"
),
Self::TimeBeforeEpoch => {
write!(formatter, "time is before the configured epoch")
}
Self::StartTimeAhead => {
write!(formatter, "start time is ahead of the generator clock")
}
Self::InvalidBitLength { name, bits, reason } => {
write!(formatter, "invalid bit length for {name}: {bits}; {reason}")
}
Self::InvalidTimeUnit { nanos, min_nanos } => {
write!(
formatter,
"invalid time unit {nanos} ns; minimum is {min_nanos} ns"
)
}
Self::RandomSourceUnavailable => {
write!(formatter, "operating system random source is unavailable")
}
Self::StatePoisoned => {
write!(formatter, "generator state mutex is poisoned")
}
}
}
}
impl Error for IdError {}