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    FkMissingTable,
48    FkViolation,
49    ViewError,
50}
51
52impl ValidationErrorKind {
53    pub fn as_str(&self) -> &'static str {
54        match self {
55            Self::ParseError => "parse_error",
56            Self::MissingField => "missing_field",
57            Self::TypeMismatch => "type_mismatch",
58            Self::EnumViolation => "enum_violation",
59            Self::UnknownField => "unknown_field",
60            Self::MissingH1 => "missing_h1",
61            Self::H1Mismatch => "h1_mismatch",
62            Self::DuplicateSection => "duplicate_section",
63            Self::MissingSection => "missing_section",
64            Self::UnknownSection => "unknown_section",
65            Self::FkMissingTable => "fk_missing_table",
66            Self::FkViolation => "fk_violation",
67            Self::ViewError => "view_error",
68        }
69    }
70}
71
72#[derive(Debug, Clone, PartialEq)]
73pub struct ValidationError {
74    pub file_path: String,
75    pub error_type: ValidationErrorKind,
76    pub field: Option<String>,
77    pub message: String,
78    pub line_number: Option<usize>,
79}
80
81impl std::fmt::Display for ValidationError {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        if let Some(line) = self.line_number {
84            write!(f, "{}:{}: {}", self.file_path, line, self.message)
85        } else {
86            write!(f, "{}: {}", self.file_path, self.message)
87        }
88    }
89}