corpora-core 0.1.0

Core domain types, immutable graph, and event bus for the corpora docs validator.
Documentation
//! Diagnostics are data, not panics — the corpus stays inspectable on any failure.

use crate::model::DocPath;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Severity {
    Error,
    Warning,
}

#[derive(Clone, Debug)]
pub struct Diagnostic {
    pub severity: Severity,
    pub code: String,
    pub path: DocPath,
    pub message: String,
}

impl Diagnostic {
    pub fn error(code: &str, path: &DocPath, message: impl Into<String>) -> Self {
        Diagnostic {
            severity: Severity::Error,
            code: code.to_string(),
            path: path.clone(),
            message: message.into(),
        }
    }

    pub fn warn(code: &str, path: &DocPath, message: impl Into<String>) -> Self {
        Diagnostic {
            severity: Severity::Warning,
            code: code.to_string(),
            path: path.clone(),
            message: message.into(),
        }
    }
}

impl std::fmt::Display for Diagnostic {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let sev = match self.severity {
            Severity::Error => "error",
            Severity::Warning => "warning",
        };
        write!(f, "{sev}[{}] {}: {}", self.code, self.path.0, self.message)
    }
}