Skip to content

Commit 43b5c78

Browse files
rename tag spec intermediates to branches
1 parent 2fe1bb8 commit 43b5c78

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

crates/djls-template-ast/src/parser.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,26 @@ impl Parser {
155155

156156
let specs = TagSpec::load_builtin_specs().unwrap_or_default();
157157

158-
// Check if this is a closing or branch tag
158+
// Check if this is a closing tag
159159
for (_, spec) in specs.iter() {
160-
if Some(&tag_name) == spec.closing.as_ref()
161-
|| spec.intermediates.as_ref()
162-
.map(|ints| ints.iter().any(|i| i.name == tag_name))
163-
.unwrap_or(false)
164-
{
160+
if Some(&tag_name) == spec.closing.as_ref() {
161+
// let node = Node::Django(DjangoNode::Tag(TagNode::Closing {
162+
// name: tag_name.clone(),
163+
// bits: bits[1..].to_vec(),
164+
// }));
165165
return Err(ParserError::ErrorSignal(Signal::SpecialTag(tag_name)));
166166
}
167167
}
168168

169+
// Check if this is a branch tag according to any spec
170+
for (_, spec) in specs.iter() {
171+
if let Some(branches) = &spec.branches {
172+
if branches.iter().any(|i| i.name == tag_name) {
173+
return Err(ParserError::ErrorSignal(Signal::SpecialTag(tag_name)));
174+
}
175+
}
176+
}
177+
169178
let tag_spec = specs.get(tag_name.as_str()).cloned();
170179
let mut children = Vec::new();
171180
let mut current_branch: Option<(String, Vec<String>, Vec<Node>)> = None;
@@ -202,9 +211,8 @@ impl Parser {
202211
})));
203212
}
204213
// Check if intermediate tag
205-
if let Some(intermediates) = &spec.intermediates {
206-
if let Some(intermediate) = intermediates.iter().find(|i| i.name == tag)
207-
{
214+
if let Some(branches) = &spec.branches {
215+
if let Some(branch) = branches.iter().find(|i| i.name == tag) {
208216
// If we have a current branch, add it to children
209217
if let Some((name, bits, branch_children)) = current_branch {
210218
children.push(Node::Django(DjangoNode::Tag(TagNode::Branch {
@@ -214,7 +222,7 @@ impl Parser {
214222
})));
215223
}
216224
// Create new branch node
217-
let branch_bits = if intermediate.args {
225+
let branch_bits = if branch.args {
218226
match &self.tokens[self.current - 1].token_type() {
219227
TokenType::DjangoBlock(content) => content
220228
.split_whitespace()
@@ -675,8 +683,7 @@ mod tests {
675683

676684
#[test]
677685
fn test_parse_complex_if_elif() {
678-
let source =
679-
"{% if x > 0 %}Positive{% elif x < 0 %}Negative{% else %}Zero{% endif %}";
686+
let source = "{% if x > 0 %}Positive{% elif x < 0 %}Negative{% else %}Zero{% endif %}";
680687
let tokens = Lexer::new(source).tokenize().unwrap();
681688
let mut parser = Parser::new(tokens);
682689
let ast = parser.parse().unwrap();

crates/djls-template-ast/src/tagspecs.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ pub struct TagSpec {
1010
#[serde(rename = "type")]
1111
pub tag_type: TagType,
1212
pub closing: Option<String>,
13-
pub intermediates: Option<Vec<IntermediateSpec>>,
13+
#[serde(rename = "intermediates")]
14+
pub branches: Option<Vec<BranchSpec>>,
1415
pub args: Option<Vec<ArgSpec>>,
1516
}
1617

1718
#[derive(Debug, Clone, Deserialize)]
18-
pub struct IntermediateSpec {
19+
pub struct BranchSpec {
1920
pub name: String,
2021
pub args: bool,
2122
}

crates/djls-template-ast/tagspecs/django.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
type = "block"
33
closing = "endif"
44

5+
# We keep the intermediates field name in TOML since we're using serde rename
6+
# to map it to branches in the Rust code
57
[[django.template.defaulttags.if.intermediates]]
68
name = "elif"
79
args = true
@@ -18,6 +20,8 @@ required = true
1820
type = "block"
1921
closing = "endfor"
2022

23+
# We keep the intermediates field name in TOML since we're using serde rename
24+
# to map it to branches in the Rust code
2125
[[django.template.defaulttags.for.intermediates]]
2226
name = "empty"
2327
args = false

0 commit comments

Comments
 (0)