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}
28
29impl std::fmt::Display for ParseError {
30 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31 write!(
32 f,
33 "error[{}]: {} at line {}, column {}",
34 self.kind as u8, self.message, self.span.line, self.span.column
35 )?;
36 if let Some(suggestion) = &self.suggestion {
37 write!(f, "\n help: {}", suggestion)?;
38 }
39 Ok(())
40 }
41}
42
43impl ParseError {
44 pub fn to_compile_error(&self) -> TokenStream {
53 let message = format!(
54 "Dampen parsing error: {}\n at line {}, column {}",
55 self.message, self.span.line, self.span.column
56 );
57
58 let mut tokens = quote! {
59 compile_error!(#message);
60 };
61
62 if let Some(ref suggestion) = self.suggestion {
63 let help = format!("help: {}", suggestion);
64 tokens.extend(quote! {
65 compile_error!(#help);
66 });
67 }
68
69 tokens
70 }
71}