13个版本
0.4.0 | 2024年7月1日 |
---|---|
0.3.3 | 2024年4月16日 |
0.3.2 | 2023年7月22日 |
0.2.3 | 2022年1月5日 |
0.1.3 | 2020年7月16日 |
在 WebAssembly 中排名 74
每月下载量 216
在 2 个Crate中使用(通过 platz-chart-ext)
175KB
4K SLoC
json-logic-rs
这是JsonLogic规范在Rust中的实现。
项目状态
我们实现了此处定义的100%的标准支持操作。
我们还实现了?:
,这在规范中没有描述,但它是if
的直接别名。
所有操作都使用我们自己的测试套件以及在此处定义的所有JsonLogic实现的共享测试进行了测试。
我们正在努力添加新的操作,以提高类型安全性,以及将函数定义为JsonLogic的能力。我们将与更广泛的JsonLogic社区沟通,看我们是否可以将它们作为标准的一部分。
由于使用Rust构建,我们能够以多种语言提供该包。下表描述了当前的语言支持
语言 | 可通过以下方式获取 |
---|---|
Rust | Cargo |
JavaScript(作为WASM) | 通过NPM进行Node/Browser安装 |
Python | PyPI |
安装
Rust
要将其作为Rust库使用,请将其添加到您的Cargo.toml
[dependencies]
jsonlogic-rs = "~0.1"
如果您只想使用命令行jsonlogic
二进制文件
cargo install jsonlogic-rs --features cmdline
Node/Browser
您可以使用npm或yarn安装JsonLogic。在NPM中
npm install --save @bestow/jsonlogic-rs
请注意,该软件包作为node软件包分发,因此您需要使用browserify
、webpack
或类似工具来安装浏览器版。
Python
支持Python 3.7+。
为许多平台提供了wheel,因此您通常可以只需运行即可
pip install jsonlogic-rs
如果您的系统中没有相应的轮子,这将尝试构建包。为了使包成功构建,您必须在本地系统上安装 Rust,并且 cargo
必须存在于您的 PATH
中。
有关更多详细信息,请参阅下面的 构建。
用法
Rust
use jsonlogic_rs;
use serde_json::{json, from_str, Value};
// You can pass JSON values deserialized with serde straight into apply().
fn main() {
let data: Value = from_str(r#"{"a": 7}"#)
assert_eq!(
jsonlogic_rs::apply(
json!({"===": [{"var": "a"}, 7]}),
data,
),
json!(true)
);
}
JavaScript
const jsonlogic = require("jsonlogic-rs")
jsonlogic.apply(
{"===": [{"var": "a"}, 7]},
{"a": 7}
)
Python
import jsonlogic_rs
res = jsonlogic_rs.apply(
{"===": [{"var": "a"}, 7]},
{"a": 7}
)
assert res == True
# If You have serialized JsonLogic and data, the `apply_serialized` method can
# be used instead
res = jsonlogic_rs.apply_serialized(
'{"===": [{"var": "a"}, 7]}',
'{"a": 7}'
)
命令行
Parse JSON data with a JsonLogic rule.
When no <data> or <data> is -, read from stdin.
The result is written to stdout as JSON, so multiple calls
can be chained together if desired.
USAGE:
jsonlogic <logic> [data]
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
ARGS:
<logic> A JSON logic string
<data> A string of JSON data to parse. May be provided as stdin.
EXAMPLES:
jsonlogic '{"===": [{"var": "a"}, "foo"]}' '{"a": "foo"}'
jsonlogic '{"===": [1, 1]}' null
echo '{"a": "foo"}' | jsonlogic '{"===": [{"var": "a"}, "foo"]}'
Inspired by and conformant with the original JsonLogic (jsonlogic.com).
运行 jsonlogic --help
以获取最新用法。
多个结果链式调用的示例
$ echo '{"a": "a"}' \
| jsonlogic '{"if": [{"===": [{"var": "a"}, "a"]}, {"result": true}, {"result": false}]}' \
| jsonlogic '{"if": [{"!!": {"var": "result"}}, "result was true", "result was false"]}'
"result was true"
使用 jsonlogic
在命令行上探索 API
> curl -s "https://catfact.ninja/facts?limit=5"
{"current_page":1,"data":[{"fact":"The Egyptian Mau is probably the oldest breed of cat. In fact, the breed is so ancient that its name is the Egyptian word for \u201ccat.\u201d","length":132},{"fact":"Julius Ceasar, Henri II, Charles XI, and Napoleon were all afraid of cats.","length":74},{"fact":"Unlike humans, cats cannot detect sweetness which likely explains why they are not drawn to it at all.","length":102},{"fact":"Cats can be taught to walk on a leash, but a lot of time and patience is required to teach them. The younger the cat is, the easier it will be for them to learn.","length":161},{"fact":"Researchers believe the word \u201ctabby\u201d comes from Attabiyah, a neighborhood in Baghdad, Iraq. Tabbies got their name because their striped coats resembled the famous wavy patterns in the silk produced in this city.","length":212}],"first_page_url":"https:\/\/catfact.ninja\/facts?page=1","from":1,"last_page":67,"last_page_url":"https:\/\/catfact.ninja\/facts?page=67","next_page_url":"https:\/\/catfact.ninja\/facts?page=2","path":"https:\/\/catfact.ninja\/facts","per_page":"5","prev_page_url":null,"to":5,"total":332}
> curl -s "https://catfact.ninja/facts?limit=5" | jsonlogic '{"var": "data"}'
[{"fact":"A cat's appetite is the barometer of its health. Any cat that does not eat or drink for more than two days should be taken to a vet.","length":132},{"fact":"Some notable people who disliked cats: Napoleon Bonaparte, Dwight D. Eisenhower, Hitler.","length":89},{"fact":"During the time of the Spanish Inquisition, Pope Innocent VIII condemned cats as evil and thousands of cats were burned. Unfortunately, the widespread killing of cats led to an explosion of the rat population, which exacerbated the effects of the Black Death.","length":259},{"fact":"A cat has approximately 60 to 80 million olfactory cells (a human has between 5 and 20 million).","length":96},{"fact":"In just seven years, a single pair of cats and their offspring could produce a staggering total of 420,000 kittens.","length":115}]
> curl -s "https://catfact.ninja/facts?limit=5" | jsonlogic '{"var": "data.0"}'
{"fact":"A tiger's stripes are like fingerprints","length":39}
> curl -s "https://catfact.ninja/facts?limit=5" | jsonlogic '{"var": "data.0.fact"}'
"Neutering a male cat will, in almost all cases, stop him from spraying (territorial marking), fighting with other males (at least over females), as well as lengthen his life and improve its quality."
> curl -s "https://catfact.ninja/facts?limit=5" \
| jsonlogic '{"var": "data.0.fact"}' \
| jsonlogic '{"in": ["cat", {"var": ""}]}'
true
> curl -s "https://catfact.ninja/facts?limit=5" \
| jsonlogic '{"var": "data.0.fact"}' \
| jsonlogic '{"in": ["cat", {"var": ""}]}' \
| jsonlogic '{"if": [{"var": ""}, "fact contained cat", "fact did not contain cat"]}'
"fact contained cat"
构建
先决条件
您必须安装 Rust 并在 PATH
中提供 cargo
。
如果您想构建或测试 Python 发行版,Python 3.7 或更高版本必须在您的 PATH
中可用。Python 发行版必须包含 venv
模块(例如,Ubuntu)。
如果您想为 WASM 包运行测试,node
10 或更高版本必须在您的 PATH
中可用。
Rust
要构建 Rust 库,只需运行 cargo build
。
您可以使用 make build
创建一个发布版本构建。
WebAssembly
您可以使用以下命令构建调试 WASM 发布版本
make debug-wasm
您可以使用以下命令构建生产 WASM 发布版本
make build-wasm
构建的 WASM 包将位于 js/
。此包可以直接从 node
导入,但需要在浏览器中使用时进行浏览器化。
Python
要执行 Python 包的开发安装,请运行
make develop-py
这将自动在 venv/
中创建一个虚拟环境,安装必要的包,然后将 jsonlogic_rs
安装到该环境中。
注意:根据我们的 CI 经验,这可能在 Windows 上的 Python 3.8 中不起作用。如果您在 Windows 机器上运行此命令并确认其是否有效,请让我们知道!
要构建生产源分发
make build-py-sdist
要构建针对您当前系统架构和 Python 版本的 wheel
make build-py-wheel
Python 发行版由 Rust 生成的 C 扩展和一个在 py/jsonlogic_rs/
中找到的薄包装器组成。运行 make develop-py
将编译 C 扩展并将其放置在该目录中,它将由您的本地 venv 导入。在构建 wheel 时,包装器和 C 扩展都打包到结果 wheel 中,该 wheel 可在 dist/
中找到。在构建 sdist 时,不编译 Rust 扩展。Rust 和 Python 源代码一起以 .tar.gz
文件的形式分发,也位于 dist/
中。
依赖关系
~2–3.5MB
~66K SLoC