Skip to content

Commit 26f4564

Browse files
committed
ENH: add log operator
1 parent 07d255a commit 26f4564

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

src/lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,17 @@ mod jsonlogic_tests {
940940
]
941941
}
942942

943+
fn log_cases() -> Vec<(Value, Value, Result<Value, ()>)> {
944+
vec![
945+
// Invalid number of arguments
946+
(json!({"log": []}), json!({}), Err(())),
947+
(json!({"log": [1, 2]}), json!({}), Err(())),
948+
// Correct number of arguments
949+
(json!({"log": [1]}), json!({}), Ok(json!(1))),
950+
(json!({"log": 1}), json!({}), Ok(json!(1))),
951+
]
952+
}
953+
943954
fn lt_cases() -> Vec<(Value, Value, Result<Value, ()>)> {
944955
vec![
945956
(json!({"<": [1, 2]}), json!({}), Ok(json!(true))),
@@ -1274,6 +1285,11 @@ mod jsonlogic_tests {
12741285
substr_cases().into_iter().for_each(assert_jsonlogic)
12751286
}
12761287

1288+
#[test]
1289+
fn test_log_op() {
1290+
log_cases().into_iter().for_each(assert_jsonlogic)
1291+
}
1292+
12771293
#[test]
12781294
fn test_lt_op() {
12791295
lt_cases().into_iter().for_each(assert_jsonlogic)

src/op/impure.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! Impure Operations
2+
3+
use serde_json::Value;
4+
5+
use crate::error::Error;
6+
7+
/// Log the Operation's Value(s)
8+
///
9+
/// The reference implementation ignores any arguments beyond the first,
10+
/// and the specification seems to indicate that the first argument is
11+
/// the only one considered, so we're doing the same.
12+
pub fn log(items: &Vec<&Value>) -> Result<Value, Error> {
13+
println!("{}", items[0]);
14+
Ok(items[0].clone())
15+
}

src/op/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::value::{Evaluated, Parsed};
1818
use crate::{js_op, Parser};
1919

2020
mod array;
21+
mod impure;
2122
mod logic;
2223
mod numeric;
2324
mod string;
@@ -181,7 +182,12 @@ pub const OPERATOR_MAP: phf::Map<&'static str, Operator> = phf_map! {
181182
"substr" => Operator {
182183
symbol: "substr",
183184
operator: string::substr,
184-
num_params: NumParams::Variadic(2..4)
185+
num_params: NumParams::Variadic(2..4),
186+
},
187+
"log" => Operator {
188+
symbol: "log",
189+
operator: impure::log,
190+
num_params: NumParams::Unary,
185191
},
186192
};
187193

src/op/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! String Operations
22
3-
use crate::NULL;
43
use serde_json::Value;
54
use std::cmp;
65
use std::convert::TryInto;
76

87
use crate::error::Error;
8+
use crate::NULL;
99

1010
/// Concatenate strings.
1111
///

0 commit comments

Comments
 (0)