use std::fmt;
use crate::value::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AppendError<V: Value> {
TimestampBeforeBase { ts: u32, base_ts: u32 },
OutOfOrder {
ts: u32,
logical_idx: u32,
prev_logical_idx: u32,
},
IntervalOverflow { count: u16 },
CountOverflow,
DeltaOverflow {
delta: i32,
current_value: V,
new_value: V,
},
TimeSpanOverflow { ts: u32, base_ts: u32, max_intervals: u32 },
BufferTooShort { expected: usize, actual: usize },
MalformedData,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DecodeError {
BufferTooShort { expected: usize, actual: usize },
InvalidHeader,
MalformedData,
}
impl<V: Value> fmt::Display for AppendError<V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TimestampBeforeBase { ts, base_ts } => {
write!(f, "timestamp {ts} is before base timestamp {base_ts}")
}
Self::OutOfOrder {
ts,
logical_idx,
prev_logical_idx,
} => {
write!(
f,
"timestamp {ts} (interval {logical_idx}) is before previous interval {prev_logical_idx}"
)
}
Self::IntervalOverflow { count } => {
write!(f, "too many readings in interval ({count}), max is 1023")
}
Self::CountOverflow => write!(f, "too many total readings, max is 65535"),
Self::DeltaOverflow {
delta,
current_value,
new_value,
} => {
write!(
f,
"value delta {delta} ({} -> {}) exceeds range [-1024, 1023]",
current_value.to_i32(),
new_value.to_i32()
)
}
Self::TimeSpanOverflow { ts, base_ts, max_intervals } => {
write!(
f,
"timestamp {ts} exceeds maximum time span from base {base_ts} (max {max_intervals} intervals)"
)
}
Self::BufferTooShort { expected, actual } => {
write!(f, "buffer too short: expected at least {expected} bytes, got {actual}")
}
Self::MalformedData => write!(f, "encoded data is malformed or corrupted"),
}
}
}
impl<V: Value> std::error::Error for AppendError<V> {}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BufferTooShort { expected, actual } => {
write!(f, "buffer too short: expected at least {expected} bytes, got {actual}")
}
Self::InvalidHeader => write!(f, "invalid header in encoded data"),
Self::MalformedData => write!(f, "encoded data is malformed or corrupted"),
}
}
}
impl std::error::Error for DecodeError {}