aboutsummaryrefslogtreecommitdiff
path: root/src/template.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/template.rs')
-rw-r--r--src/template.rs27
1 files changed, 11 insertions, 16 deletions
diff --git a/src/template.rs b/src/template.rs
index bba0bbb..1a00fe2 100644
--- a/src/template.rs
+++ b/src/template.rs
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
-use std::io::{Error, ErrorKind::Other, Result};
+use std::io::{Error, Result};
use std::path::Path;
const TXT_TEMPLATE: &str = include_str!("../templates/index.txt");
@@ -118,7 +118,7 @@ impl Parsed {
Token::Text(contents) => rendered_template.push_str(contents),
Token::DisplayDirective { content } => {
let value = ContextGetter::get(context, content).ok_or_else(|| {
- Error::new(Other, format!("{content} is not a valid key"))
+ Error::other(format!("{content} is not a valid key"))
})?;
rendered_template.push_str(&value.render());
}
@@ -134,7 +134,7 @@ impl Parsed {
}
let value = ContextGetter::get(context, &condition).ok_or_else(|| {
- Error::new(Other, format!("{condition} is not a valid key"))
+ Error::other(format!("{condition} is not a valid key"))
})?;
match value {
@@ -145,8 +145,7 @@ impl Parsed {
}
Ok(())
}
- _ => Err(Error::new(
- Other,
+ _ => Err(Error::other(
format!("{condition} is not a boolean value"),
)),
}?;
@@ -157,7 +156,7 @@ impl Parsed {
children,
} => {
let value = ContextGetter::get(context, collection).ok_or_else(|| {
- Error::new(Other, format!("{collection} is not a valid key"))
+ Error::other(format!("{collection} is not a valid key"))
})?;
match value {
@@ -171,8 +170,7 @@ impl Parsed {
}
Ok(())
}
- _ => Err(Error::new(
- Other,
+ _ => Err(Error::other(
format!("{collection} is not a collection"),
)),
}?;
@@ -195,7 +193,7 @@ fn tokenize(template: &str, tokens: &mut Vec<Token>) -> Result<()> {
while !remaining_template.is_empty() && remaining_template.contains("{{") {
let directive_start_index = remaining_template
.find("{{")
- .ok_or_else(|| Error::new(Other, "Was expecting at least one tag opener"))?;
+ .ok_or_else(|| Error::other("Was expecting at least one tag opener"))?;
if directive_start_index > 0 {
let text = remaining_template[..directive_start_index].to_string();
tokens.push(Token::Text(text.to_string()));
@@ -204,7 +202,7 @@ fn tokenize(template: &str, tokens: &mut Vec<Token>) -> Result<()> {
let directive_end_index = remaining_template
.find("}}")
- .ok_or_else(|| Error::new(Other, "Was expecting }} after {{"))?
+ .ok_or_else(|| Error::other("Was expecting }} after {{"))?
+ 2;
let directive = &remaining_template[..directive_end_index];
remaining_template = &remaining_template[directive_end_index..];
@@ -225,8 +223,7 @@ fn tokenize(template: &str, tokens: &mut Vec<Token>) -> Result<()> {
if directive_type == '?' {
let closing_block = remaining_template.find("{{?}}").ok_or_else(|| {
- Error::new(
- Other,
+ Error::other(
"Could not find closing tag, expecting {{?}}, found end-of-file",
)
})?;
@@ -240,8 +237,7 @@ fn tokenize(template: &str, tokens: &mut Vec<Token>) -> Result<()> {
} else if directive_type == '~' {
let parts: Vec<_> = content.splitn(2, ':').collect();
let closing_block = remaining_template.find("{{~}}").ok_or_else(|| {
- Error::new(
- Other,
+ Error::other(
"Could not find closing tag, expecting {{?}}, found end-of-file",
)
})?;
@@ -264,8 +260,7 @@ fn tokenize(template: &str, tokens: &mut Vec<Token>) -> Result<()> {
}
}
_ => {
- return Err(Error::new(
- Other,
+ return Err(Error::other(
"{{ }} is not a valid directive. Need one of: =, ? or ~.",
));
}