simplify nodes

This commit is contained in:
Josh Thomas 2025-01-04 23:14:51 -06:00
parent def8f6ca28
commit de296ceff4
2 changed files with 17 additions and 20 deletions

View file

@ -1,5 +1,4 @@
use serde::Serialize; use serde::Serialize;
use std::collections::BTreeMap;
use thiserror::Error; use thiserror::Error;
#[derive(Clone, Debug, Default, Serialize)] #[derive(Clone, Debug, Default, Serialize)]
@ -35,12 +34,7 @@ impl Ast {
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
pub enum Node { pub enum Node {
Django(DjangoNode),
Text(String), Text(String),
}
#[derive(Clone, Debug, Serialize)]
pub enum DjangoNode {
Comment(String), Comment(String),
Tag(TagNode), Tag(TagNode),
Variable { Variable {

View file

@ -102,7 +102,7 @@ impl Parser {
end: Option<&str>, end: Option<&str>,
) -> Result<Node, ParserError> { ) -> Result<Node, ParserError> {
match start { match start {
"{#" => Ok(Node::Django(DjangoNode::Comment(content.to_string()))), "{#" => Ok(Node::Comment(content.to_string())),
_ => Ok(Node::Text(format!( _ => Ok(Node::Text(format!(
"{}{}{}", "{}{}{}",
start, start,
@ -148,19 +148,19 @@ impl Parser {
Err(ParserError::ErrorSignal(Signal::SpecialTag(tag))) => { Err(ParserError::ErrorSignal(Signal::SpecialTag(tag))) => {
if let Some(spec) = &tag_spec { if let Some(spec) = &tag_spec {
// Check if closing tag // Check if closing tag
if spec.closing.as_ref().map(|s| s.as_str()) == Some(&tag) { if spec.closing.as_deref() == Some(&tag) {
// If we have a current branch, add it to children // If we have a current branch, add it to children
if let Some((name, bits, branch_children)) = current_branch { if let Some((name, bits, branch_children)) = current_branch {
children.push(Node::Django(DjangoNode::Tag(TagNode::Branch { children.push(Node::Tag(TagNode::Branch {
name, name,
bits, bits,
children: branch_children, children: branch_children,
}))); }));
} }
children.push(Node::Django(DjangoNode::Tag(TagNode::Closing { children.push(Node::Tag(TagNode::Closing {
name: tag, name: tag,
bits: vec![], bits: vec![],
}))); }));
found_closing_tag = true; found_closing_tag = true;
break; break;
} }
@ -169,11 +169,11 @@ impl Parser {
if let Some(branch) = branches.iter().find(|i| i.name == tag) { if let Some(branch) = branches.iter().find(|i| i.name == tag) {
// If we have a current branch, add it to children // If we have a current branch, add it to children
if let Some((name, bits, branch_children)) = current_branch { if let Some((name, bits, branch_children)) = current_branch {
children.push(Node::Django(DjangoNode::Tag(TagNode::Branch { children.push(Node::Tag(TagNode::Branch {
name, name,
bits, bits,
children: branch_children, children: branch_children,
}))); }));
} }
// Create new branch node // Create new branch node
let branch_bits = if branch.args { let branch_bits = if branch.args {
@ -194,11 +194,11 @@ impl Parser {
} }
} }
// If we get here, it's an unexpected tag // If we get here, it's an unexpected tag
let node = Node::Django(DjangoNode::Tag(TagNode::Block { let node = Node::Tag(TagNode::Block {
name: tag_name.clone(), name: tag_name.clone(),
bits: bits.clone(), bits: bits.clone(),
children: children.clone(), children: children.clone(),
})); });
return Err(ParserError::Ast(AstError::UnexpectedTag(tag), Some(node))); return Err(ParserError::Ast(AstError::UnexpectedTag(tag), Some(node)));
} }
Err(ParserError::Ast(AstError::StreamError(kind), _)) if kind == "AtEnd" => { Err(ParserError::Ast(AstError::StreamError(kind), _)) if kind == "AtEnd" => {
@ -208,11 +208,11 @@ impl Parser {
} }
} }
let node = Node::Django(DjangoNode::Tag(TagNode::Block { let node = Node::Tag(TagNode::Block {
name: tag_name.clone(), name: tag_name.clone(),
bits, bits,
children, children,
})); });
if !found_closing_tag { if !found_closing_tag {
return Err(ParserError::Ast( return Err(ParserError::Ast(
@ -249,7 +249,7 @@ impl Parser {
}) })
.collect(); .collect();
Ok(Node::Django(DjangoNode::Variable { bits, filters })) Ok(Node::Variable { bits, filters })
} }
fn parse_text(&mut self) -> Result<Node, ParserError> { fn parse_text(&mut self) -> Result<Node, ParserError> {
@ -345,7 +345,10 @@ impl Parser {
} }
self.consume()?; self.consume()?;
} }
Err(ParserError::Ast(AstError::StreamError("AtEnd".into()), None)) Err(ParserError::Ast(
AstError::StreamError("AtEnd".into()),
None,
))
} }
} }