use crate::serde::SerdeError;
#[derive(thiserror::Error, Debug)]
pub enum ProjectionError {
#[error("Serialization error: {0}")]
Serialization(#[from] SerdeError),
#[error("Database error: {0}")]
Database(String),
#[error("Invalid data: {0}")]
InvalidData(String),
#[error("Stream processing error: {0}")]
StreamProcessing(String),
#[error("Json error: {0}")]
Json(#[from] serde_json::Error),
}
pub type Result<T> = std::result::Result<T, ProjectionError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let db_error = ProjectionError::Database("Connection failed".to_string());
assert_eq!(db_error.to_string(), "Database error: Connection failed");
let invalid_data_error = ProjectionError::InvalidData("Bad format".to_string());
assert_eq!(invalid_data_error.to_string(), "Invalid data: Bad format");
let stream_error = ProjectionError::StreamProcessing("Stream closed".to_string());
assert_eq!(stream_error.to_string(), "Stream processing error: Stream closed");
}
#[test]
fn test_serde_error_conversion() {
let serde_error = SerdeError::ConversionError("Type mismatch".to_string());
let projection_error: ProjectionError = serde_error.into();
assert!(matches!(projection_error, ProjectionError::Serialization(_)));
assert_eq!(
projection_error.to_string(),
"Serialization error: failed to convert type values: Type mismatch"
);
}
#[test]
fn test_json_error_conversion() {
let json_str = "{ invalid json }";
let json_error = serde_json::from_str::<serde_json::Value>(json_str).unwrap_err();
let projection_error: ProjectionError = json_error.into();
assert!(matches!(projection_error, ProjectionError::Json(_)));
assert!(projection_error.to_string().starts_with("Json error:"));
}
#[test]
fn test_result_type() {
fn returns_result() -> Result<String> {
Ok("success".to_string())
}
let result = returns_result();
assert!(result.is_ok());
assert_eq!(result.unwrap(), "success");
fn returns_error() -> Result<String> {
Err(ProjectionError::Database("Failed".to_string()))
}
let error = returns_error();
assert!(error.is_err());
}
}