From a4b6742c6a67055109ce8d32c1a57f98f1f10f22 Mon Sep 17 00:00:00 2001 From: Josh Date: Sat, 4 Jan 2025 16:23:36 -0600 Subject: [PATCH] add errors --- crates/djls-template-ast/src/ast.rs | 33 ++++++++++++++++--- ...rser__tests__comments__parse_comments.snap | 1 + ..._tests__django__parse_complex_if_elif.snap | 1 + ...tests__django__parse_django_for_block.snap | 1 + ..._tests__django__parse_django_if_block.snap | 1 + ..._tests__django__parse_django_variable.snap | 1 + ...r__tests__django__parse_filter_chains.snap | 1 + ...r__tests__django__parse_mixed_content.snap | 1 + ...r__tests__django__parse_nested_for_if.snap | 1 + ...er__tests__full_templates__parse_full.snap | 1 + ...rser__tests__html__parse_html_doctype.snap | 1 + ...__parser__tests__html__parse_html_tag.snap | 1 + ..._parser__tests__html__parse_html_void.snap | 1 + ...__parser__tests__script__parse_script.snap | 1 + ...st__parser__tests__style__parse_style.snap | 1 + 15 files changed, 42 insertions(+), 5 deletions(-) diff --git a/crates/djls-template-ast/src/ast.rs b/crates/djls-template-ast/src/ast.rs index ea85e55..56262ed 100644 --- a/crates/djls-template-ast/src/ast.rs +++ b/crates/djls-template-ast/src/ast.rs @@ -5,6 +5,7 @@ use thiserror::Error; #[derive(Clone, Debug, Default, Serialize)] pub struct Ast { nodes: Vec, + errors: Vec, } impl Ast { @@ -12,12 +13,20 @@ impl Ast { &self.nodes } + pub fn errors(&self) -> &Vec { + &self.errors + } + pub fn add_node(&mut self, node: Node) { self.nodes.push(node); } + pub fn add_error(&mut self, error: AstError) { + self.errors.push(error); + } + pub fn finalize(&mut self) -> Result { - if self.nodes.is_empty() { + if self.nodes.is_empty() && self.errors.is_empty() { return Err(AstError::EmptyAst); } Ok(self.clone()) @@ -123,10 +132,24 @@ pub enum AttributeValue { pub type Attributes = BTreeMap; -#[derive(Error, Debug)] +#[derive(Clone, Debug, Error, Serialize)] pub enum AstError { - #[error("error parsing django tag, recieved empty tag name")] - EmptyTag, - #[error("empty ast")] + #[error("Empty AST")] EmptyAst, + #[error("Stream error: {0}")] + StreamError(String), + #[error("Unclosed tag: {0}")] + UnclosedTag(String), + #[error("Unexpected tag: {0}")] + UnexpectedTag(String), + #[error("Invalid tag: {0}")] + InvalidTag(String), + #[error("Block error: {0} in {1}")] + BlockError(String, String), + #[error("Argument error: {0} - {1}")] + ArgumentError(String, String), + #[error("Unexpected token")] + UnexpectedToken, + #[error("Unexpected end of file")] + UnexpectedEof, } diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__comments__parse_comments.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__comments__parse_comments.snap index 0bd0dc2..e1ad42e 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__comments__parse_comments.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__comments__parse_comments.snap @@ -7,3 +7,4 @@ nodes: Comment: HTML comment - Django: Comment: Django comment +errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_complex_if_elif.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_complex_if_elif.snap index 2cad824..6f60c46 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_complex_if_elif.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_complex_if_elif.snap @@ -36,3 +36,4 @@ nodes: Closing: name: endif bits: [] +errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_for_block.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_for_block.snap index 192fef3..37ebb12 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_for_block.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_for_block.snap @@ -30,3 +30,4 @@ nodes: Closing: name: endfor bits: [] +errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_if_block.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_if_block.snap index ff753ba..c78efe8 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_if_block.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_if_block.snap @@ -17,3 +17,4 @@ nodes: Closing: name: endif bits: [] +errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_variable.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_variable.snap index 57dab6e..51372e2 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_variable.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_variable.snap @@ -11,3 +11,4 @@ nodes: filters: - name: title arguments: [] +errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_filter_chains.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_filter_chains.snap index 4593b1f..c477b90 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_filter_chains.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_filter_chains.snap @@ -15,3 +15,4 @@ nodes: arguments: [] - name: upper arguments: [] +errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap index a1233df..aa96c18 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap @@ -107,3 +107,4 @@ nodes: name: endif bits: [] - Text: "!" +errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_nested_for_if.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_nested_for_if.snap index 2b6ba47..0620bc6 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_nested_for_if.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_nested_for_if.snap @@ -37,3 +37,4 @@ nodes: Closing: name: endfor bits: [] +errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap index ac140fa..e3073c9 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap @@ -128,3 +128,4 @@ nodes: Closing: name: endif bits: [] +errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_doctype.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_doctype.snap index 0b797df..0775911 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_doctype.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_doctype.snap @@ -5,3 +5,4 @@ expression: ast nodes: - Html: Doctype: "!DOCTYPE" +errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_tag.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_tag.snap index 72786c7..272ced9 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_tag.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_tag.snap @@ -11,3 +11,4 @@ nodes: Value: container children: - Text: Hello +errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_void.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_void.snap index bebb96e..52faa53 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_void.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_void.snap @@ -9,3 +9,4 @@ nodes: attributes: type: Value: text +errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__script__parse_script.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__script__parse_script.snap index 04c32c4..51115b6 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__script__parse_script.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__script__parse_script.snap @@ -19,3 +19,4 @@ nodes: content: "Multi-line\n comment" kind: MultiLine - Text: console.log(x); +errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__style__parse_style.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__style__parse_style.snap index a114191..d31e4f9 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__style__parse_style.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__style__parse_style.snap @@ -16,3 +16,4 @@ nodes: - Text: "{" - Text: "color: blue;" - Text: "}" +errors: []