use std::fmt;
#[derive(Debug)]
pub enum PrimeError {
NodeNotFound(String),
EdgeNotFound(String),
DuplicateNode(String),
InvalidTraversal(String),
ProjectionError(String),
CoreError(anyhow::Error),
ValidationFailed(String),
}
impl fmt::Display for PrimeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NodeNotFound(id) => write!(f, "node not found: {id}"),
Self::EdgeNotFound(id) => write!(f, "edge not found: {id}"),
Self::DuplicateNode(id) => write!(f, "duplicate node: {id}"),
Self::InvalidTraversal(msg) => write!(f, "invalid traversal: {msg}"),
Self::ProjectionError(msg) => write!(f, "projection error: {msg}"),
Self::CoreError(e) => write!(f, "core error: {e}"),
Self::ValidationFailed(msg) => write!(f, "validation failed: {msg}"),
}
}
}
impl From<super::schema::ValidationError> for PrimeError {
fn from(e: super::schema::ValidationError) -> Self {
Self::ValidationFailed(e.to_string())
}
}
impl std::error::Error for PrimeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::CoreError(e) => Some(e.as_ref()),
_ => None,
}
}
}
impl From<anyhow::Error> for PrimeError {
fn from(e: anyhow::Error) -> Self {
Self::CoreError(e)
}
}
impl From<crate::error::AllSourceError> for PrimeError {
fn from(e: crate::error::AllSourceError) -> Self {
Self::CoreError(e.into())
}
}
pub type PrimeResult<T> = std::result::Result<T, PrimeError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let e = PrimeError::NodeNotFound("n-123".to_string());
assert_eq!(e.to_string(), "node not found: n-123");
let e = PrimeError::EdgeNotFound("e-456".to_string());
assert_eq!(e.to_string(), "edge not found: e-456");
let e = PrimeError::DuplicateNode("n-789".to_string());
assert_eq!(e.to_string(), "duplicate node: n-789");
let e = PrimeError::InvalidTraversal("cycle detected".to_string());
assert_eq!(e.to_string(), "invalid traversal: cycle detected");
let e = PrimeError::ProjectionError("failed to snapshot".to_string());
assert_eq!(e.to_string(), "projection error: failed to snapshot");
}
#[test]
fn test_from_anyhow() {
let anyhow_err = anyhow::anyhow!("something went wrong");
let prime_err = PrimeError::from(anyhow_err);
assert!(matches!(prime_err, PrimeError::CoreError(_)));
assert!(prime_err.to_string().contains("something went wrong"));
}
}