use thiserror::Error;
#[derive(Debug, Error)]
pub enum IjimaError {
#[error("duplicate memory rejected: {detail}")]
Duplicate {
detail: String,
},
#[error("not found: {detail}")]
NotFound {
detail: String,
},
#[error("store error: {detail}")]
Store {
detail: String,
},
#[error("invalid input: {detail}")]
InvalidInput {
detail: String,
},
#[error("schema error: {detail}")]
Schema {
detail: String,
},
#[error("mining error: {detail}")]
Mining {
detail: String,
},
#[error("transport error: {detail}")]
Transport {
detail: String,
},
}
impl IjimaError {
pub fn duplicate(detail: impl Into<String>) -> Self {
Self::Duplicate {
detail: detail.into(),
}
}
pub fn not_found(detail: impl Into<String>) -> Self {
Self::NotFound {
detail: detail.into(),
}
}
pub fn invalid_input(detail: impl Into<String>) -> Self {
Self::InvalidInput {
detail: detail.into(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn duplicate_error_carries_detail() {
let err = IjimaError::duplicate("content hash abc123 already present");
match err {
IjimaError::Duplicate { detail } => {
assert_eq!(detail, "content hash abc123 already present");
}
other => panic!("expected Duplicate, got {other:?}"),
}
}
#[test]
fn error_display_is_human_readable() {
let err = IjimaError::not_found("session sess_42");
assert_eq!(err.to_string(), "not found: session sess_42");
}
}