跳转到主要内容

使用Rust后端实现的JsonLogic

项目描述

json-logic-rs

Continuous Integration

这是JsonLogic规范的Rust实现。

项目状态

我们实现了此处定义的100%的标准支持操作 这里

我们还实现了 ?:,该操作在该规范中没有描述,但它是 if 的直接别名。

所有操作都使用我们自己的Rust测试套件以及所有JsonLogic实现共享的测试进行了测试 这里

我们正在努力添加新的操作,提高类型安全性,以及将函数定义为JsonLogic的能力。我们将与更广泛的JsonLogic社区沟通,看看我们能否在这样做的时候将它们作为标准的一部分。

由于是在Rust中构建的,我们能够以多种语言提供该软件包。下表描述了当前的语言支持

语言 通过以下方式提供
Rust Cargo
JavaScript(作为WASM) 通过NPM提供 NPM
Python PyPI

安装

Rust

要将作为Rust库使用,请将其添加到您的 Cargo.toml

[dependencies]
jsonlogic-rs = "~0.1"

如果您只想使用命令行 jsonlogic 二进制文件

cargo install jsonlogic-rs --features cmdline

节点/浏览器

您可以使用npm或yarn安装JsonLogic。在NPM

npm install --save @bestow/jsonlogic-rs

请注意,该软件包作为node软件包分发,因此您需要使用browserifywebpack或类似工具来为浏览器安装。

Python

支持Python 3.7及以上版本。

提供了许多平台的车轮,因此您通常可以直接运行

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,并且cargo必须在您的PATH中可用。

如果您想构建或测试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导入,但需要在浏览器中使用browserify才能使用。

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/中。

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于安装软件包的信息。

源代码分发

jsonlogic_rs-0.4.0.tar.gz (35.8 kB 查看哈希)

上传于 来源

构建的发行版

jsonlogic_rs-0.4.0-cp312-cp312-musllinux_1_1_x86_64.whl (369.7 kB 查看哈希值)

上传于 CPython 3.12 musllinux: musl 1.1+ x86-64

jsonlogic_rs-0.4.0-cp312-cp312-musllinux_1_1_aarch64.whl (370.3 kB 查看哈希值)

上传于 CPython 3.12 musllinux: musl 1.1+ ARM64

jsonlogic_rs-0.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (337.5 kB 查看哈希值)

上传于 CPython 3.12 manylinux: glibc 2.17+ x86-64

jsonlogic_rs-0.4.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (337.6 kB 查看哈希值)

上传于 CPython 3.12 manylinux: glibc 2.17+ ARM64

jsonlogic_rs-0.4.0-cp312-cp312-macosx_11_0_arm64.whl (295.4 kB 查看哈希值)

上传于 CPython 3.12 macOS 11.0+ ARM64

jsonlogic_rs-0.4.0-cp312-cp312-macosx_10_9_x86_64.whl (305.4 kB 查看哈希值)

上传于 CPython 3.12 macOS 10.9+ x86-64

jsonlogic_rs-0.4.0-cp312-cp312-macosx_10_9_universal2.whl (593.5 kB 查看哈希值)

上传于 CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

jsonlogic_rs-0.4.0-cp311-cp311-musllinux_1_1_x86_64.whl (369.7 kB 查看哈希值)

上传于 CPython 3.11 musllinux: musl 1.1+ x86-64

jsonlogic_rs-0.4.0-cp311-cp311-musllinux_1_1_aarch64.whl (370.3 kB 查看哈希值)

上传于 CPython 3.11 musllinux: musl 1.1+ ARM64

jsonlogic_rs-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (337.5 kB 查看哈希值)

上传于 CPython 3.11 manylinux: glibc 2.17+ x86-64

jsonlogic_rs-0.4.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (337.6 kB 查看哈希值)

上传时间 CPython 3.11 manylinux: glibc 2.17+ ARM64

jsonlogic_rs-0.4.0-cp311-cp311-macosx_11_0_arm64.whl (295.4 kB 查看哈希值)

上传时间 CPython 3.11 macOS 11.0+ ARM64

jsonlogic_rs-0.4.0-cp311-cp311-macosx_10_9_x86_64.whl (305.4 kB 查看哈希值)

