Skip to content

Commit

Permalink
Implement 'lower' function in library
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler committed Dec 5, 2024
1 parent 2518381 commit 3b4ed98
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
1 change: 0 additions & 1 deletion esi/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use thiserror::Error;

use fastly::http::request::SendError;
use regex::Error;

/// Describes an error encountered during ESI parsing or execution.
#[derive(Error, Debug)]
Expand Down
27 changes: 15 additions & 12 deletions esi/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ fn eval_expr(expr: Expr, ctx: &EvalContext) -> Result<Value> {
fn call_dispatch(identifier: String, args: Vec<Value>) -> Result<Value> {
let result = match identifier.as_str() {
"ping" => Value::String("pong".to_string()),
"lower" => {
if args.len() != 1 {
Value::Error("wrong number of arguments to 'lower'".to_string())
} else {
match &args[0] {
Value::String(s) => Value::String(s.to_lowercase()),
_ => Value::Error("incorrect type passed to 'lower'".to_string()),
}
}
}
_ => Value::Error(format!("unknown function: {}", identifier)),
};
Ok(result)
Expand Down Expand Up @@ -106,7 +116,6 @@ fn parse(tokens: Vec<Token>) -> Result<Expr> {
Ok(expr)
}
fn parse_expr(cur: &mut Peekable<Iter<Token>>) -> Result<Expr> {
println!("peek: {:?}", cur.peek());
let node = if let Some(token) = cur.next() {
match token {
Token::String(s) => Expr::String(s.clone()),
Expand Down Expand Up @@ -157,7 +166,6 @@ fn parse_call(identifier: String, cur: &mut Peekable<Iter<Token>>) -> Result<Exp
match cur.next() {
Some(Token::OpenParen) => {
let mut args = Vec::new();
let mut first_arg = true;
loop {
match cur.peek() {
Some(&&Token::CloseParen) => {
Expand Down Expand Up @@ -625,18 +633,13 @@ mod tests {
assert_eq!(result, Value::String("pong".to_string()));
Ok(())
}

// TODO: more negative tests
// #[test]
fn test_evaluation_error() -> Result<()> {
#[test]
fn test_eval_lower_call() -> Result<()> {
let result = evaluate_expression(
"$hello".to_string(),
EvalContext::new(&Variables::from([(
"hello".to_string(),
Value::String("goodbye".to_string()),
)])),
"$lower('FOO')".to_string(),
EvalContext::new(&Variables::new()),
)?;
assert_eq!(result, Value::Error("$hello".to_string()));
assert_eq!(result, Value::String("foo".to_string()));
Ok(())
}
}
6 changes: 5 additions & 1 deletion esi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,11 @@ fn event_receiver(
}) => {
let mut chose_branch = false;
for (when, events) in when_branches {
if let Tag::When { test, match_name } = when {
if let Tag::When {
test,
match_name: _,
} = when
{
let result = evaluate_expression(test, EvalContext::new(&variables))?;
if result.to_bool() {
chose_branch = true;
Expand Down

0 comments on commit 3b4ed98

Please sign in to comment.