Skip to content

Commit

Permalink
Merge d3b6304 into e6a8283
Browse files Browse the repository at this point in the history
  • Loading branch information
morenol committed Aug 1, 2020
2 parents e6a8283 + d3b6304 commit 7e9a65d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum Filter {
Min,
String,
Sum,
Truncate,
Upper,
WordCount,
}
Expand All @@ -37,6 +38,7 @@ impl Filter {
"min" => Ok(Filter::Min),
"string" => Ok(Filter::String),
"sum" => Ok(Filter::Sum),
"truncate" => Ok(Filter::Truncate),
"upper" => Ok(Filter::Upper),
"wordcount" => Ok(Filter::WordCount),
_ => todo!(),
Expand All @@ -55,7 +57,6 @@ impl Filter {
Filter::Escape => base_value.escape(),
Filter::First => base_value.first(),
Filter::Int => Ok(Value::Integer(base_value.int()?)), // TODO change to accept parameters

Filter::Float => Ok(Value::Double(base_value.float()?)), // TODO change to accept parameters
Filter::Last => base_value.last(),
Filter::Length => Ok(Value::Integer(base_value.len()? as i64)),
Expand All @@ -64,6 +65,7 @@ impl Filter {
Filter::Min => base_value.min(), // TODO Accept params
Filter::String => Ok(Value::String(base_value.to_string())),
Filter::Sum => base_value.sum(), // TODO: ACcept params
Filter::Truncate => base_value.truncate(params, context),
Filter::Upper => base_value.upper(),
Filter::WordCount => base_value.wordcount(),
}
Expand Down
26 changes: 26 additions & 0 deletions src/value/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::context::Context;
use crate::error::{Error, ErrorKind, Result};
use crate::expression_evaluator::CallParams;
use crate::expression_evaluator::Evaluate;
use std::convert::TryInto;

use regex::Regex;

Expand Down Expand Up @@ -176,6 +177,31 @@ impl Value {
Err(Error::from(ErrorKind::InvalidOperation))
}
}
pub fn truncate(self, params: &Option<CallParams>, context: Context) -> Result<Self> {
let mut string_value = self.to_string();

let size = match params {
None => 150,
Some(call_params) => {
if let Some(value) = call_params.kw_params.get("length") {
value.evaluate(context)?.int()?.try_into().unwrap()
} else if let Some(value) = call_params.pos_params.first() {
value.evaluate(context)?.int()?.try_into().unwrap()
} else {
150
}
}
};
let value = if string_value.len() > size {
string_value.truncate(size - 3);
string_value.push_str("...");
string_value
} else {
string_value
};
Ok(Value::String(value))
}

pub fn upper(self) -> Result<Self> {
match self {
Value::String(s) => Ok(Value::String(s.to_uppercase())),
Expand Down
6 changes: 6 additions & 0 deletions tests/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,9 @@ fn default_filter() -> Result<()> {
)?;
assert_render_template_eq("{{ undefined | default }}", "", Some(context.clone()))
}
#[test]
fn truncate_filter() -> Result<()> {
assert_render_template_eq("{{ ('a' * 20) | truncate(10) }}", "aaaaaaa...", None)?;
assert_render_template_eq("{{ ('a' * 20) | truncate(length=10) }}", "aaaaaaa...", None)?;
assert_render_template_eq("{{ ('a' * 20) | truncate }}", "aaaaaaaaaaaaaaaaaaaa", None)
}

0 comments on commit 7e9a65d

Please sign in to comment.