use thiserror::Error;
#[derive(Debug, Error)]
pub enum StoreError {
#[error("not found: {id}")]
NotFound { id: String },
#[error("conflict on {id}: {reason}")]
Conflict { id: String, reason: String },
#[error("io error: {0}")]
Io(String),
#[error("invalid query: {0}")]
InvalidQuery(String),
}
impl StoreError {
pub fn not_found(id: impl std::fmt::Display) -> Self {
Self::NotFound { id: id.to_string() }
}
pub fn conflict(id: impl std::fmt::Display, reason: impl Into<String>) -> Self {
Self::Conflict { id: id.to_string(), reason: reason.into() }
}
pub fn io(msg: impl Into<String>) -> Self {
Self::Io(msg.into())
}
pub fn invalid_query(msg: impl Into<String>) -> Self {
Self::InvalidQuery(msg.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn not_found_display() {
let e = StoreError::not_found("atom-42");
assert_eq!(e.to_string(), "not found: atom-42");
}
#[test]
fn conflict_display() {
let e = StoreError::conflict("block-1", "duplicate key");
assert_eq!(e.to_string(), "conflict on block-1: duplicate key");
}
}