A JsonLogic implementation in Rust.
To use this library, add
[dependencies]
jsonlogic = "0.5"
to your Cargo.toml
.
use serde_json::{json, Value};
let rule = json!({
"===": [
2,
{ "var": "foo" }
]
});
let data = json!({ "foo": 2 });
assert_eq!(jsonlogic::apply(&rule, &data), Ok(Value::Bool(true)));
let data = json!({ "foo": 3 });
assert_eq!(jsonlogic::apply(&rule, &data), Ok(Value::Bool(false)));
See the examples
directory for more usage examples.
jsonlogic_rs supports all JsonLogic operations. For detailed informations about all operations and their arguments, head over to Supported Operations on jsonlogic.com.
It does differ quite a bit in how null values are handled though. Regardless of null or missing input, we never return a null value from an operation. Most of these changes can be checked through the null tests file.
For Rust usage examples and edge cases have a look at the linked tests for each operator below.
- Accessing Data
- Logic and Boolean Operations
- Numeric Operations
- Array Operations
- String Operations
- Miscellaneous
Besides the default operators, this repo also implements a few new operators:
><
: Receives a geo-coordinate and a list of region objects, and checks whether the coordinate is inside the any region.>.<
: Receives a geo-coordinate and a region object, and checks whether the coordinate is inside the region.>t<
: Receives a point, in virtual coordinates from 0 to 100_000 and a rectangle array of [x, y, width, height], to determine wether the point is within the rectangle. Third parameter is optional for mapping the rectangle to a different area of the plane. Returns a boolean.tsrep
: Receives the current unix timestamp, a timestamp from the start of the day and a repetition value to determine wether the current timestamp is a repeating point within the day. Returns a boolean.match
: Checks whether the first parameter matches a regular expression in the second parameter. Returns the match array ornull
.*=
: Checks whether the first argument starts with the second argument=*
: Checks whether the first argument ends with the second argument
You can find examples on how to use these operators on the test files geo
, time
and string_extra
.