From 3b4ed9869de4645c54f470a2d1a5681f35408e64 Mon Sep 17 00:00:00 2001 From: Tyler McMullen Date: Thu, 5 Dec 2024 19:07:37 +0000 Subject: [PATCH] Implement 'lower' function in library --- esi/src/error.rs | 1 - esi/src/expression.rs | 27 +++++++++++++++------------ esi/src/lib.rs | 6 +++++- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/esi/src/error.rs b/esi/src/error.rs index 44bf393..2122a6f 100644 --- a/esi/src/error.rs +++ b/esi/src/error.rs @@ -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)] diff --git a/esi/src/expression.rs b/esi/src/expression.rs index adbd53a..e3f8e5a 100644 --- a/esi/src/expression.rs +++ b/esi/src/expression.rs @@ -74,6 +74,16 @@ fn eval_expr(expr: Expr, ctx: &EvalContext) -> Result { fn call_dispatch(identifier: String, args: Vec) -> Result { 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) @@ -106,7 +116,6 @@ fn parse(tokens: Vec) -> Result { Ok(expr) } fn parse_expr(cur: &mut Peekable>) -> Result { - println!("peek: {:?}", cur.peek()); let node = if let Some(token) = cur.next() { match token { Token::String(s) => Expr::String(s.clone()), @@ -157,7 +166,6 @@ fn parse_call(identifier: String, cur: &mut Peekable>) -> Result { let mut args = Vec::new(); - let mut first_arg = true; loop { match cur.peek() { Some(&&Token::CloseParen) => { @@ -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(()) } } diff --git a/esi/src/lib.rs b/esi/src/lib.rs index 976c5dc..c548f3f 100644 --- a/esi/src/lib.rs +++ b/esi/src/lib.rs @@ -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;