use std::error::Error;
use std::fmt::{Display, Formatter};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CoreError {
    BlockNotFound(String),
    DuplicateBlockId(String),
    InvalidOperation(&'static str),
    InvalidSplitOffset { block_id: String, offset: usize },
    MarkdownParse(String),
}

impl Display for CoreError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::BlockNotFound(block_id) => write!(f, "未找到块: {block_id}"),
            Self::DuplicateBlockId(block_id) => write!(f, "块 id 已存在: {block_id}"),
            Self::InvalidOperation(message) => write!(f, "无效操作: {message}"),
            Self::InvalidSplitOffset { block_id, offset } => {
                write!(f, "块 {block_id} 的拆分位置无效: {offset}")
            }
            Self::MarkdownParse(message) => write!(f, "Markdown 解析失败: {message}"),
        }
    }
}

impl Error for CoreError {}

pub type CoreResult<T> = Result<T, CoreError>;
