use std::fmt;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
Redis(String),
UnexpectedReply(String),
NoGroup {
stream: String,
group: String,
},
MissingField(&'static str),
EmptyField(&'static str),
DuplicateField(String),
InvalidUtf8 {
field: String,
},
UnsupportedVersion(String),
InvalidValue {
field: String,
expected: &'static str,
},
ReservedField(String),
TooLarge {
what: &'static str,
limit: usize,
actual: usize,
},
InvalidEntryId(String),
StreamFull {
stream: String,
depth: u64,
target_depth: u64,
},
InvalidConfig(String),
Thread(String),
Timeout {
task_id: String,
timeout: std::time::Duration,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Redis(message) => write!(f, "redis: {message}"),
Self::UnexpectedReply(message) => write!(f, "unexpected reply from redis: {message}"),
Self::NoGroup { stream, group } => {
write!(f, "the `{group}` group doesn't exist on the `{stream}` stream")
}
Self::MissingField(field) => write!(f, "the envelope has no `{field}` field"),
Self::EmptyField(field) => write!(f, "the envelope's `{field}` field is empty"),
Self::DuplicateField(field) => write!(f, "the envelope names the `{field}` field twice"),
Self::InvalidUtf8 { field } => write!(f, "the envelope's `{field}` field isn't UTF-8"),
Self::UnsupportedVersion(version) => {
write!(f, "the envelope is version `{version}`; this crate reads version `1`")
}
Self::InvalidValue { field, expected } => {
write!(f, "the envelope's `{field}` field isn't {expected}")
}
Self::ReservedField(field) => {
write!(
f,
"the envelope sets `{field}` itself; it can't be given as an extra field"
)
}
Self::TooLarge { what, limit, actual } => {
write!(f, "the entry's {what} is {actual}, over the limit of {limit}")
}
Self::InvalidEntryId(id) => write!(f, "`{id}` isn't a stream entry id"),
Self::StreamFull {
stream,
depth,
target_depth,
} => write!(
f,
"the `{stream}` stream holds {depth} entries, at or over its target depth of {target_depth}; \
send again once it drains"
),
Self::InvalidConfig(message) => write!(f, "invalid configuration: {message}"),
Self::Thread(message) => write!(f, "couldn't start a worker thread: {message}"),
Self::Timeout { task_id, timeout } => {
write!(f, "no reply to task `{task_id}` within {} ms", timeout.as_millis())
}
}
}
}
impl std::error::Error for Error {}