use thiserror::Error;
#[derive(Error, Debug, Clone, PartialEq)]
pub enum BlocksError {
#[error("Invalid block type: {0}")]
InvalidBlockType(String),
#[error("Invalid header level: {level}. Must be between 1 and 6")]
InvalidHeaderLevel { level: u8 },
#[error("Empty content not allowed for block type: {block_type}")]
EmptyContent { block_type: String },
#[error("Conversion error: {message}")]
ConversionError { message: String },
#[error("Serialization error: {0}")]
SerializationError(String),
#[error("Invalid URL format: {url}")]
InvalidUrl { url: String },
#[error("Invalid image format: {reason}")]
InvalidImage { reason: String },
#[error("Document validation failed: {message}")]
ValidationError { message: String },
#[error("Invalid table structure: {reason}")]
InvalidTable { reason: String },
#[error("Invalid columns configuration: {reason}")]
InvalidColumns { reason: String },
#[error("Invalid embed configuration: {reason}")]
InvalidEmbed { reason: String },
#[error("CSS parsing error: {reason}")]
CssError { reason: String },
}
pub type Result<T> = std::result::Result<T, BlocksError>;
impl From<serde_json::Error> for BlocksError {
fn from(err: serde_json::Error) -> Self {
BlocksError::SerializationError(err.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = BlocksError::InvalidHeaderLevel { level: 10 };
assert_eq!(
err.to_string(),
"Invalid header level: 10. Must be between 1 and 6"
);
}
#[test]
fn test_error_clone() {
let err = BlocksError::EmptyContent {
block_type: "Text".to_string(),
};
let cloned = err.clone();
assert_eq!(err, cloned);
}
#[test]
fn test_from_serde_error() {
let json_err = serde_json::from_str::<i32>("invalid json").unwrap_err();
let blocks_err = BlocksError::from(json_err);
match blocks_err {
BlocksError::SerializationError(msg) => assert!(msg.contains("expected")),
_ => panic!("Expected SerializationError"),
}
}
}