Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 93 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ version = "0.21.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567"

[[package]]
name = "base64"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"

[[package]]
name = "basic-toml"
version = "0.1.10"
Expand Down Expand Up @@ -1750,6 +1756,12 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"

[[package]]
name = "hifijson"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "242402749acf71e6f32f5857598b7002c4058a4e3c3b22b4c7d51cab9aea754e"

[[package]]
name = "home"
version = "0.5.12"
Expand Down Expand Up @@ -2121,17 +2133,67 @@ version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"

[[package]]
name = "jaq-core"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7561783b20275a6c9cb576e39208b0c635f34ef14357f1f05a2927a774f3adec"
dependencies = [
"dyn-clone",
"once_cell",
"typed-arena",
]

[[package]]
name = "jaq-json"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4ec9aaad7340e6990c6c1878ef3b46dbec624e535d7f786cc9ddcf94f773d33"
dependencies = [
"bstr",
"bytes",
"foldhash 0.1.5",
"hifijson",
"indexmap",
"jaq-core",
"jaq-std",
"num-bigint",
"num-traits",
"ryu",
"self_cell",
"serde_core",
]

[[package]]
name = "jaq-std"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8bdc5a74b0feeb5e6a1dc2dd08c34280a61e37668d10a6a3b27ad69d0fb9ce2e"
dependencies = [
"aho-corasick",
"base64 0.22.1",
"bstr",
"jaq-core",
"jiff",
"libm",
"log",
"regex-bites",
"urlencoding",
]

[[package]]
name = "jiff"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49cce2b81f2098e7e3efc35bc2e0a6b7abec9d34128283d7a26fa8f32a6dbb35"
dependencies = [
"jiff-static",
"jiff-tzdb-platform",
"log",
"portable-atomic",
"portable-atomic-util",
"serde_core",
"windows-sys 0.61.2",
]

[[package]]
Expand All @@ -2145,6 +2207,21 @@ dependencies = [
"syn",
]

[[package]]
name = "jiff-tzdb"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c900ef84826f1338a557697dc8fc601df9ca9af4ac137c7fb61d4c6f2dfd3076"

[[package]]
name = "jiff-tzdb-platform"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "875a5a69ac2bab1a891711cf5eccbec1ce0341ea805560dcd90b7a2e925132e8"
dependencies = [
"jiff-tzdb",
]

[[package]]
name = "jobserver"
version = "0.1.34"
Expand All @@ -2171,6 +2248,9 @@ version = "0.1.0"
dependencies = [
"fs-err",
"getopts",
"jaq-core",
"jaq-json",
"jaq-std",
"jsonpath-rust",
"regex",
"serde_json",
Expand Down Expand Up @@ -3408,6 +3488,12 @@ dependencies = [
"regex-syntax",
]

[[package]]
name = "regex-bites"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6a15a2fa0bfda9361941c45550896ae87b15cc6c8c939ea350079670332e211"

[[package]]
name = "regex-lite"
version = "0.1.8"
Expand Down Expand Up @@ -4942,7 +5028,7 @@ version = "0.0.0"
dependencies = [
"arrayvec",
"askama",
"base64",
"base64 0.21.7",
"expect-test",
"indexmap",
"itertools",
Expand Down Expand Up @@ -5941,6 +6027,12 @@ dependencies = [
"rustc-hash 2.1.1",
]

[[package]]
name = "typed-arena"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a"

[[package]]
name = "typeid"
version = "1.0.3"
Expand Down
22 changes: 20 additions & 2 deletions src/tools/compiletest/src/directives/directive_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,5 +324,23 @@ pub(crate) const KNOWN_HTMLDOCCK_DIRECTIVE_NAMES: &[&str] = &[
"!snapshot",
];

pub(crate) const KNOWN_JSONDOCCK_DIRECTIVE_NAMES: &[&str] =
&["count", "!count", "has", "!has", "is", "!is", "ismany", "!ismany", "set", "!set"];
pub(crate) const KNOWN_JSONDOCCK_DIRECTIVE_NAMES: &[&str] = &[
"count",
"!count",
"has",
"!has",
"is",
"!is",
"ismany",
"!ismany",
"set",
"jq_count",
"!jq_count",
"jq_has",
"!jq_has",
"jq_is",
"!jq_is",
"jq_ismany",
"!jq_ismany",
"jq_set",
];
3 changes: 3 additions & 0 deletions src/tools/jsondocck/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ regex = "1.4"
shlex = "1.0"
serde_json = "1.0"
fs-err = "2.5.0"
jaq-core = "3"
jaq-std = { version = "3", default-features = false }
jaq-json = { version = "2", features = ["serde"] }
34 changes: 34 additions & 0 deletions src/tools/jsondocck/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ use std::collections::HashMap;
use std::path::Path;

use fs_err as fs;
use jaq_core::load::{Arena, File, Loader};
use jaq_core::{Ctx, Vars, data, unwrap_valr};
use jaq_json::Val;
use serde_json::Value;

use crate::config::Config;

#[derive(Debug)]
pub struct Cache {
jq_value: Val,
pub jq_variables: HashMap<String, Val>,
value: Value,
pub variables: HashMap<String, Value>,
}
Comment thread
kohsine marked this conversation as resolved.
Expand All @@ -23,6 +28,8 @@ impl Cache {
let content = fs::read_to_string(&file_path).expect("failed to read JSON file");

Cache {
jq_value: serde_json::from_str::<Val>(&content).expect("failed to convert from JSON"),
jq_variables: HashMap::from([("FILE".to_owned(), config.template.clone().into())]),
value: serde_json::from_str::<Value>(&content).expect("failed to convert from JSON"),
variables: HashMap::from([("FILE".to_owned(), config.template.clone().into())]),
}
Expand All @@ -32,4 +39,31 @@ impl Cache {
pub fn select(&self, path: &str) -> Vec<&Value> {
jsonpath_rust::query::js_path_vals(path, &self.value).unwrap()
}

pub fn jq_select(&self, filter_src: &str) -> Vec<Val> {
let program = File { code: filter_src, path: () };

let defs = jaq_core::defs().chain(jaq_std::defs()).chain(jaq_json::defs());
let funs = jaq_core::funs().chain(jaq_std::funs()).chain(jaq_json::funs());

let loader = Loader::new(defs);
let arena = Arena::default();

let modules = loader.load(&arena, program).unwrap();

let vars = self.jq_variables.iter().map(|t| (format!("${}", t.0), t.1)).collect::<Vec<_>>();

let filter = jaq_core::Compiler::default()
.with_funs(funs)
.with_global_vars(vars.clone().iter().map(|t| t.0.as_str()))
.compile(modules)
.unwrap();

let ctx = Ctx::<data::JustLut<Val>>::new(
&filter.lut,
Vars::new(vars.iter().map(|t| t.1.clone())),
);

filter.id.run((ctx, self.jq_value.clone())).map(unwrap_valr).map(Result::unwrap).collect()
}
}
Loading
Loading