use serde_json::Value;
pub fn eval_type(value: &Value) -> Value {
let type_str = match value {
Value::Null => "null",
Value::Bool(_) => "boolean",
Value::Number(_) => "number",
Value::String(_) => "string",
Value::Array(_) => "array",
Value::Object(_) => "object",
};
Value::String(type_str.to_string())
}
pub fn eval_not(value: &Value) -> Value {
let is_falsy = matches!(value, Value::Null | Value::Bool(false));
Value::Bool(is_falsy)
}
#[cfg(test)]
mod tests {
use crate::filter::builtins::{eval, Builtin};
use serde_json::{json, Value};
#[test]
fn test_type_null() {
assert_eq!(eval(&Builtin::Type, &json!(null)).unwrap(), vec![json!("null")]);
}
#[test]
fn test_type_boolean() {
assert_eq!(eval(&Builtin::Type, &json!(true)).unwrap(), vec![json!("boolean")]);
assert_eq!(eval(&Builtin::Type, &json!(false)).unwrap(), vec![json!("boolean")]);
}
#[test]
fn test_type_number() {
assert_eq!(eval(&Builtin::Type, &json!(42)).unwrap(), vec![json!("number")]);
assert_eq!(eval(&Builtin::Type, &json!(3.14)).unwrap(), vec![json!("number")]);
}
#[test]
fn test_type_string() {
assert_eq!(eval(&Builtin::Type, &json!("hello")).unwrap(), vec![json!("string")]);
}
#[test]
fn test_type_array() {
assert_eq!(eval(&Builtin::Type, &json!([1, 2, 3])).unwrap(), vec![json!("array")]);
}
#[test]
fn test_type_object() {
assert_eq!(eval(&Builtin::Type, &json!({"a": 1})).unwrap(), vec![json!("object")]);
}
#[test]
fn test_empty() {
let empty: Vec<Value> = vec![];
assert_eq!(eval(&Builtin::Empty, &json!({"anything": "here"})).unwrap(), empty);
assert_eq!(eval(&Builtin::Empty, &json!(null)).unwrap(), empty);
}
#[test]
fn test_not_null() {
assert_eq!(eval(&Builtin::Not, &json!(null)).unwrap(), vec![json!(true)]);
}
#[test]
fn test_not_false() {
assert_eq!(eval(&Builtin::Not, &json!(false)).unwrap(), vec![json!(true)]);
}
#[test]
fn test_not_true() {
assert_eq!(eval(&Builtin::Not, &json!(true)).unwrap(), vec![json!(false)]);
}
#[test]
fn test_not_truthy_values() {
assert_eq!(eval(&Builtin::Not, &json!(0)).unwrap(), vec![json!(false)]);
assert_eq!(eval(&Builtin::Not, &json!("")).unwrap(), vec![json!(false)]);
assert_eq!(eval(&Builtin::Not, &json!([])).unwrap(), vec![json!(false)]);
assert_eq!(eval(&Builtin::Not, &json!({})).unwrap(), vec![json!(false)]);
}
}