dampen_core/parser/
error.rs1use crate::ir::span::Span;
2use proc_macro2::TokenStream;
3use quote::quote;
4
5#[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 UnsupportedVersion,
25 DeprecatedAttribute,
27 InvalidChild,
29 InvalidDateFormat,
31 InvalidTimeFormat,
33 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 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}