上传时间 CPython 3.11 macOS 10.9+ x86-64

jsonlogic_rs-0.4.0-cp311-cp311-macosx_10_9_universal2.whl (593.5 kB 查看哈希值)

上传时间 CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

jsonlogic_rs-0.4.0-cp310-cp310-musllinux_1_1_x86_64.whl (369.7 kB 查看哈希值)

上传时间 CPython 3.10 musllinux: musl 1.1+ x86-64

jsonlogic_rs-0.4.0-cp310-cp310-musllinux_1_1_aarch64.whl (370.3 kB 查看哈希值)

上传时间 CPython 3.10 musllinux: musl 1.1+ ARM64

jsonlogic_rs-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (337.5 kB 查看哈希值)

上传时间 CPython 3.10 manylinux: glibc 2.17+ x86-64

jsonlogic_rs-0.4.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (337.6 kB 查看哈希值)

上传时间 CPython 3.10 manylinux: glibc 2.17+ ARM64

jsonlogic_rs-0.4.0-cp310-cp310-macosx_11_0_arm64.whl (295.4 kB 查看哈希值)

上传时间 CPython 3.10 macOS 11.0+ ARM64

jsonlogic_rs-0.4.0-cp310-cp310-macosx_10_9_x86_64.whl (305.4 kB 查看哈希值)

上传时间 CPython 3.10 macOS 10.9+ x86-64

jsonlogic_rs-0.4.0-cp310-cp310-macosx_10_9_universal2.whl (593.5 kB 查看哈希值)

上传于 CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

jsonlogic_rs-0.4.0-cp39-cp39-musllinux_1_1_x86_64.whl (369.7 kB 查看哈希值)

上传于 CPython 3.9 musllinux: musl 1.1+ x86-64

jsonlogic_rs-0.4.0-cp39-cp39-musllinux_1_1_aarch64.whl (370.3 kB 查看哈希值)

上传于 CPython 3.9 musllinux: musl 1.1+ ARM64

jsonlogic_rs-0.4.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (337.5 kB 查看哈希值)

上传于 CPython 3.9 manylinux: glibc 2.17+ x86-64

jsonlogic_rs-0.4.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (337.6 kB 查看哈希值)

上传于 CPython 3.9 manylinux: glibc 2.17+ ARM64

jsonlogic_rs-0.4.0-cp39-cp39-macosx_11_0_arm64.whl (295.4 kB 查看哈希值)

上传于 CPython 3.9 macOS 11.0+ ARM64

jsonlogic_rs-0.4.0-cp39-cp39-macosx_10_9_x86_64.whl (305.4 kB 查看哈希值)

上传于 CPython 3.9 macOS 10.9+ x86-64

jsonlogic_rs-0.4.0-cp39-cp39-macosx_10_9_universal2.whl (593.5 kB 查看哈希值)

上传于 CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

jsonlogic_rs-0.4.0-cp38-cp38-musllinux_1_1_x86_64.whl (369.8 kB 查看哈希值)

上传于 CPython 3.8 musllinux: musl 1.1+ x86-64

jsonlogic_rs-0.4.0-cp38-cp38-musllinux_1_1_aarch64.whl (370.3 kB 查看哈希值)

上传于 CPython 3.8 musllinux: musl 1.1+ ARM64

jsonlogic_rs-0.4.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (337.5 kB 查看哈希值)

上传时间 CPython 3.8 manylinux: glibc 2.17+ x86-64

jsonlogic_rs-0.4.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (337.6 kB 查看哈希值)

上传时间 CPython 3.8 manylinux: glibc 2.17+ ARM64

jsonlogic_rs-0.4.0-cp38-cp38-macosx_11_0_arm64.whl (295.4 kB 查看哈希值)

上传时间 CPython 3.8 macOS 11.0+ ARM64

jsonlogic_rs-0.4.0-cp38-cp38-macosx_10_9_x86_64.whl (305.4 kB 查看哈希值)

上传时间 CPython 3.8 macOS 10.9+ x86-64

jsonlogic_rs-0.4.0-cp38-cp38-macosx_10_9_universal2.whl (593.5 kB 查看哈希值)

上传时间 CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

由以下机构支持