aboutsummaryrefslogtreecommitdiff
path: root/src/template.rs
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-04-06 00:57:56 +0200
committerRuben Beltran del Rio <git@r.bdr.sh>2025-04-06 00:57:56 +0200
commitd0f582b98712d967b2f95d0405886d063bd89468 (patch)
treecc644c21278d336772557366bcdd3e46b22065db /src/template.rs
parent8b3b94a38b443c50afc5b42cca45db7c18ce280d (diff)
Get stricter clippy
Diffstat (limited to 'src/template.rs')
-rw-r--r--src/template.rs78
1 files changed, 41 insertions, 37 deletions
diff --git a/src/template.rs b/src/template.rs
index 2e4c8a6..8e61978 100644
--- a/src/template.rs
+++ b/src/template.rs
@@ -89,9 +89,11 @@ impl ContextGetter {
}
fn recursively_get_value(context: &Context, path: &[&str]) -> Option<Value> {
- match context.get(path[0]) {
+ let next_path = path.first()?.to_owned();
+ match context.get(next_path) {
Some(Value::Context(next)) if path.len() > 1 => {
- ContextGetter::recursively_get_value(next, &path[1..])
+ let remainder = &path.get(1..)?.to_owned();
+ ContextGetter::recursively_get_value(next, remainder)
}
Some(value) if path.len() == 1 => Some(value.clone()),
_ => None,
@@ -207,7 +209,7 @@ fn tokenize(template: &str, tokens: &mut Vec<Token>) -> Result<()> {
let directive = &remaining_template[..directive_end_index];
remaining_template = &remaining_template[directive_end_index..];
- let directive_type = directive.chars().nth(2).unwrap();
+ let directive_type = directive.chars().nth(2).unwrap_or(' ');
match directive_type {
// Simple Directives
'=' => {
@@ -221,42 +223,44 @@ fn tokenize(template: &str, tokens: &mut Vec<Token>) -> Result<()> {
let content = directive[3..directive.len() - 2].trim();
let mut children = Vec::new();
- match directive_type {
- '?' => {
- let closing_block = remaining_template.find("{{?}}").ok_or_else(|| {
- Error::new(
- Other,
- "Could not find closing tag, expecting {{?}}, found end-of-file",
- )
- })?;
- let directive_block = &remaining_template[..closing_block];
- remaining_template = &remaining_template[closing_block + 5..];
- tokenize(directive_block, &mut children)?;
- tokens.push(Token::ConditionalDirective {
- condition: content.to_string(),
- children,
- });
- }
- '~' => {
- let parts: Vec<_> = content.splitn(2, ':').collect();
- let closing_block = remaining_template.find("{{~}}").ok_or_else(|| {
- Error::new(
- Other,
- "Could not find closing tag, expecting {{?}}, found end-of-file",
- )
- })?;
- let directive_block = &remaining_template[..closing_block];
- remaining_template = &remaining_template[closing_block + 5..];
- tokenize(directive_block, &mut children)?;
- if parts.len() == 2 {
- tokens.push(Token::IteratorDirective {
- collection: parts[0].trim().to_string(),
- member_label: parts[1].trim().to_string(),
- children,
- });
+ if directive_type == '?' {
+ let closing_block = remaining_template.find("{{?}}").ok_or_else(|| {
+ Error::new(
+ Other,
+ "Could not find closing tag, expecting {{?}}, found end-of-file",
+ )
+ })?;
+ let directive_block = &remaining_template[..closing_block];
+ remaining_template = &remaining_template[closing_block + 5..];
+ tokenize(directive_block, &mut children)?;
+ tokens.push(Token::ConditionalDirective {
+ condition: content.to_string(),
+ children,
+ });
+ } else if directive_type == '~' {
+ let parts: Vec<_> = content.splitn(2, ':').collect();
+ let closing_block = remaining_template.find("{{~}}").ok_or_else(|| {
+ Error::new(
+ Other,
+ "Could not find closing tag, expecting {{?}}, found end-of-file",
+ )
+ })?;
+ let directive_block = &remaining_template[..closing_block];
+ remaining_template = &remaining_template[closing_block + 5..];
+ tokenize(directive_block, &mut children)?;
+ if parts.len() == 2 {
+ if let Some(first_part) = parts.first() {
+ if let Some(second_part) = parts.get(1) {
+ let collection = first_part.trim().to_string();
+ let member_label = second_part.trim().to_string();
+ tokens.push(Token::IteratorDirective {
+ collection,
+ member_label,
+ children,
+ });
+ }
}
}
- _ => unreachable!(),
}
}
_ => {