Skip to main content

dampen_core/parser/
error.rs

1use crate::ir::span::Span;
2use proc_macro2::TokenStream;
3use quote::quote;
4
5/// Error during parsing
6#[derive(Debug, Clone, PartialEq)]
7pub struct ParseError {
8    pub kind: ParseErrorKind,
9    pub message: String,
10    pub span: Span,
11    pub suggestion: Option<String>,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub enum ParseErrorKind {
16    XmlSyntax,
17    UnknownWidget,
18    UnknownAttribute,
19    InvalidValue,
20    InvalidExpression,
21    UnclosedBinding,
22    MissingAttribute,
23    /// Schema version is newer than the framework supports
24    UnsupportedVersion,
25    /// Attribute is deprecated in favor of a standardized alternative
26    DeprecatedAttribute,
27    /// Child widget is not allowed in this context
28    InvalidChild,
29    /// Invalid date format for static value
30    InvalidDateFormat,
31    /// Invalid time format for static value
32    InvalidTimeFormat,
33    /// Invalid date range (min > max)
34    InvalidDateRange,
35}
36
37impl std::fmt::Display for ParseError {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        write!(
40            f,
41            "error[{}]: {} at line {}, column {}",
42            self.kind as u8, self.message, self.span.line, self.span.column
43        )?;
44        if let Some(suggestion) = &self.suggestion {
45            write!(f, "\n  help: {}", suggestion)?;
46        }
47        Ok(())
48    }
49}
50
51impl ParseError {
52    /// Convert this error into a compile_error! macro invocation.
53    ///
54    /// This is used by procedural macros to emit compile-time errors
55    /// with proper location information and helpful suggestions.
56    ///
57    /// # Returns
58    ///
59    /// A `TokenStream` containing a `compile_error!` macro invocation.
60    pub fn to_compile_error(&self) -> TokenStream {
61        let message = format!(
62            "Dampen parsing error: {}\n  at line {}, column {}",
63            self.message, self.span.line, self.span.column
64        );
65
66        let mut tokens = quote! {
67            compile_error!(#message);
68        };
69
70        if let Some(ref suggestion) = self.suggestion {
71            let help = format!("help: {}", suggestion);
72            tokens.extend(quote! {
73                compile_error!(#help);
74            });
75        }
76
77        tokens
78    }
79}