Skip to main content

mdql_core/
errors.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum MdqlError {
5    #[error("Schema not found: {0}")]
6    SchemaNotFound(String),
7
8    #[error("Schema invalid: {0}")]
9    SchemaInvalid(String),
10
11    #[error("Parse error: {0}")]
12    Parse(String),
13
14    #[error("Query parse error: {0}")]
15    QueryParse(String),
16
17    #[error("Query execution error: {0}")]
18    QueryExecution(String),
19
20    #[error("Database config error: {0}")]
21    DatabaseConfig(String),
22
23    #[error("Journal recovery error: {0}")]
24    JournalRecovery(String),
25
26    #[error("{0}")]
27    General(String),
28
29    #[error("IO error: {0}")]
30    Io(#[from] std::io::Error),
31}
32
33pub type Result<T> = std::result::Result<T, MdqlError>;
34
35#[derive(Debug, Clone, PartialEq)]
36pub enum ValidationErrorKind {
37    ParseError,
38    MissingField,
39    TypeMismatch,
40    EnumViolation,
41    UnknownField,
42    MissingH1,
43    H1Mismatch,
44    DuplicateSection,
45    MissingSection,
46    UnknownSection,
47    LooseBody,
48    FkMissingTable,
49    FkViolation,
50    ViewError,
51}
52
53impl ValidationErrorKind {
54    pub fn as_str(&self) -> &'static str {
55        match self {
56            Self::ParseError => "parse_error",
57            Self::MissingField => "missing_field",
58            Self::TypeMismatch => "type_mismatch",
59            Self::EnumViolation => "enum_violation",
60            Self::UnknownField => "unknown_field",
61            Self::MissingH1 => "missing_h1",
62            Self::H1Mismatch => "h1_mismatch",
63            Self::DuplicateSection => "duplicate_section",
64            Self::MissingSection => "missing_section",
65            Self::UnknownSection => "unknown_section",
66            Self::LooseBody => "loose_body",
67            Self::FkMissingTable => "fk_missing_table",
68            Self::FkViolation => "fk_violation",
69            Self::ViewError => "view_error",
70        }
71    }
72}
73
74#[derive(Debug, Clone, PartialEq)]
75pub struct ValidationError {
76    pub file_path: String,
77    pub error_type: ValidationErrorKind,
78    pub field: Option<String>,
79    pub message: String,
80    pub line_number: Option<usize>,
81}
82
83impl std::fmt::Display for ValidationError {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        if let Some(line) = self.line_number {
86            write!(f, "{}:{}: {}", self.file_path, line, self.message)
87        } else {
88            write!(f, "{}: {}", self.file_path, self.message)
89        }
90    }
91}