├── .github └── workflows │ ├── ci.yml │ └── release.yml ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── PERFORMANCE.md ├── README.md ├── assets └── github-repositories.json ├── crates ├── jql-parser │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── README.md │ ├── src │ │ ├── combinators.rs │ │ ├── errors.rs │ │ ├── group.rs │ │ ├── lib.rs │ │ ├── parser.rs │ │ └── tokens.rs │ └── tests │ │ └── integration.rs ├── jql-runner │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── README.md │ ├── benches │ │ └── benchmark.rs │ ├── src │ │ ├── array.rs │ │ ├── errors.rs │ │ ├── lib.rs │ │ ├── object.rs │ │ └── runner.rs │ └── tests │ │ └── integration.rs └── jql │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── README.md │ └── src │ ├── args.rs │ ├── errors.rs │ ├── main.rs │ └── panic.rs ├── fuzz ├── Cargo.lock ├── Cargo.toml └── fuzz_targets │ └── fuzz_parser.rs ├── jql.svg ├── justfile ├── performance.sh ├── release.toml └── rustfmt.toml /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: 7 | - main 8 | 9 | jobs: 10 | audit: 11 | name: audit 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v4 15 | - uses: rustsec/audit-check@v1.4.1 16 | with: 17 | token: ${{ secrets.GITHUB_TOKEN }} 18 | 19 | clippy: 20 | name: clippy 21 | runs-on: ubuntu-latest 22 | steps: 23 | - uses: actions/checkout@v4 24 | - uses: dtolnay/rust-toolchain@stable 25 | with: 26 | components: clippy 27 | - uses: Swatinem/rust-cache@v2 28 | - run: cargo clippy --all-targets --all-features -- -D warnings 29 | 30 | fmt: 31 | name: format 32 | runs-on: ubuntu-latest 33 | steps: 34 | - uses: actions/checkout@v4 35 | - uses: dtolnay/rust-toolchain@nightly 36 | with: 37 | components: rustfmt 38 | - uses: Swatinem/rust-cache@v2 39 | - run: cargo fmt --all -- --check 40 | 41 | test: 42 | name: test 43 | runs-on: ${{ matrix.os }} 44 | strategy: 45 | fail-fast: false 46 | matrix: 47 | os: [ubuntu-latest, windows-latest, macOS-latest] 48 | rust: [stable, beta, nightly] 49 | steps: 50 | - uses: actions/checkout@v4 51 | - uses: dtolnay/rust-toolchain@master 52 | with: 53 | toolchain: ${{ matrix.rust }} 54 | - uses: Swatinem/rust-cache@v2 55 | - run: cargo test --all-features 56 | 57 | shell-tests: 58 | name: shell tests 59 | runs-on: ${{ matrix.os }} 60 | strategy: 61 | matrix: 62 | os: [ubuntu-latest, macOS-latest] 63 | rust: [stable, beta, nightly] 64 | steps: 65 | - uses: actions/checkout@v4 66 | - uses: dtolnay/rust-toolchain@master 67 | with: 68 | toolchain: ${{ matrix.rust }} 69 | - uses: Swatinem/rust-cache@v2 70 | - name: Test pipe 71 | run: if ! echo "{\"a\":4}" | cargo run '"a"' | grep -q "4"; then exit 1; fi 72 | - name: Test raw-string flag 73 | run: if echo "{\"foo\":\"bar\"}" | cargo run '"foo"' -r | grep -q \"bar\"; then exit 1; fi 74 | - name: Test heredoc 75 | run: | 76 | cargo run -q -- '"one"[2:0],"two","three"' < selectors.txt 100 | if !echo "{\"foo\":\"bar\"}" | cargo run -- -q selectors.txt | grep -q \"bar\"; then exit 1; fi 101 | 102 | bench: 103 | if: "!contains(github.ref, 'refs/heads/main')" 104 | name: bench 105 | runs-on: ${{ matrix.os }} 106 | strategy: 107 | matrix: 108 | os: [ubuntu-latest] 109 | rust: [stable, beta, nightly] 110 | steps: 111 | - uses: actions/checkout@v4 112 | - uses: dtolnay/rust-toolchain@master 113 | with: 114 | toolchain: ${{ matrix.rust }} 115 | - uses: Swatinem/rust-cache@v2 116 | - name: Run the benchmarks against the current branch, main and compare 117 | run: | 118 | git fetch origin main 119 | cargo bench --bench benchmark -- --noplot --save-baseline current 120 | git checkout -b main 121 | cargo bench --bench benchmark -- --noplot --save-baseline main 122 | cargo install critcmp --force 123 | critcmp main current 124 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: release 2 | 3 | on: 4 | push: 5 | tags: ["jql-v[0-9]+.[0-9]+.[0-9]+*"] 6 | 7 | jobs: 8 | performance: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v4 12 | with: 13 | ref: "main" 14 | 15 | - uses: dtolnay/rust-toolchain@stable 16 | 17 | - uses: Swatinem/rust-cache@v2 18 | 19 | - name: Install hyperfine and jql - jq is already available! 20 | run: cargo install hyperfine && cargo install jql 21 | 22 | - name: Run performance benchmarks 23 | run: ./performance.sh 24 | 25 | - name: Create pull-request 26 | uses: peter-evans/create-pull-request@v5 27 | with: 28 | author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com> 29 | body: Update PERFORMANCE.md file 30 | branch: performance 31 | commit-message: "chore(performance): update benchmarks" 32 | committer: GitHub 33 | delete-branch: true 34 | labels: enhancement 35 | reviewers: yamafaktory 36 | title: "[Performance] Update benchmarks" 37 | token: ${{ secrets.GITHUB_TOKEN }} 38 | 39 | build-release: 40 | runs-on: ${{ matrix.os }} 41 | strategy: 42 | fail-fast: false 43 | matrix: 44 | include: 45 | # Linux 46 | - os: ubuntu-latest 47 | target: x86_64-unknown-linux-musl 48 | archive: tar.gz 49 | archive-cmd: tar czf 50 | sha-cmd: sha256sum 51 | - os: ubuntu-latest 52 | target: aarch64-unknown-linux-musl 53 | archive: tar.gz 54 | archive-cmd: tar czf 55 | sha-cmd: sha256sum 56 | - os: ubuntu-latest 57 | target: arm-unknown-linux-musleabihf 58 | archive: tar.gz 59 | archive-cmd: tar czf 60 | sha-cmd: sha256sum 61 | - os: ubuntu-latest 62 | target: loongarch64-unknown-linux-gnu 63 | archive: tar.gz 64 | archive-cmd: tar czf 65 | sha-cmd: sha256sum 66 | 67 | # Darwin 68 | - os: macos-latest 69 | target: x86_64-apple-darwin 70 | archive: zip 71 | archive-cmd: zip -r 72 | sha-cmd: shasum -a 256 73 | - os: macos-latest 74 | target: aarch64-apple-darwin 75 | archive: zip 76 | archive-cmd: zip -r 77 | sha-cmd: shasum -a 256 78 | 79 | # Windows 80 | - os: windows-latest 81 | target: x86_64-pc-windows-msvc 82 | archive: zip 83 | archive-cmd: 7z a 84 | sha-cmd: sha256sum 85 | 86 | steps: 87 | - name: Checkout repo 88 | uses: actions/checkout@v4 89 | 90 | - name: Setup Rust toolchain 91 | uses: dtolnay/rust-toolchain@master 92 | with: 93 | target: ${{ matrix.target }} 94 | toolchain: stable 95 | 96 | - uses: Swatinem/rust-cache@v2 97 | 98 | - name: Build Linux 99 | if: matrix.os == 'ubuntu-latest' 100 | run: | 101 | cargo install cross --git https://github.com/cross-rs/cross 102 | cross build --release --target ${{ matrix.target }} 103 | 104 | - name: Build Darwin & Windows 105 | if: matrix.os == 'macos-latest' || matrix.os == 'windows-latest' 106 | run: cargo build --release --target ${{ matrix.target }} 107 | 108 | - name: Package Artifacts 109 | shell: bash 110 | run: | 111 | src=$(pwd) 112 | stage=$(mktemp -d) 113 | ver=${GITHUB_REF#refs/tags/} 114 | asset_name="$ver-${{ matrix.target }}.${{ matrix.archive }}" 115 | ASSET_PATH="$src/$asset_name" 116 | CHECKSUM_PATH="$ASSET_PATH.sha256" 117 | cp target/${{ matrix.target }}/release/jql $stage/ 118 | cd $stage 119 | ${{ matrix.archive-cmd }} $ASSET_PATH * 120 | cd $src 121 | ${{ matrix.sha-cmd }} $asset_name > $CHECKSUM_PATH 122 | if [ "$RUNNER_OS" == "Windows" ]; then 123 | echo "ASSET_PATH=$(cygpath -m $ASSET_PATH)" >> $GITHUB_ENV 124 | echo "CHECKSUM_PATH=$(cygpath -m $CHECKSUM_PATH)" >> $GITHUB_ENV 125 | else 126 | echo "ASSET_PATH=$ASSET_PATH" >> $GITHUB_ENV 127 | echo "CHECKSUM_PATH=$CHECKSUM_PATH" >> $GITHUB_ENV 128 | fi 129 | 130 | - name: Release 131 | uses: softprops/action-gh-release@v1 132 | if: startsWith(github.ref, 'refs/tags/') 133 | with: 134 | fail_on_unmatched_files: true 135 | files: | 136 | ${{ env.ASSET_PATH }} 137 | ${{ env.CHECKSUM_PATH }} 138 | env: 139 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 140 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/*.rs.bk 2 | /fuzz/artifacts 3 | /fuzz/corpus 4 | /fuzz/coverage 5 | /fuzz/target 6 | /target 7 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "addr2line" 7 | version = "0.24.2" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" 10 | dependencies = [ 11 | "gimli", 12 | ] 13 | 14 | [[package]] 15 | name = "adler2" 16 | version = "2.0.0" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" 19 | 20 | [[package]] 21 | name = "aho-corasick" 22 | version = "1.1.3" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" 25 | dependencies = [ 26 | "memchr", 27 | ] 28 | 29 | [[package]] 30 | name = "anes" 31 | version = "0.1.6" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" 34 | 35 | [[package]] 36 | name = "anstream" 37 | version = "0.6.18" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" 40 | dependencies = [ 41 | "anstyle", 42 | "anstyle-parse", 43 | "anstyle-query", 44 | "anstyle-wincon", 45 | "colorchoice", 46 | "is_terminal_polyfill", 47 | "utf8parse", 48 | ] 49 | 50 | [[package]] 51 | name = "anstyle" 52 | version = "1.0.10" 53 | source = "registry+https://github.com/rust-lang/crates.io-index" 54 | checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" 55 | 56 | [[package]] 57 | name = "anstyle-parse" 58 | version = "0.2.6" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" 61 | dependencies = [ 62 | "utf8parse", 63 | ] 64 | 65 | [[package]] 66 | name = "anstyle-query" 67 | version = "1.1.2" 68 | source = "registry+https://github.com/rust-lang/crates.io-index" 69 | checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" 70 | dependencies = [ 71 | "windows-sys 0.59.0", 72 | ] 73 | 74 | [[package]] 75 | name = "anstyle-wincon" 76 | version = "3.0.6" 77 | source = "registry+https://github.com/rust-lang/crates.io-index" 78 | checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" 79 | dependencies = [ 80 | "anstyle", 81 | "windows-sys 0.59.0", 82 | ] 83 | 84 | [[package]] 85 | name = "anyhow" 86 | version = "1.0.98" 87 | source = "registry+https://github.com/rust-lang/crates.io-index" 88 | checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" 89 | 90 | [[package]] 91 | name = "autocfg" 92 | version = "1.4.0" 93 | source = "registry+https://github.com/rust-lang/crates.io-index" 94 | checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" 95 | 96 | [[package]] 97 | name = "backtrace" 98 | version = "0.3.74" 99 | source = "registry+https://github.com/rust-lang/crates.io-index" 100 | checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" 101 | dependencies = [ 102 | "addr2line", 103 | "cfg-if", 104 | "libc", 105 | "miniz_oxide", 106 | "object", 107 | "rustc-demangle", 108 | "windows-targets", 109 | ] 110 | 111 | [[package]] 112 | name = "bumpalo" 113 | version = "3.16.0" 114 | source = "registry+https://github.com/rust-lang/crates.io-index" 115 | checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" 116 | 117 | [[package]] 118 | name = "bytes" 119 | version = "1.9.0" 120 | source = "registry+https://github.com/rust-lang/crates.io-index" 121 | checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" 122 | 123 | [[package]] 124 | name = "cast" 125 | version = "0.3.0" 126 | source = "registry+https://github.com/rust-lang/crates.io-index" 127 | checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" 128 | 129 | [[package]] 130 | name = "cc" 131 | version = "1.2.2" 132 | source = "registry+https://github.com/rust-lang/crates.io-index" 133 | checksum = "f34d93e62b03caf570cccc334cbc6c2fceca82f39211051345108adcba3eebdc" 134 | dependencies = [ 135 | "shlex", 136 | ] 137 | 138 | [[package]] 139 | name = "cfg-if" 140 | version = "1.0.0" 141 | source = "registry+https://github.com/rust-lang/crates.io-index" 142 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 143 | 144 | [[package]] 145 | name = "ciborium" 146 | version = "0.2.2" 147 | source = "registry+https://github.com/rust-lang/crates.io-index" 148 | checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" 149 | dependencies = [ 150 | "ciborium-io", 151 | "ciborium-ll", 152 | "serde", 153 | ] 154 | 155 | [[package]] 156 | name = "ciborium-io" 157 | version = "0.2.2" 158 | source = "registry+https://github.com/rust-lang/crates.io-index" 159 | checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" 160 | 161 | [[package]] 162 | name = "ciborium-ll" 163 | version = "0.2.2" 164 | source = "registry+https://github.com/rust-lang/crates.io-index" 165 | checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" 166 | dependencies = [ 167 | "ciborium-io", 168 | "half", 169 | ] 170 | 171 | [[package]] 172 | name = "clap" 173 | version = "4.5.37" 174 | source = "registry+https://github.com/rust-lang/crates.io-index" 175 | checksum = "eccb054f56cbd38340b380d4a8e69ef1f02f1af43db2f0cc817a4774d80ae071" 176 | dependencies = [ 177 | "clap_builder", 178 | "clap_derive", 179 | ] 180 | 181 | [[package]] 182 | name = "clap_builder" 183 | version = "4.5.37" 184 | source = "registry+https://github.com/rust-lang/crates.io-index" 185 | checksum = "efd9466fac8543255d3b1fcad4762c5e116ffe808c8a3043d4263cd4fd4862a2" 186 | dependencies = [ 187 | "anstream", 188 | "anstyle", 189 | "clap_lex", 190 | "strsim", 191 | ] 192 | 193 | [[package]] 194 | name = "clap_derive" 195 | version = "4.5.32" 196 | source = "registry+https://github.com/rust-lang/crates.io-index" 197 | checksum = "09176aae279615badda0765c0c0b3f6ed53f4709118af73cf4655d85d1530cd7" 198 | dependencies = [ 199 | "heck", 200 | "proc-macro2", 201 | "quote", 202 | "syn", 203 | ] 204 | 205 | [[package]] 206 | name = "clap_lex" 207 | version = "0.7.4" 208 | source = "registry+https://github.com/rust-lang/crates.io-index" 209 | checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" 210 | 211 | [[package]] 212 | name = "colorchoice" 213 | version = "1.0.3" 214 | source = "registry+https://github.com/rust-lang/crates.io-index" 215 | checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" 216 | 217 | [[package]] 218 | name = "colored_json" 219 | version = "5.0.0" 220 | source = "registry+https://github.com/rust-lang/crates.io-index" 221 | checksum = "e35980a1b846f8e3e359fd18099172a0857140ba9230affc4f71348081e039b6" 222 | dependencies = [ 223 | "serde", 224 | "serde_json", 225 | "yansi", 226 | ] 227 | 228 | [[package]] 229 | name = "criterion" 230 | version = "0.5.1" 231 | source = "registry+https://github.com/rust-lang/crates.io-index" 232 | checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" 233 | dependencies = [ 234 | "anes", 235 | "cast", 236 | "ciborium", 237 | "clap", 238 | "criterion-plot", 239 | "is-terminal", 240 | "itertools", 241 | "num-traits", 242 | "once_cell", 243 | "oorandom", 244 | "plotters", 245 | "rayon", 246 | "regex", 247 | "serde", 248 | "serde_derive", 249 | "serde_json", 250 | "tinytemplate", 251 | "walkdir", 252 | ] 253 | 254 | [[package]] 255 | name = "criterion-plot" 256 | version = "0.5.0" 257 | source = "registry+https://github.com/rust-lang/crates.io-index" 258 | checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" 259 | dependencies = [ 260 | "cast", 261 | "itertools", 262 | ] 263 | 264 | [[package]] 265 | name = "crossbeam-deque" 266 | version = "0.8.5" 267 | source = "registry+https://github.com/rust-lang/crates.io-index" 268 | checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" 269 | dependencies = [ 270 | "crossbeam-epoch", 271 | "crossbeam-utils", 272 | ] 273 | 274 | [[package]] 275 | name = "crossbeam-epoch" 276 | version = "0.9.18" 277 | source = "registry+https://github.com/rust-lang/crates.io-index" 278 | checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" 279 | dependencies = [ 280 | "crossbeam-utils", 281 | ] 282 | 283 | [[package]] 284 | name = "crossbeam-utils" 285 | version = "0.8.20" 286 | source = "registry+https://github.com/rust-lang/crates.io-index" 287 | checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" 288 | 289 | [[package]] 290 | name = "crunchy" 291 | version = "0.2.2" 292 | source = "registry+https://github.com/rust-lang/crates.io-index" 293 | checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" 294 | 295 | [[package]] 296 | name = "either" 297 | version = "1.13.0" 298 | source = "registry+https://github.com/rust-lang/crates.io-index" 299 | checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" 300 | 301 | [[package]] 302 | name = "equivalent" 303 | version = "1.0.1" 304 | source = "registry+https://github.com/rust-lang/crates.io-index" 305 | checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" 306 | 307 | [[package]] 308 | name = "gimli" 309 | version = "0.31.1" 310 | source = "registry+https://github.com/rust-lang/crates.io-index" 311 | checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" 312 | 313 | [[package]] 314 | name = "half" 315 | version = "2.4.1" 316 | source = "registry+https://github.com/rust-lang/crates.io-index" 317 | checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" 318 | dependencies = [ 319 | "cfg-if", 320 | "crunchy", 321 | ] 322 | 323 | [[package]] 324 | name = "hashbrown" 325 | version = "0.15.2" 326 | source = "registry+https://github.com/rust-lang/crates.io-index" 327 | checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" 328 | 329 | [[package]] 330 | name = "heck" 331 | version = "0.5.0" 332 | source = "registry+https://github.com/rust-lang/crates.io-index" 333 | checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" 334 | 335 | [[package]] 336 | name = "hermit-abi" 337 | version = "0.4.0" 338 | source = "registry+https://github.com/rust-lang/crates.io-index" 339 | checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" 340 | 341 | [[package]] 342 | name = "indexmap" 343 | version = "2.9.0" 344 | source = "registry+https://github.com/rust-lang/crates.io-index" 345 | checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e" 346 | dependencies = [ 347 | "equivalent", 348 | "hashbrown", 349 | "rayon", 350 | ] 351 | 352 | [[package]] 353 | name = "is-terminal" 354 | version = "0.4.13" 355 | source = "registry+https://github.com/rust-lang/crates.io-index" 356 | checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b" 357 | dependencies = [ 358 | "hermit-abi", 359 | "libc", 360 | "windows-sys 0.52.0", 361 | ] 362 | 363 | [[package]] 364 | name = "is_terminal_polyfill" 365 | version = "1.70.1" 366 | source = "registry+https://github.com/rust-lang/crates.io-index" 367 | checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" 368 | 369 | [[package]] 370 | name = "itertools" 371 | version = "0.10.5" 372 | source = "registry+https://github.com/rust-lang/crates.io-index" 373 | checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" 374 | dependencies = [ 375 | "either", 376 | ] 377 | 378 | [[package]] 379 | name = "itoa" 380 | version = "1.0.14" 381 | source = "registry+https://github.com/rust-lang/crates.io-index" 382 | checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" 383 | 384 | [[package]] 385 | name = "jql" 386 | version = "8.0.6" 387 | dependencies = [ 388 | "anyhow", 389 | "clap", 390 | "colored_json", 391 | "jql-runner", 392 | "serde", 393 | "serde_json", 394 | "serde_stacker", 395 | "tokio", 396 | ] 397 | 398 | [[package]] 399 | name = "jql-parser" 400 | version = "8.0.6" 401 | dependencies = [ 402 | "thiserror", 403 | "winnow", 404 | ] 405 | 406 | [[package]] 407 | name = "jql-runner" 408 | version = "8.0.6" 409 | dependencies = [ 410 | "criterion", 411 | "indexmap", 412 | "jql-parser", 413 | "rayon", 414 | "serde_json", 415 | "thiserror", 416 | ] 417 | 418 | [[package]] 419 | name = "js-sys" 420 | version = "0.3.74" 421 | source = "registry+https://github.com/rust-lang/crates.io-index" 422 | checksum = "a865e038f7f6ed956f788f0d7d60c541fff74c7bd74272c5d4cf15c63743e705" 423 | dependencies = [ 424 | "once_cell", 425 | "wasm-bindgen", 426 | ] 427 | 428 | [[package]] 429 | name = "libc" 430 | version = "0.2.167" 431 | source = "registry+https://github.com/rust-lang/crates.io-index" 432 | checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" 433 | 434 | [[package]] 435 | name = "log" 436 | version = "0.4.22" 437 | source = "registry+https://github.com/rust-lang/crates.io-index" 438 | checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" 439 | 440 | [[package]] 441 | name = "memchr" 442 | version = "2.7.4" 443 | source = "registry+https://github.com/rust-lang/crates.io-index" 444 | checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" 445 | 446 | [[package]] 447 | name = "miniz_oxide" 448 | version = "0.8.0" 449 | source = "registry+https://github.com/rust-lang/crates.io-index" 450 | checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" 451 | dependencies = [ 452 | "adler2", 453 | ] 454 | 455 | [[package]] 456 | name = "num-traits" 457 | version = "0.2.19" 458 | source = "registry+https://github.com/rust-lang/crates.io-index" 459 | checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" 460 | dependencies = [ 461 | "autocfg", 462 | ] 463 | 464 | [[package]] 465 | name = "object" 466 | version = "0.36.5" 467 | source = "registry+https://github.com/rust-lang/crates.io-index" 468 | checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" 469 | dependencies = [ 470 | "memchr", 471 | ] 472 | 473 | [[package]] 474 | name = "once_cell" 475 | version = "1.20.2" 476 | source = "registry+https://github.com/rust-lang/crates.io-index" 477 | checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" 478 | 479 | [[package]] 480 | name = "oorandom" 481 | version = "11.1.4" 482 | source = "registry+https://github.com/rust-lang/crates.io-index" 483 | checksum = "b410bbe7e14ab526a0e86877eb47c6996a2bd7746f027ba551028c925390e4e9" 484 | 485 | [[package]] 486 | name = "pin-project-lite" 487 | version = "0.2.15" 488 | source = "registry+https://github.com/rust-lang/crates.io-index" 489 | checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff" 490 | 491 | [[package]] 492 | name = "plotters" 493 | version = "0.3.7" 494 | source = "registry+https://github.com/rust-lang/crates.io-index" 495 | checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" 496 | dependencies = [ 497 | "num-traits", 498 | "plotters-backend", 499 | "plotters-svg", 500 | "wasm-bindgen", 501 | "web-sys", 502 | ] 503 | 504 | [[package]] 505 | name = "plotters-backend" 506 | version = "0.3.7" 507 | source = "registry+https://github.com/rust-lang/crates.io-index" 508 | checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" 509 | 510 | [[package]] 511 | name = "plotters-svg" 512 | version = "0.3.7" 513 | source = "registry+https://github.com/rust-lang/crates.io-index" 514 | checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" 515 | dependencies = [ 516 | "plotters-backend", 517 | ] 518 | 519 | [[package]] 520 | name = "proc-macro2" 521 | version = "1.0.92" 522 | source = "registry+https://github.com/rust-lang/crates.io-index" 523 | checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" 524 | dependencies = [ 525 | "unicode-ident", 526 | ] 527 | 528 | [[package]] 529 | name = "psm" 530 | version = "0.1.24" 531 | source = "registry+https://github.com/rust-lang/crates.io-index" 532 | checksum = "200b9ff220857e53e184257720a14553b2f4aa02577d2ed9842d45d4b9654810" 533 | dependencies = [ 534 | "cc", 535 | ] 536 | 537 | [[package]] 538 | name = "quote" 539 | version = "1.0.37" 540 | source = "registry+https://github.com/rust-lang/crates.io-index" 541 | checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" 542 | dependencies = [ 543 | "proc-macro2", 544 | ] 545 | 546 | [[package]] 547 | name = "rayon" 548 | version = "1.10.0" 549 | source = "registry+https://github.com/rust-lang/crates.io-index" 550 | checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" 551 | dependencies = [ 552 | "either", 553 | "rayon-core", 554 | ] 555 | 556 | [[package]] 557 | name = "rayon-core" 558 | version = "1.12.1" 559 | source = "registry+https://github.com/rust-lang/crates.io-index" 560 | checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" 561 | dependencies = [ 562 | "crossbeam-deque", 563 | "crossbeam-utils", 564 | ] 565 | 566 | [[package]] 567 | name = "regex" 568 | version = "1.11.1" 569 | source = "registry+https://github.com/rust-lang/crates.io-index" 570 | checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" 571 | dependencies = [ 572 | "aho-corasick", 573 | "memchr", 574 | "regex-automata", 575 | "regex-syntax", 576 | ] 577 | 578 | [[package]] 579 | name = "regex-automata" 580 | version = "0.4.9" 581 | source = "registry+https://github.com/rust-lang/crates.io-index" 582 | checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" 583 | dependencies = [ 584 | "aho-corasick", 585 | "memchr", 586 | "regex-syntax", 587 | ] 588 | 589 | [[package]] 590 | name = "regex-syntax" 591 | version = "0.8.5" 592 | source = "registry+https://github.com/rust-lang/crates.io-index" 593 | checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" 594 | 595 | [[package]] 596 | name = "rustc-demangle" 597 | version = "0.1.24" 598 | source = "registry+https://github.com/rust-lang/crates.io-index" 599 | checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" 600 | 601 | [[package]] 602 | name = "ryu" 603 | version = "1.0.18" 604 | source = "registry+https://github.com/rust-lang/crates.io-index" 605 | checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" 606 | 607 | [[package]] 608 | name = "same-file" 609 | version = "1.0.6" 610 | source = "registry+https://github.com/rust-lang/crates.io-index" 611 | checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" 612 | dependencies = [ 613 | "winapi-util", 614 | ] 615 | 616 | [[package]] 617 | name = "serde" 618 | version = "1.0.219" 619 | source = "registry+https://github.com/rust-lang/crates.io-index" 620 | checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" 621 | dependencies = [ 622 | "serde_derive", 623 | ] 624 | 625 | [[package]] 626 | name = "serde_derive" 627 | version = "1.0.219" 628 | source = "registry+https://github.com/rust-lang/crates.io-index" 629 | checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" 630 | dependencies = [ 631 | "proc-macro2", 632 | "quote", 633 | "syn", 634 | ] 635 | 636 | [[package]] 637 | name = "serde_json" 638 | version = "1.0.140" 639 | source = "registry+https://github.com/rust-lang/crates.io-index" 640 | checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" 641 | dependencies = [ 642 | "indexmap", 643 | "itoa", 644 | "memchr", 645 | "ryu", 646 | "serde", 647 | ] 648 | 649 | [[package]] 650 | name = "serde_stacker" 651 | version = "0.1.12" 652 | source = "registry+https://github.com/rust-lang/crates.io-index" 653 | checksum = "69c8defe6c780725cce4ec6ad3bd91e321baf6fa4e255df1f31e345d507ef01a" 654 | dependencies = [ 655 | "serde", 656 | "stacker", 657 | ] 658 | 659 | [[package]] 660 | name = "shlex" 661 | version = "1.3.0" 662 | source = "registry+https://github.com/rust-lang/crates.io-index" 663 | checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" 664 | 665 | [[package]] 666 | name = "stacker" 667 | version = "0.1.17" 668 | source = "registry+https://github.com/rust-lang/crates.io-index" 669 | checksum = "799c883d55abdb5e98af1a7b3f23b9b6de8ecada0ecac058672d7635eb48ca7b" 670 | dependencies = [ 671 | "cc", 672 | "cfg-if", 673 | "libc", 674 | "psm", 675 | "windows-sys 0.59.0", 676 | ] 677 | 678 | [[package]] 679 | name = "strsim" 680 | version = "0.11.1" 681 | source = "registry+https://github.com/rust-lang/crates.io-index" 682 | checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" 683 | 684 | [[package]] 685 | name = "syn" 686 | version = "2.0.90" 687 | source = "registry+https://github.com/rust-lang/crates.io-index" 688 | checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" 689 | dependencies = [ 690 | "proc-macro2", 691 | "quote", 692 | "unicode-ident", 693 | ] 694 | 695 | [[package]] 696 | name = "thiserror" 697 | version = "2.0.12" 698 | source = "registry+https://github.com/rust-lang/crates.io-index" 699 | checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" 700 | dependencies = [ 701 | "thiserror-impl", 702 | ] 703 | 704 | [[package]] 705 | name = "thiserror-impl" 706 | version = "2.0.12" 707 | source = "registry+https://github.com/rust-lang/crates.io-index" 708 | checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" 709 | dependencies = [ 710 | "proc-macro2", 711 | "quote", 712 | "syn", 713 | ] 714 | 715 | [[package]] 716 | name = "tinytemplate" 717 | version = "1.2.1" 718 | source = "registry+https://github.com/rust-lang/crates.io-index" 719 | checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" 720 | dependencies = [ 721 | "serde", 722 | "serde_json", 723 | ] 724 | 725 | [[package]] 726 | name = "tokio" 727 | version = "1.44.2" 728 | source = "registry+https://github.com/rust-lang/crates.io-index" 729 | checksum = "e6b88822cbe49de4185e3a4cbf8321dd487cf5fe0c5c65695fef6346371e9c48" 730 | dependencies = [ 731 | "backtrace", 732 | "bytes", 733 | "pin-project-lite", 734 | "tokio-macros", 735 | ] 736 | 737 | [[package]] 738 | name = "tokio-macros" 739 | version = "2.5.0" 740 | source = "registry+https://github.com/rust-lang/crates.io-index" 741 | checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8" 742 | dependencies = [ 743 | "proc-macro2", 744 | "quote", 745 | "syn", 746 | ] 747 | 748 | [[package]] 749 | name = "unicode-ident" 750 | version = "1.0.14" 751 | source = "registry+https://github.com/rust-lang/crates.io-index" 752 | checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" 753 | 754 | [[package]] 755 | name = "utf8parse" 756 | version = "0.2.2" 757 | source = "registry+https://github.com/rust-lang/crates.io-index" 758 | checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" 759 | 760 | [[package]] 761 | name = "walkdir" 762 | version = "2.5.0" 763 | source = "registry+https://github.com/rust-lang/crates.io-index" 764 | checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" 765 | dependencies = [ 766 | "same-file", 767 | "winapi-util", 768 | ] 769 | 770 | [[package]] 771 | name = "wasm-bindgen" 772 | version = "0.2.97" 773 | source = "registry+https://github.com/rust-lang/crates.io-index" 774 | checksum = "d15e63b4482863c109d70a7b8706c1e364eb6ea449b201a76c5b89cedcec2d5c" 775 | dependencies = [ 776 | "cfg-if", 777 | "once_cell", 778 | "wasm-bindgen-macro", 779 | ] 780 | 781 | [[package]] 782 | name = "wasm-bindgen-backend" 783 | version = "0.2.97" 784 | source = "registry+https://github.com/rust-lang/crates.io-index" 785 | checksum = "8d36ef12e3aaca16ddd3f67922bc63e48e953f126de60bd33ccc0101ef9998cd" 786 | dependencies = [ 787 | "bumpalo", 788 | "log", 789 | "once_cell", 790 | "proc-macro2", 791 | "quote", 792 | "syn", 793 | "wasm-bindgen-shared", 794 | ] 795 | 796 | [[package]] 797 | name = "wasm-bindgen-macro" 798 | version = "0.2.97" 799 | source = "registry+https://github.com/rust-lang/crates.io-index" 800 | checksum = "705440e08b42d3e4b36de7d66c944be628d579796b8090bfa3471478a2260051" 801 | dependencies = [ 802 | "quote", 803 | "wasm-bindgen-macro-support", 804 | ] 805 | 806 | [[package]] 807 | name = "wasm-bindgen-macro-support" 808 | version = "0.2.97" 809 | source = "registry+https://github.com/rust-lang/crates.io-index" 810 | checksum = "98c9ae5a76e46f4deecd0f0255cc223cfa18dc9b261213b8aa0c7b36f61b3f1d" 811 | dependencies = [ 812 | "proc-macro2", 813 | "quote", 814 | "syn", 815 | "wasm-bindgen-backend", 816 | "wasm-bindgen-shared", 817 | ] 818 | 819 | [[package]] 820 | name = "wasm-bindgen-shared" 821 | version = "0.2.97" 822 | source = "registry+https://github.com/rust-lang/crates.io-index" 823 | checksum = "6ee99da9c5ba11bd675621338ef6fa52296b76b83305e9b6e5c77d4c286d6d49" 824 | 825 | [[package]] 826 | name = "web-sys" 827 | version = "0.3.74" 828 | source = "registry+https://github.com/rust-lang/crates.io-index" 829 | checksum = "a98bc3c33f0fe7e59ad7cd041b89034fa82a7c2d4365ca538dda6cdaf513863c" 830 | dependencies = [ 831 | "js-sys", 832 | "wasm-bindgen", 833 | ] 834 | 835 | [[package]] 836 | name = "winapi-util" 837 | version = "0.1.9" 838 | source = "registry+https://github.com/rust-lang/crates.io-index" 839 | checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" 840 | dependencies = [ 841 | "windows-sys 0.59.0", 842 | ] 843 | 844 | [[package]] 845 | name = "windows-sys" 846 | version = "0.52.0" 847 | source = "registry+https://github.com/rust-lang/crates.io-index" 848 | checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" 849 | dependencies = [ 850 | "windows-targets", 851 | ] 852 | 853 | [[package]] 854 | name = "windows-sys" 855 | version = "0.59.0" 856 | source = "registry+https://github.com/rust-lang/crates.io-index" 857 | checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" 858 | dependencies = [ 859 | "windows-targets", 860 | ] 861 | 862 | [[package]] 863 | name = "windows-targets" 864 | version = "0.52.6" 865 | source = "registry+https://github.com/rust-lang/crates.io-index" 866 | checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" 867 | dependencies = [ 868 | "windows_aarch64_gnullvm", 869 | "windows_aarch64_msvc", 870 | "windows_i686_gnu", 871 | "windows_i686_gnullvm", 872 | "windows_i686_msvc", 873 | "windows_x86_64_gnu", 874 | "windows_x86_64_gnullvm", 875 | "windows_x86_64_msvc", 876 | ] 877 | 878 | [[package]] 879 | name = "windows_aarch64_gnullvm" 880 | version = "0.52.6" 881 | source = "registry+https://github.com/rust-lang/crates.io-index" 882 | checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" 883 | 884 | [[package]] 885 | name = "windows_aarch64_msvc" 886 | version = "0.52.6" 887 | source = "registry+https://github.com/rust-lang/crates.io-index" 888 | checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" 889 | 890 | [[package]] 891 | name = "windows_i686_gnu" 892 | version = "0.52.6" 893 | source = "registry+https://github.com/rust-lang/crates.io-index" 894 | checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" 895 | 896 | [[package]] 897 | name = "windows_i686_gnullvm" 898 | version = "0.52.6" 899 | source = "registry+https://github.com/rust-lang/crates.io-index" 900 | checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" 901 | 902 | [[package]] 903 | name = "windows_i686_msvc" 904 | version = "0.52.6" 905 | source = "registry+https://github.com/rust-lang/crates.io-index" 906 | checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" 907 | 908 | [[package]] 909 | name = "windows_x86_64_gnu" 910 | version = "0.52.6" 911 | source = "registry+https://github.com/rust-lang/crates.io-index" 912 | checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" 913 | 914 | [[package]] 915 | name = "windows_x86_64_gnullvm" 916 | version = "0.52.6" 917 | source = "registry+https://github.com/rust-lang/crates.io-index" 918 | checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" 919 | 920 | [[package]] 921 | name = "windows_x86_64_msvc" 922 | version = "0.52.6" 923 | source = "registry+https://github.com/rust-lang/crates.io-index" 924 | checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" 925 | 926 | [[package]] 927 | name = "winnow" 928 | version = "0.7.8" 929 | source = "registry+https://github.com/rust-lang/crates.io-index" 930 | checksum = "9e27d6ad3dac991091e4d35de9ba2d2d00647c5d0fc26c5496dee55984ae111b" 931 | dependencies = [ 932 | "memchr", 933 | ] 934 | 935 | [[package]] 936 | name = "yansi" 937 | version = "1.0.1" 938 | source = "registry+https://github.com/rust-lang/crates.io-index" 939 | checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" 940 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | resolver = "2" 3 | members = [ 4 | "crates/*" 5 | ] 6 | 7 | [workspace.dependencies] 8 | thiserror = "2.0.12" 9 | serde_json = { features = ["preserve_order", "unbounded_depth"], version = "1.0.140" } 10 | 11 | [workspace.package] 12 | authors = ["Davy Duperron "] 13 | categories = ["command-line-utilities", "filesystem"] 14 | edition = "2021" 15 | keywords = ["cli", "json", "terminal", "tool", "query"] 16 | license = "MIT OR Apache-2.0" 17 | readme = "README.md" 18 | repository = "https://github.com/yamafaktory/jql" 19 | version = "8.0.6" 20 | 21 | [workspace.lints.rust] 22 | missing_debug_implementations = "warn" 23 | missing_docs = "warn" 24 | nonstandard_style = { level = "deny", priority= -1 } 25 | rust_2021_compatibility = { level = "forbid", priority= -1 } 26 | unreachable_pub = "warn" 27 | unsafe_code = "deny" 28 | 29 | [workspace.lints.clippy] 30 | all = "deny" 31 | 32 | # https://github.com/rust-lang/cargo/issues/8264 33 | [profile.release] 34 | codegen-units = 1 35 | lto = true 36 | opt-level = 'z' 37 | panic = 'abort' 38 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2023 Davy Duperron 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright 2023 Davy Duperron 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /PERFORMANCE.md: -------------------------------------------------------------------------------- 1 | | Command | Mean [ms] | Min [ms] | Max [ms] | Relative | 2 | |:---|---:|---:|---:|---:| 3 | | `echo '[1, [2], [[3]]]' \| jq 'flatten'` | 2.5 ± 0.1 | 2.4 | 3.6 | 1.17 ± 0.09 | 4 | | `echo '[1, [2], [[3]]]' \| jql '..'` | 2.2 ± 0.2 | 2.0 | 6.5 | 1.00 | 5 | 6 | | Command | Mean [ms] | Min [ms] | Max [ms] | Relative | 7 | |:---|---:|---:|---:|---:| 8 | | `echo '[1, 2, 3]' \| jq '.[0]'` | 2.5 ± 0.2 | 2.4 | 7.2 | 1.16 ± 0.09 | 9 | | `echo '[1, 2, 3]' \| jql '[0]'` | 2.1 ± 0.1 | 2.0 | 3.0 | 1.00 | 10 | 11 | | Command | Mean [ms] | Min [ms] | Max [ms] | Relative | 12 | |:---|---:|---:|---:|---:| 13 | | `echo '{ "foo": "bar" }' \| jq '.foo'` | 2.5 ± 0.2 | 2.3 | 6.9 | 1.16 ± 0.16 | 14 | | `echo '{ "foo": "bar" }' \| jql '"foo"'` | 2.2 ± 0.2 | 2.0 | 9.5 | 1.00 | 15 | 16 | | Command | Mean [ms] | Min [ms] | Max [ms] | Relative | 17 | |:---|---:|---:|---:|---:| 18 | | `cat /home/runner/work/jql/jql/assets/github-repositories.json \| jq -r '[.[] \| {name: .name, url: .url, language: .language, stargazers_count: .stargazers_count, watchers_count: .watchers_count}]' > /dev/null` | 90.8 ± 4.5 | 86.7 | 126.5 | 5.29 ± 0.36 | 19 | | `cat /home/runner/work/jql/jql/assets/github-repositories.json \| jql '\|>{"name", "url", "language", "stargazers_count", "watchers_count"}' > /dev/null` | 17.2 ± 0.8 | 15.4 | 23.0 | 1.00 | 20 | 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![jql](jql.svg) 2 | 3 | --- 4 | 5 | [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/yamafaktory/jql/ci.yml?branch=main&logo=github&style=flat-square)](https://github.com/yamafaktory/jql/actions/workflows/ci.yml) 6 | [![Crates.io](https://img.shields.io/crates/v/jql?style=flat-square)](https://crates.io/crates/jql) 7 | [![Docs.rs](https://img.shields.io/docsrs/jql-parser?label=jql-parser%20docs&style=flat-square)](https://docs.rs/jql-parser/latest/jql_parser/) 8 | [![Docs.rs](https://img.shields.io/docsrs/jql-runner?label=jql-runner%20docs&style=flat-square)](https://docs.rs/jql-runner/latest/jql_runner/) 9 | 10 | `jql` is a JSON Query Language tool built with Rust 🦀. 11 | 12 | Pronounce it as **jackal** 🐺. 13 | 14 | ## 📜 Philosophy 15 | 16 | - ⚡Be fast 17 | - 🪶 Stay lightweight 18 | - 🎮 Keep its features as simple as possible 19 | - 🧠 Avoid redundancy 20 | - 💡 Provide meaningful error messages 21 | - 🍰 Eat JSON as input, process, output JSON back 22 | 23 | ## 🚀 Installation 24 | 25 | ### Alpine Linux 26 | 27 | The package is maintained by @jirutka. 28 | 29 | ```sh 30 | apk add jql 31 | ``` 32 | 33 | ### Archlinux 34 | 35 | The AUR package is maintained by @barklan. 36 | 37 | ```sh 38 | yay -S jql 39 | ``` 40 | 41 | ### Cargo 42 | 43 | ```sh 44 | cargo install jql 45 | ``` 46 | 47 | ### Cargo Binstall 48 | 49 | ```sh 50 | cargo binstall jql 51 | ``` 52 | 53 | ### Fedora 54 | 55 | ```sh 56 | dnf install jql 57 | ``` 58 | 59 | ### FreeBSD 60 | 61 | ```sh 62 | pkg install jql 63 | ``` 64 | 65 | ### Homebrew 66 | 67 | ```sh 68 | brew install jql 69 | ``` 70 | 71 | ### Nix 72 | 73 | ```sh 74 | nix-env -i jql 75 | ``` 76 | 77 | ### openSUSE 78 | 79 | ```sh 80 | zypper install jql 81 | ``` 82 | 83 | ### Manual installation from GitHub 84 | 85 | Compiled binary versions are automatically uploaded to GitHub when a new release is made. You can install `jql` manually by [downloading a release](https://github.com/yamafaktory/jql/releases). 86 | 87 | ## 🛠️ Usage 88 | 89 | To make a selection from a JSON input, `jql` expects a **query** as a sequence of **tokens**. 90 | 91 | To be fully compliant with the JSON format, `jql` always expect key selectors to be **double-quoted**, see [The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc8259#section-13). 92 | 93 | ```json 94 | { 95 | ".valid": 1337, 96 | "": "yeah!", 97 | "\"": "yup, valid too!" 98 | } 99 | ``` 100 | 101 | Consequently, to be shell compliant, a query must be either enclosed by single quotation marks or every inner double quotation mark must be escaped. 102 | 103 | ### Separators 104 | 105 | #### Group separator 106 | 107 | Group separators build up an array from sub-queries. 108 | 109 | **JSON input** 110 | 111 | ```json 112 | { "a": 1, "b": 2, "c": 3 } 113 | ``` 114 | 115 | **Query** 116 | 117 | ```sh 118 | '"a","b","c"' 119 | ``` 120 | 121 | **JSON output** 122 | 123 | ```json 124 | [1, 2, 3] 125 | ``` 126 | 127 | ### Selectors 128 | 129 | #### Arrays 130 | 131 | ##### Array index selector 132 | 133 | Indexes can be used in arbitrary order. 134 | 135 | **JSON input** 136 | 137 | ```json 138 | [1, 2, 3] 139 | ``` 140 | 141 | **Query** 142 | 143 | ```sh 144 | '[2,1]' 145 | ``` 146 | 147 | **JSON output** 148 | 149 | ```json 150 | [3, 2] 151 | ``` 152 | 153 | ##### Array range selector 154 | 155 | Range can be in natural order `[0:2]`, reversed `[2:0]`, without lower `[:2]` or upper bound `[0:]`. 156 | 157 | **JSON input** 158 | 159 | ```json 160 | [1, 2, 3] 161 | ``` 162 | 163 | **Query** 164 | 165 | ```sh 166 | '[2:1]' 167 | ``` 168 | 169 | **JSON output** 170 | 171 | ```json 172 | [3, 2] 173 | ``` 174 | 175 | ##### Lens selector 176 | 177 | Lens can be a combination of one or more selectors with or an optional value, a value being any of **boolean** | **null** | **number** | **string**. 178 | 179 | **JSON input** 180 | 181 | ```json 182 | [ 183 | { "a": 1, "b": { "d": 2 } }, 184 | { "a": 2, "b": "some" }, 185 | { "a": 2, "b": { "d": null } }, 186 | { "a": 2, "b": true }, 187 | { "c": 3, "b": 4 } 188 | ] 189 | ``` 190 | 191 | **Query** 192 | 193 | ```sh 194 | '|={"b""d"=2, "c"}' 195 | ``` 196 | 197 | **JSON output** 198 | 199 | ```json 200 | [ 201 | { "a": 1, "b": { "d": 2 } }, 202 | { "c": 3, "b": 4 } 203 | ] 204 | ``` 205 | 206 | #### Objects 207 | 208 | ##### Key selector 209 | 210 | Any valid JSON key can be used. 211 | 212 | **JSON input** 213 | 214 | ```json 215 | { "a": 1, "b": 2, "c": 3 } 216 | ``` 217 | 218 | **Query** 219 | 220 | ```sh 221 | '"c"' 222 | ``` 223 | 224 | **JSON output** 225 | 226 | ```json 227 | 3 228 | ``` 229 | 230 | ##### Multi key selector 231 | 232 | Keys can be used in arbitrary order. 233 | 234 | **JSON input** 235 | 236 | ```json 237 | { "a": 1, "b": 2, "c": 3 } 238 | ``` 239 | 240 | **Query** 241 | 242 | ```sh 243 | '{"c","a"}' 244 | ``` 245 | 246 | **JSON output** 247 | 248 | ```json 249 | { "c": 3, "a": 1 } 250 | ``` 251 | 252 | ##### Object index selector 253 | 254 | Indexes can be used in arbitrary order. 255 | 256 | **JSON input** 257 | 258 | ```json 259 | { "a": 1, "b": 2, "c": 3 } 260 | ``` 261 | 262 | **Query** 263 | 264 | ```sh 265 | '{2,0}' 266 | ``` 267 | 268 | **JSON output** 269 | 270 | ```json 271 | { "c": 3, "a": 1 } 272 | ``` 273 | 274 | ##### Object range selector 275 | 276 | Range can be in natural order `{0:2}`, reversed `{2:0}`, without lower `{:2}` or upper bound `{0:}`. 277 | 278 | **JSON input** 279 | 280 | ```json 281 | { "a": 1, "b": 2, "c": 3 } 282 | ``` 283 | 284 | **Query** 285 | 286 | ```sh 287 | '{2:1}' 288 | ``` 289 | 290 | **JSON output** 291 | 292 | ```json 293 | { "c": 3, "b": 2 } 294 | ``` 295 | 296 | #### Operators 297 | 298 | ##### Flatten operator 299 | 300 | Flattens arrays and objects. 301 | 302 | **JSON input** 303 | 304 | ```json 305 | [[[[[[[[[[[[[[{ "a": 1 }]]]]]]]]]]]]], [[[[[{ "b": 2 }]]]], { "c": 3 }], null] 306 | ``` 307 | 308 | **Query** 309 | 310 | ```sh 311 | '..' 312 | ``` 313 | 314 | **JSON output** 315 | 316 | ```json 317 | [{ "a": 1 }, { "b": 2 }, { "c": 3 }, null] 318 | ``` 319 | 320 | **JSON input** 321 | 322 | ```json 323 | { "a": { "c": false }, "b": { "d": { "e": { "f": 1, "g": { "h": 2 } } } } } 324 | ``` 325 | 326 | **Query** 327 | 328 | ```sh 329 | '..' 330 | ``` 331 | 332 | **JSON output** 333 | 334 | ```json 335 | { 336 | "a.c": false, 337 | "b.d.e.f": 1, 338 | "b.d.e.g.h": 2 339 | } 340 | ``` 341 | 342 | ##### Keys operator 343 | 344 | Returns the keys of an object or the indices of an array. Other primitives are returned as is. 345 | 346 | **JSON input** 347 | 348 | ```json 349 | { "a": 1, "b": 2, "c": 3 } 350 | ``` 351 | 352 | **Query** 353 | 354 | ```sh 355 | '@' 356 | ``` 357 | 358 | **JSON output** 359 | 360 | ```json 361 | ["a", "b", "c"] 362 | ``` 363 | 364 | ##### Pipe in operator 365 | 366 | Applies the next tokens in parallel on each element of an array. 367 | 368 | **JSON input** 369 | 370 | ```json 371 | { "a": [{ "b": { "c": 1 } }, { "b": { "c": 2 } }] } 372 | ``` 373 | 374 | **Query** 375 | 376 | ```sh 377 | '"a"|>"b""c"' 378 | ``` 379 | 380 | **JSON output** 381 | 382 | ```json 383 | [1, 2] 384 | ``` 385 | 386 | ##### Pipe out operator 387 | 388 | Stops the parallelization initiated by the pipe in operator. 389 | 390 | **JSON input** 391 | 392 | ```json 393 | { "a": [{ "b": { "c": 1 } }, { "b": { "c": 2 } }] } 394 | ``` 395 | 396 | **Query** 397 | 398 | ```sh 399 | '"a"|>"b""c"<|[1]' 400 | ``` 401 | 402 | **JSON output** 403 | 404 | ```json 405 | 2 406 | ``` 407 | 408 | ##### Truncate operator 409 | 410 | Maps the output into simple JSON primitives **boolean** | **null** | **number** | **string** | **[]** | **{}**. 411 | 412 | **JSON input** 413 | 414 | ```json 415 | { "a": [1, 2, 3] } 416 | ``` 417 | 418 | **Query** 419 | 420 | ```sh 421 | '"a"!' 422 | ``` 423 | 424 | **JSON output** 425 | 426 | ```json 427 | [] 428 | ``` 429 | 430 | ## 💻 Shell integration 431 | 432 | ### How to save the output 433 | 434 | ```sh 435 | jql '"a"' input.json > output.json 436 | ``` 437 | 438 | ### How to read from stdin 439 | 440 | ```sh 441 | cat test.json | jql '"a"' 442 | ``` 443 | 444 | ### Available flags 445 | 446 | #### Inline the JSON output 447 | 448 | By default, the output is pretty printed in a more human-readable way, this can be disabled. 449 | 450 | ```sh 451 | -i, --inline 452 | ``` 453 | 454 | #### Read the query from file 455 | 456 | The command will read the provided query from a file instead of the stdin. 457 | 458 | ```sh 459 | -q, --query 460 | ``` 461 | 462 | #### Write to stdout without JSON double-quotes 463 | 464 | This can be useful to drop the double-quotes surrounding a string primitive. 465 | 466 | ```sh 467 | -r, --raw-string 468 | ``` 469 | 470 | #### Read a stream of JSON data line by line 471 | 472 | This flag is only about reading processing any JSON output streamed line by line (e.g. Docker logs with the `--follow` flag). This is not an option to read an incomplete streamed content (e.g. a very large input). 473 | 474 | ```sh 475 | -s, --stream 476 | ``` 477 | 478 | #### Validate the JSON data 479 | 480 | The command will return a matching exit code based on the validity of the JSON content or file provided. 481 | 482 | ```sh 483 | -v, --validate 484 | ``` 485 | 486 | #### Print help 487 | 488 | ```sh 489 | -h, --help 490 | ``` 491 | 492 | #### Print version 493 | 494 | ```sh 495 | -V, --version 496 | ``` 497 | 498 | #### Help 499 | 500 | ```sh 501 | jql -h 502 | jql --help 503 | ``` 504 | 505 | ## 🦀 Workspace 506 | 507 | This project is composed of following crates: 508 | 509 | - jql (_binary_) 510 | - [jql-parser](https://docs.rs/jql-parser/latest/jql_parser/) (_library_) 511 | - [jql-runner](https://docs.rs/jql-runner/latest/jql_runner/) (_library_) 512 | 513 | ## Development 514 | 515 | Some commands are available as a `justfile` at the root of the workspace (testing / fuzzing). 516 | 517 | ### Prerequisites 518 | 519 | - [cargo-nextest](https://nexte.st/) 520 | - [just](https://just.systems/man/en/) 521 | 522 | ### Commands 523 | 524 | ```sh 525 | just --list 526 | ``` 527 | 528 | ## ⚠️ Non-goal 529 | 530 | There's no plan to align `jql` with `jq` or any other similar tool. 531 | 532 | ## ⚡ Performance 533 | 534 | Some benchmarks comparing a set of similar functionalities provided by this tool and [jq](https://stedolan.github.io/jq/) are available [here](PERFORMANCE.md). 535 | 536 | ## 📔 Licenses 537 | 538 | - [Apache License, Version 2.0](https://github.com/yamafaktory/jql/blob/main/LICENSE-APACHE) 539 | - [MIT license](https://github.com/yamafaktory/jql/blob/main/LICENSE-MIT) 540 | -------------------------------------------------------------------------------- /crates/jql-parser/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | authors.workspace = true 3 | categories.workspace = true 4 | description = "Parser for jql - the JSON Query Language tool." 5 | edition.workspace = true 6 | keywords.workspace = true 7 | license.workspace = true 8 | name = "jql-parser" 9 | readme.workspace = true 10 | repository.workspace = true 11 | version.workspace = true 12 | 13 | [dependencies] 14 | thiserror.workspace = true 15 | winnow = { version = "0.7.8", features = ["simd"] } 16 | 17 | [lib] 18 | path = "src/lib.rs" 19 | -------------------------------------------------------------------------------- /crates/jql-parser/LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | ../../LICENSE-APACHE -------------------------------------------------------------------------------- /crates/jql-parser/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | ../../LICENSE-MIT -------------------------------------------------------------------------------- /crates/jql-parser/README.md: -------------------------------------------------------------------------------- 1 | # jql-parser 2 | 3 | ## About 4 | 5 | This crate is a workspace dependency of the [jql](https://github.com/yamafaktory/jql) tool. 6 | 7 | ## Features 8 | 9 | - Parser 10 | - Errors 11 | - Group splitter 12 | - Tokens 13 | 14 | ## License 15 | 16 | For licensing information, please check the [workspace root](https://github.com/yamafaktory/jql). 17 | -------------------------------------------------------------------------------- /crates/jql-parser/src/combinators.rs: -------------------------------------------------------------------------------- 1 | use winnow::{ 2 | Parser, 3 | Result, 4 | ascii::{ 5 | digit1, 6 | multispace0, 7 | }, 8 | combinator::{ 9 | alt, 10 | delimited, 11 | dispatch, 12 | fail, 13 | opt, 14 | peek, 15 | preceded, 16 | repeat, 17 | separated, 18 | separated_pair, 19 | }, 20 | error::ParserError, 21 | token::{ 22 | any, 23 | literal, 24 | take_until, 25 | }, 26 | }; 27 | 28 | use crate::tokens::{ 29 | Index, 30 | LensValue, 31 | Range, 32 | Token, 33 | }; 34 | 35 | /// Colon. 36 | static COLON: char = ':'; 37 | /// Comma. 38 | static COMMA: char = ','; 39 | /// Curly brace open. 40 | static CURLY_BRACKET_OPEN: char = '{'; 41 | /// Curly brace close. 42 | static CURLY_BRACKET_CLOSE: char = '}'; 43 | /// Double-quote. 44 | static DOUBLE_QUOTE: char = '"'; 45 | /// Equal. 46 | static EQUAL: char = '='; 47 | /// Square brace open. 48 | static SQUARE_BRACKET_OPEN: char = '['; 49 | /// Square brace close. 50 | static SQUARE_BRACKET_CLOSE: char = ']'; 51 | 52 | /// False. 53 | static FALSE: &str = "false"; 54 | /// True. 55 | static TRUE: &str = "true"; 56 | 57 | /// Lenses start. 58 | static LENSES_START: &str = "|={"; 59 | 60 | /// Keys operator. 61 | static KEYS: &str = "@"; 62 | /// Flatten operator. 63 | static FLATTEN: &str = ".."; 64 | /// Group separator. 65 | static GROUP_SEP: &str = ","; 66 | /// Pipe in operator. 67 | static PIPE_IN: &str = "|>"; 68 | /// Pipe out operator. 69 | static PIPE_OUT: &str = "<|"; 70 | /// Truncate operator. 71 | static TRUNCATE: &str = "!"; 72 | 73 | /// A combinator which takes an `inner` parser and produces a parser which also 74 | /// consumes both leading and trailing whitespaces, returning the output of 75 | /// `inner`. 76 | pub(crate) fn trim<'a, F, O, E>(inner: F) -> impl Parser<&'a str, O, E> 77 | where 78 | E: ParserError<&'a str>, 79 | F: Parser<&'a str, O, E>, 80 | { 81 | delimited(multispace0, inner, multispace0) 82 | } 83 | 84 | /// A combinator which parses a stringified number as an `Index`. 85 | pub(crate) fn parse_number(input: &mut &str) -> Result { 86 | digit1.parse_to().parse_next(input) 87 | } 88 | 89 | /// A combinator which parses a key surrounded by double quotes. 90 | pub(crate) fn parse_key<'a>(input: &mut &'a str) -> Result<&'a str> { 91 | trim(delimited( 92 | DOUBLE_QUOTE, 93 | take_until(0.., r#"""#), 94 | DOUBLE_QUOTE, 95 | )) 96 | .parse_next(input) 97 | } 98 | 99 | /// A combinator which parses a list of `Index`. 100 | pub(crate) fn parse_indexes(input: &mut &str) -> Result> { 101 | separated(1.., parse_number, trim(COMMA)).parse_next(input) 102 | } 103 | 104 | /// A combinator which parses a list of keys. 105 | fn parse_keys<'a>(input: &mut &'a str) -> Result> { 106 | trim(separated(1.., parse_key, trim(COMMA))).parse_next(input) 107 | } 108 | 109 | /// A combinator which parses a list of keys surrounded by curly braces. 110 | pub(crate) fn parse_multi_key<'a>(input: &mut &'a str) -> Result> { 111 | delimited(CURLY_BRACKET_OPEN, parse_keys, CURLY_BRACKET_CLOSE).parse_next(input) 112 | } 113 | 114 | /// A combinator which parses an array of `Index`. 115 | pub(crate) fn parse_array_index(input: &mut &str) -> Result> { 116 | delimited( 117 | trim(SQUARE_BRACKET_OPEN), 118 | parse_indexes, 119 | trim(SQUARE_BRACKET_CLOSE), 120 | ) 121 | .parse_next(input) 122 | } 123 | 124 | /// A combinator which parses an array range. 125 | pub(crate) fn parse_array_range(input: &mut &str) -> Result<(Option, Option)> { 126 | trim(delimited( 127 | SQUARE_BRACKET_OPEN, 128 | separated_pair(opt(parse_number), trim(COLON), opt(parse_number)), 129 | SQUARE_BRACKET_CLOSE, 130 | )) 131 | .parse_next(input) 132 | } 133 | 134 | /// A combinator which parses a list of index surrounded by curly braces. 135 | pub(crate) fn parse_object_index(input: &mut &str) -> Result> { 136 | delimited( 137 | trim(CURLY_BRACKET_OPEN), 138 | parse_indexes, 139 | trim(CURLY_BRACKET_CLOSE), 140 | ) 141 | .parse_next(input) 142 | } 143 | 144 | /// A combinator which parses an object range. 145 | pub(crate) fn parse_object_range(input: &mut &str) -> Result<(Option, Option)> { 146 | delimited( 147 | trim(CURLY_BRACKET_OPEN), 148 | separated_pair(opt(parse_number), trim(COLON), opt(parse_number)), 149 | trim(CURLY_BRACKET_CLOSE), 150 | ) 151 | .parse_next(input) 152 | } 153 | 154 | /// A combinator which parses any lens value. 155 | pub(crate) fn parse_lens_value<'a>(input: &mut &'a str) -> Result> { 156 | dispatch! {peek(any); 157 | 'f' => FALSE.value(LensValue::Bool(false)), 158 | 't' => TRUE.value(LensValue::Bool(true)), 159 | 'n' => "null".value(LensValue::Null), 160 | '0'..='9' => digit1.try_map(|s: &str| s.parse::().map(LensValue::Number)), 161 | _ => parse_key.map(LensValue::String), 162 | } 163 | .parse_next(input) 164 | } 165 | // 166 | /// A combinator which parses a lens key. 167 | fn parse_lens_key<'a>(input: &mut &'a str) -> Result> { 168 | trim( 169 | dispatch! {peek(any); 170 | '[' => { 171 | alt(( 172 | parse_array_index.map(Token::ArrayIndexSelector), 173 | parse_array_range.map(|(start, end)| Token::ArrayRangeSelector(Range(start, end))), 174 | )) 175 | }, 176 | '"' => parse_key.map(Token::KeySelector), 177 | '{' => { 178 | alt(( 179 | parse_multi_key.map(Token::MultiKeySelector), 180 | parse_object_index.map(Token::ObjectIndexSelector), 181 | parse_object_range.map(|(start, end)| Token::ObjectRangeSelector(Range(start, end))), 182 | )) 183 | }, 184 | _ => fail 185 | } 186 | ) 187 | .parse_next(input) 188 | } 189 | 190 | /// A combinator which parses multiple lens keys. 191 | fn parse_lens_keys<'a>(input: &mut &'a str) -> Result>> { 192 | repeat(1.., parse_lens_key).parse_next(input) 193 | } 194 | 195 | /// A combinator which parses a lens. 196 | pub(crate) fn parse_lens<'a>( 197 | input: &mut &'a str, 198 | ) -> Result<(Vec>, Option>)> { 199 | trim(( 200 | parse_lens_keys, 201 | opt(preceded(trim(EQUAL), parse_lens_value)), 202 | )) 203 | .parse_next(input) 204 | } 205 | 206 | /// A combinator which parses a list of lenses. 207 | pub(crate) fn parse_lenses<'a>( 208 | input: &mut &'a str, 209 | ) -> Result>, Option>)>> { 210 | delimited( 211 | trim(LENSES_START), 212 | separated(1.., parse_lens, trim(COMMA)), 213 | trim(CURLY_BRACKET_CLOSE), 214 | ) 215 | .parse_next(input) 216 | } 217 | 218 | /// A combinator which parses a keys operator. 219 | pub(crate) fn parse_keys_operator<'a>(input: &mut &'a str) -> Result<&'a str> { 220 | literal(KEYS).parse_next(input) 221 | } 222 | 223 | /// A combinator which parses a flatten operator. 224 | pub(crate) fn parse_flatten_operator<'a>(input: &mut &'a str) -> Result<&'a str> { 225 | literal(FLATTEN).parse_next(input) 226 | } 227 | 228 | /// A combinator which parses a pipe in operator. 229 | pub(crate) fn parse_pipe_in_operator<'a>(input: &mut &'a str) -> Result<&'a str> { 230 | literal(PIPE_IN).parse_next(input) 231 | } 232 | 233 | /// A combinator which parses a pipe out operator. 234 | pub(crate) fn parse_pipe_out_operator<'a>(input: &mut &'a str) -> Result<&'a str> { 235 | literal(PIPE_OUT).parse_next(input) 236 | } 237 | 238 | /// A combinator which parses a truncate operator. 239 | pub(crate) fn parse_truncate_operator<'a>(input: &mut &'a str) -> Result<&'a str> { 240 | trim(TRUNCATE).parse_next(input) 241 | } 242 | 243 | /// A combinator which parses a group separator. 244 | pub(crate) fn parse_group_separator<'a>(input: &mut &'a str) -> Result<&'a str> { 245 | literal(GROUP_SEP).parse_next(input) 246 | } 247 | 248 | #[cfg(test)] 249 | mod tests { 250 | use super::{ 251 | FLATTEN, 252 | GROUP_SEP, 253 | KEYS, 254 | PIPE_IN, 255 | PIPE_OUT, 256 | TRUNCATE, 257 | parse_array_index, 258 | parse_array_range, 259 | parse_flatten_operator, 260 | parse_group_separator, 261 | parse_indexes, 262 | parse_key, 263 | parse_keys_operator, 264 | parse_lens, 265 | parse_lenses, 266 | parse_multi_key, 267 | parse_number, 268 | parse_object_index, 269 | parse_object_range, 270 | parse_pipe_in_operator, 271 | parse_pipe_out_operator, 272 | parse_truncate_operator, 273 | }; 274 | use crate::tokens::{ 275 | Index, 276 | LensValue, 277 | Token, 278 | }; 279 | 280 | #[test] 281 | fn check_parse_number() { 282 | assert_eq!(parse_number(&mut "123"), Ok(Index(123))); 283 | assert!(parse_number(&mut "abc").is_err()); 284 | assert!(parse_number(&mut "abc123").is_err()); 285 | } 286 | 287 | #[test] 288 | fn check_parse_key() { 289 | assert_eq!(parse_key(&mut r#""abc""#), Ok("abc")); 290 | assert!(parse_key(&mut "abc").is_err()); 291 | } 292 | 293 | #[test] 294 | fn check_parse_indexes() { 295 | assert_eq!(parse_indexes(&mut "123"), Ok(vec![Index(123)])); 296 | assert_eq!( 297 | parse_indexes(&mut "123,456,789"), 298 | Ok(vec![Index(123), Index(456), Index(789)]) 299 | ); 300 | assert!(parse_indexes(&mut "abc").is_err()); 301 | } 302 | 303 | #[test] 304 | fn check_parse_multi_key() { 305 | assert_eq!(parse_multi_key(&mut r#"{"abc"}"#), Ok(vec!["abc"])); 306 | assert_eq!( 307 | parse_multi_key(&mut r#"{"abc","def"}"#), 308 | Ok(vec!["abc", "def"]) 309 | ); 310 | assert!(parse_multi_key(&mut "{}").is_err()); 311 | assert!(parse_multi_key(&mut "{123}").is_err()); 312 | } 313 | 314 | #[test] 315 | fn check_parse_array_index() { 316 | assert_eq!(parse_array_index(&mut "[1]"), Ok(vec![Index(1)])); 317 | assert_eq!( 318 | parse_array_index(&mut "[1,2,3]"), 319 | Ok(vec![Index(1), Index(2), Index(3)]) 320 | ); 321 | assert!(parse_array_index(&mut "[]").is_err()); 322 | assert!(parse_array_index(&mut r#"["1"]"#).is_err()); 323 | } 324 | 325 | #[test] 326 | fn check_parse_array_range() { 327 | assert_eq!(parse_array_range(&mut "[:]"), Ok((None, None))); 328 | assert_eq!(parse_array_range(&mut "[1:]"), Ok((Some(Index(1)), None))); 329 | assert_eq!(parse_array_range(&mut "[:1]"), Ok((None, Some(Index(1))))); 330 | assert_eq!( 331 | parse_array_range(&mut "[1:3]"), 332 | Ok((Some(Index(1)), Some(Index(3)))) 333 | ); 334 | assert!(parse_array_range(&mut "[]").is_err()); 335 | } 336 | 337 | #[test] 338 | fn check_parse_object_index() { 339 | assert_eq!(parse_object_index(&mut "{1}"), Ok(vec![Index(1)])); 340 | assert_eq!( 341 | parse_object_index(&mut "{1,2}"), 342 | Ok(vec![Index(1), Index(2)]) 343 | ); 344 | assert!(parse_object_index(&mut "{}").is_err()); 345 | } 346 | 347 | #[test] 348 | fn check_parse_object_range() { 349 | assert_eq!(parse_object_range(&mut "{:}"), Ok((None, None))); 350 | assert_eq!(parse_object_range(&mut "{1:}"), Ok((Some(Index(1)), None))); 351 | assert_eq!(parse_object_range(&mut "{:1}"), Ok((None, Some(Index(1))))); 352 | assert_eq!( 353 | parse_object_range(&mut "{1:3}"), 354 | Ok((Some(Index(1)), Some(Index(3)))) 355 | ); 356 | assert!(parse_object_range(&mut "{}").is_err()); 357 | } 358 | 359 | #[test] 360 | fn check_parse_keys_operator() { 361 | assert_eq!(parse_keys_operator(&mut "@"), Ok(KEYS)); 362 | assert!(parse_keys_operator(&mut "").is_err()); 363 | } 364 | 365 | #[test] 366 | fn check_parse_flatten_operator() { 367 | assert_eq!(parse_flatten_operator(&mut ".."), Ok(FLATTEN)); 368 | assert!(parse_flatten_operator(&mut "").is_err()); 369 | } 370 | 371 | #[test] 372 | fn check_parse_pipe_in_operator() { 373 | assert_eq!(parse_pipe_in_operator(&mut "|>"), Ok(PIPE_IN)); 374 | assert!(parse_pipe_in_operator(&mut "").is_err()); 375 | } 376 | 377 | #[test] 378 | fn check_parse_pipe_out_operator() { 379 | assert_eq!(parse_pipe_out_operator(&mut "<|"), Ok(PIPE_OUT)); 380 | assert!(parse_pipe_out_operator(&mut "").is_err()); 381 | } 382 | 383 | #[test] 384 | fn check_parse_truncate_operator() { 385 | assert_eq!(parse_truncate_operator(&mut "!"), Ok(TRUNCATE)); 386 | assert!(parse_truncate_operator(&mut "").is_err()); 387 | } 388 | 389 | #[test] 390 | fn check_parse_group_separator() { 391 | assert_eq!(parse_group_separator(&mut ","), Ok(GROUP_SEP)); 392 | assert!(parse_group_separator(&mut "").is_err()); 393 | } 394 | 395 | #[test] 396 | fn check_parse_lens() { 397 | assert_eq!( 398 | parse_lens(&mut r#""abc""#), 399 | Ok((vec![Token::KeySelector("abc")], None)) 400 | ); 401 | assert_eq!( 402 | parse_lens(&mut r#""abc"=null"#), 403 | Ok((vec![Token::KeySelector("abc")], Some(LensValue::Null))) 404 | ); 405 | assert_eq!( 406 | parse_lens(&mut r#""abc"="def""#), 407 | Ok(( 408 | vec![Token::KeySelector("abc")], 409 | Some(LensValue::String("def")) 410 | )) 411 | ); 412 | assert_eq!( 413 | parse_lens(&mut r#""abc"=123"#), 414 | Ok(( 415 | vec![Token::KeySelector("abc")], 416 | Some(LensValue::Number(123)) 417 | )) 418 | ); 419 | assert_eq!( 420 | parse_lens(&mut r#""abc""bcd"[0]=123"#), 421 | Ok(( 422 | vec![ 423 | Token::KeySelector("abc"), 424 | Token::KeySelector("bcd"), 425 | Token::ArrayIndexSelector(vec![Index(0)]) 426 | ], 427 | Some(LensValue::Number(123)) 428 | )) 429 | ); 430 | assert!(parse_lens(&mut "").is_err()); 431 | } 432 | 433 | #[test] 434 | fn check_parse_lenses() { 435 | assert_eq!( 436 | parse_lenses(&mut r#"|={"abc","bcd"=123,"efg"=null,"hij"="test"}"#), 437 | Ok(vec![ 438 | (vec![Token::KeySelector("abc")], None), 439 | ( 440 | vec![Token::KeySelector("bcd")], 441 | Some(LensValue::Number(123)) 442 | ), 443 | (vec![Token::KeySelector("efg")], Some(LensValue::Null)), 444 | ( 445 | vec![Token::KeySelector("hij")], 446 | Some(LensValue::String("test")) 447 | ), 448 | ]) 449 | ); 450 | } 451 | } 452 | -------------------------------------------------------------------------------- /crates/jql-parser/src/errors.rs: -------------------------------------------------------------------------------- 1 | use thiserror::Error; 2 | 3 | fn display_content(content: &str) -> String { 4 | if content.is_empty() { 5 | String::new() 6 | } else { 7 | format!(" after {content}") 8 | } 9 | } 10 | 11 | /// Error type returned by the parser. 12 | #[derive(Debug, Error, PartialEq)] 13 | pub enum JqlParserError { 14 | /// Empty input error. 15 | #[error("Empty input")] 16 | EmptyInputError, 17 | 18 | /// Parsing error. 19 | #[error("Unable to parse input {unparsed}{}", display_content(tokens))] 20 | ParsingError { 21 | /// Tokens found while parsing. 22 | tokens: String, 23 | /// Unparsed content. 24 | unparsed: String, 25 | }, 26 | 27 | /// Truncate error. 28 | #[error("Truncate operator found as non last element or multiple times in {0}")] 29 | TruncateError(String), 30 | 31 | /// Unknown error. 32 | #[error("Unknown error")] 33 | UnknownError, 34 | } 35 | 36 | #[cfg(test)] 37 | mod tests { 38 | 39 | use super::display_content; 40 | 41 | #[test] 42 | fn check_display_content() { 43 | assert_eq!(display_content("some"), " after some"); 44 | assert_eq!(display_content(""), ""); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /crates/jql-parser/src/group.rs: -------------------------------------------------------------------------------- 1 | use crate::tokens::Token; 2 | 3 | #[must_use] 4 | /// Splits a list of `Tokens` by `GroupSeparator`. 5 | /// Returns groups of `Tokens`. 6 | pub fn split<'a>(tokens: &'a [Token<'a>]) -> Vec>> { 7 | tokens 8 | .iter() 9 | .fold(vec![], |mut acc: Vec>, token| { 10 | if token == &Token::GroupSeparator { 11 | acc.push(vec![]); 12 | 13 | return acc; 14 | } 15 | 16 | if acc.is_empty() { 17 | acc.push(vec![]); 18 | } 19 | 20 | if let Some(last) = acc.last_mut() { 21 | last.push(token); 22 | } 23 | 24 | acc 25 | }) 26 | .into_iter() 27 | .filter(|group| !group.is_empty()) 28 | .collect() 29 | } 30 | 31 | #[cfg(test)] 32 | mod tests { 33 | 34 | use super::split; 35 | use crate::tokens::Token; 36 | 37 | #[test] 38 | fn check_split() { 39 | assert!(split(&[Token::GroupSeparator,]).is_empty()); 40 | assert_eq!( 41 | split(&[Token::KeySelector("abc")]), 42 | vec![vec![&Token::KeySelector("abc")]] 43 | ); 44 | assert_eq!( 45 | split(&[ 46 | Token::GroupSeparator, 47 | Token::GroupSeparator, 48 | Token::KeySelector("abc") 49 | ]), 50 | vec![vec![&Token::KeySelector("abc")]] 51 | ); 52 | assert_eq!( 53 | split(&[ 54 | Token::KeySelector("abc"), 55 | Token::GroupSeparator, 56 | Token::KeySelector("abc") 57 | ]), 58 | vec![ 59 | vec![&Token::KeySelector("abc")], 60 | vec![&Token::KeySelector("abc")], 61 | ] 62 | ); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /crates/jql-parser/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![doc = include_str!("../README.md")] 2 | #![doc( 3 | html_logo_url = "https://raw.githubusercontent.com/yamafaktory/jql/a438ab0039faf64c3329fb09bae399ae95601000/jql.svg" 4 | )] 5 | 6 | mod combinators; 7 | /// Parser errors. 8 | pub mod errors; 9 | /// Grouping utilities. 10 | pub mod group; 11 | /// Parsing utilities. 12 | pub mod parser; 13 | /// Tokens for the parser. 14 | pub mod tokens; 15 | -------------------------------------------------------------------------------- /crates/jql-parser/src/parser.rs: -------------------------------------------------------------------------------- 1 | use winnow::{ 2 | Parser, 3 | Result, 4 | combinator::{ 5 | alt, 6 | dispatch, 7 | fail, 8 | iterator, 9 | peek, 10 | }, 11 | token::any, 12 | }; 13 | 14 | use crate::{ 15 | combinators::{ 16 | parse_array_index, 17 | parse_array_range, 18 | parse_flatten_operator, 19 | parse_group_separator, 20 | parse_key, 21 | parse_keys_operator, 22 | parse_lenses, 23 | parse_multi_key, 24 | parse_object_index, 25 | parse_object_range, 26 | parse_pipe_in_operator, 27 | parse_pipe_out_operator, 28 | parse_truncate_operator, 29 | trim, 30 | }, 31 | errors::JqlParserError, 32 | tokens::{ 33 | Lens, 34 | Range, 35 | Token, 36 | View, 37 | }, 38 | }; 39 | 40 | /// Parses the provided input and map it to the first matching token. 41 | fn parse_fragment<'a>(input: &mut &'a str) -> Result> { 42 | trim( 43 | dispatch! {peek(any); 44 | '[' => { 45 | alt(( 46 | parse_array_index.map(Token::ArrayIndexSelector), 47 | parse_array_range.map(|(start, end)| Token::ArrayRangeSelector(Range(start, end))), 48 | )) 49 | }, 50 | '"' => parse_key.map(Token::KeySelector), 51 | '{' => { 52 | alt(( 53 | parse_multi_key.map(Token::MultiKeySelector), 54 | parse_object_index.map(Token::ObjectIndexSelector), 55 | parse_object_range.map(|(start, end)| Token::ObjectRangeSelector(Range(start, end))), 56 | )) 57 | }, 58 | '|' => { 59 | alt(( 60 | parse_lenses.map(|lenses| { 61 | Token::LensSelector( 62 | lenses 63 | .into_iter() 64 | .map(|(tokens, value)| Lens(tokens, value)) 65 | .collect(), 66 | ) 67 | }), 68 | parse_pipe_in_operator.value(Token::PipeInOperator), 69 | )) 70 | }, 71 | '@' => parse_keys_operator.value(Token::KeyOperator), 72 | '.' => parse_flatten_operator.value(Token::FlattenOperator), 73 | '<' => parse_pipe_out_operator.value(Token::PipeOutOperator), 74 | ',' => parse_group_separator.value(Token::GroupSeparator), 75 | '!' => parse_truncate_operator.value(Token::TruncateOperator), 76 | _ => fail 77 | } 78 | ) 79 | .parse_next(input) 80 | } 81 | 82 | /// Parses the provided input and returns a vector of `Tokens`. 83 | /// 84 | /// # Errors 85 | /// 86 | /// Returns a `JqlParserError` on failure. 87 | pub fn parse(input: &str) -> Result, JqlParserError> { 88 | let mut parser_iterator = iterator(input, parse_fragment); 89 | let tokens = parser_iterator.collect::>(); 90 | let result: Result<_, _> = parser_iterator.finish(); 91 | 92 | match result { 93 | Ok((unparsed, ())) => { 94 | if !unparsed.is_empty() { 95 | return Err(JqlParserError::ParsingError { 96 | tokens: tokens.stringify(), 97 | unparsed: unparsed.to_string(), 98 | }); 99 | } 100 | 101 | let truncate_count = tokens 102 | .iter() 103 | .filter(|&token| *token == Token::TruncateOperator) 104 | .count(); 105 | 106 | if truncate_count > 1 107 | || (truncate_count == 1 && tokens.last() != Some(&Token::TruncateOperator)) 108 | { 109 | return Err(JqlParserError::TruncateError(tokens.stringify())); 110 | } 111 | 112 | Ok(tokens) 113 | } 114 | Err(_) => Err(JqlParserError::UnknownError), 115 | } 116 | } 117 | 118 | #[cfg(test)] 119 | mod tests { 120 | use super::{ 121 | parse, 122 | parse_fragment, 123 | }; 124 | use crate::{ 125 | errors::JqlParserError, 126 | tokens::{ 127 | Index, 128 | Lens, 129 | LensValue, 130 | Range, 131 | Token, 132 | View, 133 | }, 134 | }; 135 | 136 | #[test] 137 | fn check_array_index_selector() { 138 | assert_eq!( 139 | parse_fragment(&mut "[0,1,2]"), 140 | Ok(Token::ArrayIndexSelector(vec![ 141 | Index(0), 142 | Index(1), 143 | Index(2) 144 | ])) 145 | ); 146 | assert_eq!( 147 | parse_fragment(&mut " [ 0 , 1 , 2 ] "), 148 | Ok(Token::ArrayIndexSelector(vec![ 149 | Index(0), 150 | Index(1), 151 | Index(2) 152 | ])) 153 | ); 154 | } 155 | 156 | #[test] 157 | fn check_array_range_selector() { 158 | assert_eq!( 159 | parse_fragment(&mut "[0:2]"), 160 | Ok(Token::ArrayRangeSelector(Range( 161 | Some(Index(0)), 162 | Some(Index(2)) 163 | ))) 164 | ); 165 | assert_eq!( 166 | parse_fragment(&mut "[:2]"), 167 | Ok(Token::ArrayRangeSelector(Range(None, Some(Index(2))))) 168 | ); 169 | assert_eq!( 170 | parse_fragment(&mut "[0:]"), 171 | Ok(Token::ArrayRangeSelector(Range(Some(Index(0)), None))) 172 | ); 173 | assert_eq!( 174 | parse_fragment(&mut "[:]"), 175 | Ok(Token::ArrayRangeSelector(Range(None, None))) 176 | ); 177 | } 178 | 179 | #[test] 180 | fn check_key_selector() { 181 | assert_eq!( 182 | parse_fragment(&mut r#""one""#), 183 | Ok(Token::KeySelector("one")) 184 | ); 185 | assert_eq!( 186 | parse_fragment(&mut r#" "one" "#), 187 | Ok(Token::KeySelector("one")) 188 | ); 189 | } 190 | 191 | #[test] 192 | fn check_multi_key_selector() { 193 | assert_eq!( 194 | parse_fragment(&mut r#"{"one","two","three"}"#), 195 | Ok(Token::MultiKeySelector(vec!["one", "two", "three"])) 196 | ); 197 | assert_eq!( 198 | parse_fragment(&mut r#" { "one", "two" , "three" } "#), 199 | Ok(Token::MultiKeySelector(vec!["one", "two", "three"])) 200 | ); 201 | } 202 | 203 | #[test] 204 | fn check_object_index_selector() { 205 | assert_eq!( 206 | parse_fragment(&mut "{0,1,2}"), 207 | Ok(Token::ObjectIndexSelector(vec![ 208 | Index(0), 209 | Index(1), 210 | Index(2) 211 | ])) 212 | ); 213 | assert_eq!( 214 | parse_fragment(&mut " { 0 , 1 , 2 } "), 215 | Ok(Token::ObjectIndexSelector(vec![ 216 | Index(0), 217 | Index(1), 218 | Index(2) 219 | ])) 220 | ); 221 | } 222 | 223 | #[test] 224 | fn check_object_range_selector() { 225 | assert_eq!( 226 | parse_fragment(&mut "{0:2}"), 227 | Ok(Token::ObjectRangeSelector(Range( 228 | Some(Index(0)), 229 | Some(Index(2)) 230 | ))) 231 | ); 232 | assert_eq!( 233 | parse_fragment(&mut "{:2}"), 234 | Ok(Token::ObjectRangeSelector(Range(None, Some(Index(2))))) 235 | ); 236 | assert_eq!( 237 | parse_fragment(&mut "{0:}"), 238 | Ok(Token::ObjectRangeSelector(Range(Some(Index(0)), None))) 239 | ); 240 | assert_eq!( 241 | parse_fragment(&mut "{:}"), 242 | Ok(Token::ObjectRangeSelector(Range(None, None))) 243 | ); 244 | } 245 | 246 | #[test] 247 | fn check_lens_selector() { 248 | assert_eq!( 249 | parse_fragment(&mut r#"|={"abc""c","bcd""d"=123,"efg"=null,"hij"="test"}"#), 250 | Ok(Token::LensSelector(vec![ 251 | Lens( 252 | vec![Token::KeySelector("abc"), Token::KeySelector("c")], 253 | None 254 | ), 255 | Lens( 256 | vec![Token::KeySelector("bcd"), Token::KeySelector("d")], 257 | Some(LensValue::Number(123)) 258 | ), 259 | Lens(vec![Token::KeySelector("efg")], Some(LensValue::Null)), 260 | Lens( 261 | vec![Token::KeySelector("hij")], 262 | Some(LensValue::String("test")) 263 | ), 264 | ])) 265 | ); 266 | } 267 | 268 | #[test] 269 | fn check_flatten_operator() { 270 | assert_eq!(parse_fragment(&mut ".."), Ok(Token::FlattenOperator)); 271 | assert_eq!(parse_fragment(&mut " .. "), Ok(Token::FlattenOperator)); 272 | } 273 | 274 | #[test] 275 | fn check_pipe_in_operator() { 276 | assert_eq!(parse_fragment(&mut "|>"), Ok(Token::PipeInOperator)); 277 | assert_eq!(parse_fragment(&mut " |> "), Ok(Token::PipeInOperator)); 278 | } 279 | 280 | #[test] 281 | fn check_pipe_out_operator() { 282 | assert_eq!(parse_fragment(&mut "<|"), Ok(Token::PipeOutOperator)); 283 | assert_eq!(parse_fragment(&mut " <| "), Ok(Token::PipeOutOperator)); 284 | } 285 | 286 | #[test] 287 | fn check_truncate_operator() { 288 | assert_eq!(parse_fragment(&mut "!"), Ok(Token::TruncateOperator)); 289 | assert_eq!(parse_fragment(&mut " ! "), Ok(Token::TruncateOperator)); 290 | } 291 | 292 | #[test] 293 | fn check_group_separator() { 294 | assert_eq!(parse_fragment(&mut ","), Ok(Token::GroupSeparator)); 295 | assert_eq!(parse_fragment(&mut " , "), Ok(Token::GroupSeparator)); 296 | } 297 | 298 | #[test] 299 | fn check_full_parser() { 300 | assert_eq!( 301 | parse(r#""this"[9,0]"#), 302 | Ok(vec![ 303 | Token::KeySelector("this"), 304 | Token::ArrayIndexSelector(vec![Index(9), Index(0)]) 305 | ]), 306 | ); 307 | assert_eq!( 308 | parse("[9,0]nope"), 309 | Err(JqlParserError::ParsingError { 310 | tokens: [Token::ArrayIndexSelector(vec![Index(9), Index(0)])].stringify(), 311 | unparsed: "nope".to_string(), 312 | }) 313 | ); 314 | assert_eq!( 315 | parse(r#""this"[9,0]|>"some"<|"ok"..!"#), 316 | Ok(vec![ 317 | Token::KeySelector("this"), 318 | Token::ArrayIndexSelector(vec![Index(9), Index(0)]), 319 | Token::PipeInOperator, 320 | Token::KeySelector("some"), 321 | Token::PipeOutOperator, 322 | Token::KeySelector("ok"), 323 | Token::FlattenOperator, 324 | Token::TruncateOperator 325 | ]), 326 | ); 327 | assert_eq!( 328 | parse(r#""a"!"b""#), 329 | Err(JqlParserError::TruncateError( 330 | [ 331 | Token::KeySelector("a"), 332 | Token::TruncateOperator, 333 | Token::KeySelector("b") 334 | ] 335 | .stringify() 336 | )) 337 | ); 338 | } 339 | } 340 | -------------------------------------------------------------------------------- /crates/jql-parser/src/tokens.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | fmt, 3 | num::{ 4 | NonZeroUsize, 5 | ParseIntError, 6 | }, 7 | str::FromStr, 8 | string::ToString, 9 | }; 10 | 11 | /// `Index` used for arrays and objects. 12 | /// Internally mapped to a `usize` with the newtype pattern. 13 | #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] 14 | pub struct Index(pub(crate) usize); 15 | 16 | impl Index { 17 | #[must_use] 18 | /// Creates a new `Index`. 19 | pub fn new(index: usize) -> Index { 20 | Index(index) 21 | } 22 | } 23 | 24 | impl From for usize { 25 | fn from(index: Index) -> usize { 26 | index.0 27 | } 28 | } 29 | 30 | impl fmt::Display for Index { 31 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 32 | write!(f, "Index ({})", self.0) 33 | } 34 | } 35 | 36 | impl FromStr for Index { 37 | type Err = ParseIntError; 38 | 39 | fn from_str(s: &str) -> Result { 40 | Ok(Index(s.parse::()?)) 41 | } 42 | } 43 | 44 | /// `Range` used for arrays and objects. 45 | /// Internally mapped to a tuple of `Option` of `Index`. 46 | #[derive(Debug, Clone, PartialEq, Eq)] 47 | pub struct Range(pub(crate) Option, pub(crate) Option); 48 | 49 | impl Range { 50 | #[must_use] 51 | /// Creates a new `Range`. 52 | pub fn new(start: Option, end: Option) -> Range { 53 | Range(start, end) 54 | } 55 | 56 | #[must_use] 57 | /// Maps a `Range` to a tuple of boundaries as `usize`. 58 | /// `start` defaults to 0 if `None`. 59 | /// `end` is injected based on `len` if `None`. 60 | pub fn to_boundaries(&self, len: NonZeroUsize) -> (usize, usize) { 61 | let start = self.0.unwrap_or(Index(0)); 62 | let end = self.1.unwrap_or(Index(len.get() - 1)); 63 | 64 | (start.0, end.0) 65 | } 66 | } 67 | 68 | impl fmt::Display for Range { 69 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 70 | let format_bound = |bound: &Option| match bound { 71 | Some(index) => index.to_string(), 72 | None => String::new(), 73 | }; 74 | 75 | write!( 76 | f, 77 | "Range [{}:{}]", 78 | format_bound(&self.0), 79 | format_bound(&self.1) 80 | ) 81 | } 82 | } 83 | 84 | /// `Lens` used for `LensSelector`. 85 | /// Internally mapped to a tuple of a slice of `Token` and `Option` of 86 | /// `LensValue`. 87 | #[derive(Debug, Clone, PartialEq, Eq)] 88 | pub struct Lens<'a>(pub(crate) Vec>, pub(crate) Option>); 89 | 90 | impl<'a> Lens<'a> { 91 | #[must_use] 92 | /// Creates a new `Lens`. 93 | pub fn new(tokens: &[Token<'a>], value: Option>) -> Lens<'a> { 94 | Lens(tokens.to_vec(), value) 95 | } 96 | 97 | #[must_use] 98 | /// Gets the content of a `Lens`. 99 | pub fn get(&self) -> (Vec>, Option>) { 100 | (self.0.clone(), self.1.clone()) 101 | } 102 | } 103 | 104 | impl fmt::Display for Lens<'_> { 105 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 106 | write!( 107 | f, 108 | "{}{}", 109 | self.0.stringify(), 110 | match &self.1 { 111 | Some(lens_value) => { 112 | lens_value.to_string() 113 | } 114 | None => String::new(), 115 | } 116 | ) 117 | } 118 | } 119 | 120 | /// Lens value type. 121 | #[derive(Debug, Clone, PartialEq, Eq)] 122 | pub enum LensValue<'a> { 123 | /// Variant for a JSON boolean. 124 | Bool(bool), 125 | /// Variant for JSON null. 126 | Null, 127 | /// Variant for a JSON number. 128 | Number(usize), 129 | /// Variant for a JSON string. 130 | String(&'a str), 131 | } 132 | 133 | impl fmt::Display for LensValue<'_> { 134 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 135 | match self { 136 | LensValue::Bool(boolean) => { 137 | write!(f, "{boolean}") 138 | } 139 | LensValue::Null => { 140 | write!(f, "null") 141 | } 142 | LensValue::Number(number) => { 143 | write!(f, "{number}") 144 | } 145 | LensValue::String(string) => write!(f, "{string}"), 146 | } 147 | } 148 | } 149 | 150 | /// Parser tokens type. 151 | #[derive(Debug, Clone, PartialEq, Eq)] 152 | pub enum Token<'a> { 153 | /// Array index selector. 154 | ArrayIndexSelector(Vec), 155 | /// Array range selector. 156 | ArrayRangeSelector(Range), 157 | /// Flatten operator. 158 | FlattenOperator, 159 | /// Key operator. 160 | KeyOperator, 161 | /// Group separator. 162 | GroupSeparator, 163 | /// Key selector. 164 | KeySelector(&'a str), 165 | /// Lens selector. 166 | LensSelector(Vec>), 167 | /// Multi key selector. 168 | MultiKeySelector(Vec<&'a str>), 169 | /// Object index selector. 170 | ObjectIndexSelector(Vec), 171 | /// Object range selector. 172 | ObjectRangeSelector(Range), 173 | /// Pipe in operator. 174 | PipeInOperator, 175 | /// Pipe out operator. 176 | PipeOutOperator, 177 | /// Truncate operator. 178 | TruncateOperator, 179 | } 180 | 181 | impl<'a> Token<'a> { 182 | fn get_name(&self) -> &'a str { 183 | match self { 184 | Token::ArrayIndexSelector(_) => "Array Index Selector", 185 | Token::ArrayRangeSelector(_) => "Array Range Selector", 186 | Token::FlattenOperator => "Flatten Operator", 187 | Token::KeyOperator => "Key Operator", 188 | Token::GroupSeparator => "Group Separator", 189 | Token::KeySelector(_) => "Key Selector", 190 | Token::LensSelector(_) => "Lens Selector", 191 | Token::MultiKeySelector(_) => "Multi Key Selector", 192 | Token::ObjectIndexSelector(_) => "Object Index Selector", 193 | Token::ObjectRangeSelector(_) => "Object Range Selector", 194 | Token::PipeInOperator => "Pipe In Operator", 195 | Token::PipeOutOperator => "Pipe Out Operator", 196 | Token::TruncateOperator => "Truncate Operator", 197 | } 198 | } 199 | } 200 | 201 | impl fmt::Display for Token<'_> { 202 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 203 | match self { 204 | Token::ArrayIndexSelector(indexes) | Token::ObjectIndexSelector(indexes) => { 205 | let formatted_indexes = indexes 206 | .iter() 207 | .map(ToString::to_string) 208 | .collect::>() 209 | .join(", "); 210 | 211 | write!(f, "{} [{formatted_indexes}]", self.get_name()) 212 | } 213 | Token::ArrayRangeSelector(range) | Token::ObjectRangeSelector(range) => { 214 | write!(f, "{} {}", self.get_name(), range) 215 | } 216 | Token::KeySelector(key) => { 217 | write!(f, r#"{} "{key}""#, self.get_name()) 218 | } 219 | Token::LensSelector(lenses) => { 220 | let formatted_indexes = lenses 221 | .iter() 222 | .map(ToString::to_string) 223 | .collect::>() 224 | .join(", "); 225 | 226 | write!(f, "{} [{formatted_indexes}]", self.get_name()) 227 | } 228 | Token::MultiKeySelector(multi_key) => { 229 | let formatted_keys = multi_key.join(", "); 230 | 231 | write!(f, "{} {formatted_keys}", self.get_name()) 232 | } 233 | Token::FlattenOperator 234 | | Token::KeyOperator 235 | | Token::GroupSeparator 236 | | Token::PipeInOperator 237 | | Token::PipeOutOperator 238 | | Token::TruncateOperator => { 239 | write!(f, "{}", self.get_name()) 240 | } 241 | } 242 | } 243 | } 244 | 245 | /// Trait used to expose custom display methods. 246 | pub trait View { 247 | /// Returns a stringified version of `self`. 248 | fn stringify(&self) -> String; 249 | } 250 | 251 | impl<'a, T: AsRef<[Token<'a>]>> View for T { 252 | fn stringify(&self) -> String { 253 | self.as_ref() 254 | .iter() 255 | .map(ToString::to_string) 256 | .collect::>() 257 | .join(", ") 258 | } 259 | } 260 | -------------------------------------------------------------------------------- /crates/jql-parser/tests/integration.rs: -------------------------------------------------------------------------------- 1 | use jql_parser::{ 2 | parser::parse, 3 | tokens::{ 4 | Index, 5 | Token, 6 | }, 7 | }; 8 | 9 | #[test] 10 | fn check_parse_integration() { 11 | assert_eq!( 12 | parse(r#""this"[9,0]|>"some"<|"ok"..!"#), 13 | Ok(vec![ 14 | Token::KeySelector("this"), 15 | Token::ArrayIndexSelector(vec![Index::new(9), Index::new(0)]), 16 | Token::PipeInOperator, 17 | Token::KeySelector("some"), 18 | Token::PipeOutOperator, 19 | Token::KeySelector("ok"), 20 | Token::FlattenOperator, 21 | Token::TruncateOperator 22 | ]), 23 | ); 24 | } 25 | -------------------------------------------------------------------------------- /crates/jql-runner/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | authors.workspace = true 3 | categories.workspace = true 4 | description = "Runner for jql - the JSON Query Language tool." 5 | edition.workspace = true 6 | keywords.workspace = true 7 | license.workspace = true 8 | name = "jql-runner" 9 | readme.workspace = true 10 | repository.workspace = true 11 | version.workspace = true 12 | 13 | [dependencies] 14 | indexmap = { version = "2.9.0", features = ["rayon"] } 15 | jql-parser = { path = "../jql-parser", version = "8.0.6" } 16 | rayon = "1.10.0" 17 | serde_json.workspace = true 18 | thiserror.workspace = true 19 | 20 | [dev-dependencies] 21 | criterion = "0.5.1" 22 | 23 | [lib] 24 | path = "src/lib.rs" 25 | 26 | [[bench]] 27 | harness = false 28 | name = "benchmark" 29 | path = "benches/benchmark.rs" 30 | -------------------------------------------------------------------------------- /crates/jql-runner/LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | ../../LICENSE-APACHE -------------------------------------------------------------------------------- /crates/jql-runner/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | ../../LICENSE-MIT -------------------------------------------------------------------------------- /crates/jql-runner/README.md: -------------------------------------------------------------------------------- 1 | # jql-runner 2 | 3 | ## About 4 | 5 | This crate is a workspace dependency of the [jql](https://github.com/yamafaktory/jql) tool. 6 | 7 | ## Features 8 | 9 | - Raw runner (string slice as input) 10 | - Token runner (tokens as input) 11 | - Errors 12 | 13 | ## License 14 | 15 | For licensing information, please check the [workspace root](https://github.com/yamafaktory/jql). 16 | -------------------------------------------------------------------------------- /crates/jql-runner/benches/benchmark.rs: -------------------------------------------------------------------------------- 1 | use criterion::{ 2 | Criterion, 3 | criterion_group, 4 | criterion_main, 5 | }; 6 | use jql_runner::runner::raw; 7 | use serde_json::json; 8 | 9 | fn array_range_selector(c: &mut Criterion) { 10 | c.bench_function("Array range selector", move |b| { 11 | b.iter(|| raw("[2,0]", &json!([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))) 12 | }); 13 | } 14 | 15 | fn flatten_operator(c: &mut Criterion) { 16 | c.bench_function("Flatten operator", move |b| { 17 | b.iter(|| raw("..", &json!([[[[[[[0]]], 1, [[[[2]]]], 3]]]]))) 18 | }); 19 | } 20 | 21 | fn group_separator(c: &mut Criterion) { 22 | c.bench_function("Group separator", move |b| { 23 | b.iter(|| raw(r#""a","b","c""#, &json!({ "a": 1, "b": 2, "c": 3 }))) 24 | }); 25 | } 26 | 27 | fn key_selector(c: &mut Criterion) { 28 | c.bench_function("Key selector", move |b| { 29 | b.iter(|| { 30 | raw( 31 | r#""props""a""b""c""#, 32 | &json!({ "props": { "a": { "b": { "c": 1} } } }), 33 | ) 34 | }) 35 | }); 36 | } 37 | 38 | fn pipe_operators(c: &mut Criterion) { 39 | c.bench_function("Pipe operators", move |b| { 40 | b.iter(|| { 41 | raw( 42 | r#""nested"|>"laptop""brand"<|[1]"#, 43 | &json!({ 44 | "nested": [ 45 | { 46 | "laptop": { 47 | "brand": "Apple" 48 | } 49 | }, 50 | { 51 | "laptop": { 52 | "brand": "Asus" 53 | } 54 | } 55 | ] 56 | }), 57 | ) 58 | }) 59 | }); 60 | } 61 | 62 | criterion_group!( 63 | benches, 64 | array_range_selector, 65 | flatten_operator, 66 | group_separator, 67 | key_selector, 68 | pipe_operators, 69 | ); 70 | 71 | criterion_main!(benches); 72 | -------------------------------------------------------------------------------- /crates/jql-runner/src/array.rs: -------------------------------------------------------------------------------- 1 | use std::num::NonZeroUsize; 2 | 3 | use jql_parser::tokens::{ 4 | Index, 5 | Lens, 6 | LensValue, 7 | Range, 8 | Token, 9 | }; 10 | use rayon::prelude::*; 11 | use serde_json::{ 12 | Value, 13 | json, 14 | }; 15 | 16 | use crate::{ 17 | errors::JqlRunnerError, 18 | runner::group_runner, 19 | }; 20 | 21 | /// Takes a mutable reference of JSON `Value` and returns a reference of a 22 | /// mutable vector of JSON `Value` or an error. 23 | fn as_array_mut(json: &mut Value) -> Result<&mut Vec, JqlRunnerError> { 24 | if json.is_array() { 25 | // We can safely unwrap here since this is an array. 26 | Ok(json.as_array_mut().unwrap()) 27 | } else { 28 | Err(JqlRunnerError::InvalidArrayError(json.clone())) 29 | } 30 | } 31 | 32 | /// Takes an `Index` and a JSON `Value`. 33 | /// Returns a reference of a JSON `Value` or an error. 34 | fn get_array_index(index: Index, json: &Value) -> Result { 35 | let num: usize = index.into(); 36 | 37 | if let Some(value) = json.get(num) { 38 | Ok(value.clone()) 39 | } else { 40 | Err(JqlRunnerError::IndexOutOfBoundsError { 41 | index: num, 42 | parent: json.clone(), 43 | }) 44 | } 45 | } 46 | 47 | /// Takes a slice of `Index` and a reference of a JSON `Value`. 48 | /// Returns a reference of a JSON `Value` or an error. 49 | pub(crate) fn get_array_indexes(indexes: &[Index], json: &Value) -> Result { 50 | if indexes.len() == 1 { 51 | return get_array_index(indexes[0], json); 52 | } 53 | 54 | let values: Vec = indexes 55 | .iter() 56 | .try_fold(vec![], |mut acc: Vec, index| { 57 | acc.push(get_array_index(*index, json)?); 58 | 59 | Ok::, JqlRunnerError>(acc) 60 | })?; 61 | 62 | Ok(json!(values)) 63 | } 64 | 65 | /// Takes a reference of a `Range` and a mutable reference of a JSON `Value`. 66 | /// Returns a JSON `Value` or an error. 67 | pub(crate) fn get_array_range(range: &Range, json: &mut Value) -> Result { 68 | let array = as_array_mut(json)?; 69 | 70 | if array.is_empty() { 71 | return Ok(json!([])); 72 | } 73 | 74 | let len = array.len(); 75 | // Array's length can't be zero so we can safely unwrap here. 76 | let non_zero_len = NonZeroUsize::new(len).unwrap(); 77 | let (start, end) = range.to_boundaries(non_zero_len); 78 | 79 | // Out of bounds. 80 | if start + 1 > len || end + 1 > len { 81 | return Err(JqlRunnerError::RangeOutOfBoundsError { 82 | start, 83 | end, 84 | parent: json.clone(), 85 | }); 86 | } 87 | 88 | let is_natural_order = start < end; 89 | 90 | let result = if is_natural_order { 91 | &mut array[start..=end] 92 | } else { 93 | &mut array[end..=start] 94 | }; 95 | 96 | if !is_natural_order { 97 | result.reverse(); 98 | } 99 | 100 | Ok(json!(result)) 101 | } 102 | 103 | /// Takes a reference of a JSON `Value`. 104 | /// Returns a flattened array as a JSON `Value` or an error. 105 | /// Note: the runner checks that the input is a JSON array. 106 | pub(crate) fn get_flattened_array(json: &Value) -> Result { 107 | let result = json 108 | .as_array() 109 | .unwrap() 110 | .par_iter() 111 | .try_fold_with(Vec::new(), |mut acc: Vec, inner_value| { 112 | if inner_value.is_array() { 113 | let mut flattened = get_flattened_array(inner_value)?; 114 | let result = as_array_mut(&mut flattened)?; 115 | 116 | acc.append(result); 117 | } else { 118 | acc.push(inner_value.clone()); 119 | } 120 | 121 | Ok::, JqlRunnerError>(acc) 122 | }) 123 | .try_reduce(Vec::new, |mut a, b| { 124 | a.extend(b); 125 | 126 | Ok(a) 127 | })?; 128 | 129 | Ok(json!(result)) 130 | } 131 | 132 | /// Takes a slice of `Lens` and a mutable reference of a JSON `Value`. 133 | /// Returns a JSON `Value` or an error. 134 | pub(crate) fn get_array_lenses(lenses: &[Lens], json: &mut Value) -> Result { 135 | let array = as_array_mut(json)?; 136 | 137 | if array.is_empty() { 138 | return Ok(json!([])); 139 | } 140 | 141 | let result = array 142 | .par_iter() 143 | .try_fold_with(Vec::new(), |mut acc: Vec, inner_value| { 144 | if lenses.iter().any(|lens| { 145 | let (tokens, value) = lens.get(); 146 | let tokens: Vec<&Token> = tokens.iter().collect(); 147 | let result = group_runner(&tokens, inner_value); 148 | 149 | if let Ok(current_value) = result { 150 | match value { 151 | Some(LensValue::Bool(boolean)) => { 152 | current_value.is_boolean() 153 | && current_value.as_bool().unwrap() == boolean 154 | } 155 | Some(LensValue::Null) => current_value.is_null(), 156 | Some(LensValue::Number(value)) => { 157 | current_value.is_u64() 158 | && current_value.as_u64().unwrap() == value as u64 159 | } 160 | Some(LensValue::String(value)) => current_value == value, 161 | None => true, 162 | } 163 | } else { 164 | false 165 | } 166 | }) { 167 | acc.push(inner_value.clone()); 168 | } 169 | 170 | Ok::, JqlRunnerError>(acc) 171 | }) 172 | .try_reduce(Vec::new, |mut a, b| { 173 | a.extend(b); 174 | 175 | Ok(a) 176 | })?; 177 | 178 | Ok(json!(result)) 179 | } 180 | 181 | /// Takes a reference of a JSON `Value`. 182 | /// Converts the original array as indexes and returns a JSON `Value` or an error. 183 | /// Note: the runner checks that the input is a JSON array. 184 | pub(crate) fn get_array_as_indexes(json: &Value) -> Result { 185 | let result = json 186 | .as_array() 187 | .unwrap() 188 | .par_iter() 189 | .enumerate() 190 | .try_fold_with(Vec::new(), |mut acc: Vec, (i, _)| { 191 | acc.push(i.into()); 192 | 193 | Ok::, JqlRunnerError>(acc) 194 | }) 195 | .try_reduce(Vec::new, |mut a, b| { 196 | a.extend(b); 197 | 198 | Ok(a) 199 | })?; 200 | 201 | Ok(json!(result)) 202 | } 203 | 204 | #[cfg(test)] 205 | mod tests { 206 | use jql_parser::tokens::{ 207 | Index, 208 | Lens, 209 | LensValue, 210 | Range, 211 | Token, 212 | }; 213 | use serde_json::json; 214 | 215 | use super::{ 216 | get_array_as_indexes, 217 | get_array_index, 218 | get_array_indexes, 219 | get_array_lenses, 220 | get_array_range, 221 | get_flattened_array, 222 | }; 223 | use crate::errors::JqlRunnerError; 224 | 225 | #[test] 226 | fn check_get_array_index() { 227 | let value = json!(["a", "b", "c"]); 228 | 229 | assert_eq!(get_array_index(Index::new(0), &value), Ok(json!("a"))); 230 | assert_eq!( 231 | get_array_index(Index::new(3), &value), 232 | Err(JqlRunnerError::IndexOutOfBoundsError { 233 | index: 3, 234 | parent: value 235 | }) 236 | ); 237 | } 238 | 239 | #[test] 240 | fn check_get_array_indexes() { 241 | let value = json!(["a", "b", "c"]); 242 | 243 | assert_eq!(get_array_indexes(&[Index::new(0)], &value), Ok(json!("a"))); 244 | assert_eq!( 245 | get_array_indexes(&[Index::new(0), Index::new(2)], &value), 246 | Ok(json!(["a", "c"])) 247 | ); 248 | assert_eq!( 249 | get_array_indexes(&[Index::new(0), Index::new(3)], &value), 250 | Err(JqlRunnerError::IndexOutOfBoundsError { 251 | index: 3, 252 | parent: value 253 | }) 254 | ); 255 | } 256 | 257 | #[test] 258 | fn check_get_array_as_indexes() { 259 | let value = json!(["a", "b", "c"]); 260 | 261 | assert_eq!(get_array_as_indexes(&value), Ok(json!([0, 1, 2]))); 262 | } 263 | 264 | #[test] 265 | fn check_get_array_range() { 266 | let value = json!(["a", "b", "c", "d", "e"]); 267 | 268 | assert_eq!( 269 | get_array_range( 270 | &Range::new(Some(Index::new(0)), Some(Index::new(2))), 271 | &mut json!([]) 272 | ), 273 | Ok(json!([])) 274 | ); 275 | assert_eq!( 276 | get_array_range( 277 | &Range::new(Some(Index::new(0)), Some(Index::new(2))), 278 | &mut value.clone() 279 | ), 280 | Ok(json!(["a", "b", "c"])) 281 | ); 282 | assert_eq!( 283 | get_array_range( 284 | &Range::new(Some(Index::new(2)), Some(Index::new(0))), 285 | &mut value.clone() 286 | ), 287 | Ok(json!(["c", "b", "a"])) 288 | ); 289 | assert_eq!( 290 | get_array_range( 291 | &Range::new(Some(Index::new(0)), Some(Index::new(0))), 292 | &mut value.clone() 293 | ), 294 | Ok(json!(["a"])) 295 | ); 296 | assert_eq!( 297 | get_array_range(&Range::new(None, Some(Index::new(4))), &mut value.clone()), 298 | Ok(json!(["a", "b", "c", "d", "e"])) 299 | ); 300 | assert_eq!( 301 | get_array_range(&Range::new(Some(Index::new(4)), None), &mut value.clone()), 302 | Ok(json!(["e"])) 303 | ); 304 | assert_eq!( 305 | get_array_range(&Range::new(None, Some(Index::new(5))), &mut value.clone()), 306 | Err(JqlRunnerError::RangeOutOfBoundsError { 307 | start: 0, 308 | end: 5, 309 | parent: value 310 | }) 311 | ); 312 | 313 | let value = json!(1); 314 | assert_eq!( 315 | get_array_range(&Range::new(None, Some(Index::new(5))), &mut value.clone()), 316 | Err(JqlRunnerError::InvalidArrayError(value)) 317 | ); 318 | } 319 | 320 | #[test] 321 | fn check_get_flattened_array() { 322 | assert_eq!( 323 | get_flattened_array(&json!([[[[[[[[[[[[[[1]]]]]]]]]]]]], [[[[[2]]]], 3], null])), 324 | Ok(json!([1, 2, 3, null])) 325 | ); 326 | assert_eq!( 327 | get_flattened_array( 328 | &json!([[[[[[[[[[[[[[{ "a": 1 }]]]]]]]]]]]]], [[[[[{ "b": 2 }]]]], { "c": 3 }], null]) 329 | ), 330 | Ok(json!([{ "a": 1 }, { "b": 2 }, { "c": 3 }, null])) 331 | ); 332 | } 333 | 334 | #[test] 335 | #[allow(clippy::too_many_lines)] 336 | fn check_get_array_lenses() { 337 | let mut value = json!([ 338 | { "a": 1, "b": 2 }, 339 | { "a": 2, "b": "some" }, 340 | { "a": 2, "b": null }, 341 | { "a": 2, "b": true }, 342 | ]); 343 | 344 | assert_eq!( 345 | get_array_lenses( 346 | &[Lens::new(&[Token::KeySelector("a")], None)], 347 | &mut json!([]) 348 | ), 349 | Ok(json!([])) 350 | ); 351 | assert_eq!( 352 | get_array_lenses( 353 | &[Lens::new(&[Token::KeySelector("a")], None)], 354 | &mut value.clone() 355 | ), 356 | Ok(json!([ 357 | { "a": 1, "b": 2 }, 358 | { "a": 2, "b": "some" }, 359 | { "a": 2, "b": null }, 360 | { "a": 2, "b": true }, 361 | ])) 362 | ); 363 | assert_eq!( 364 | get_array_lenses( 365 | &[Lens::new( 366 | &[Token::KeySelector("a")], 367 | Some(LensValue::Number(1)) 368 | )], 369 | &mut value.clone() 370 | ), 371 | Ok(json!([{ "a": 1, "b": 2 }])) 372 | ); 373 | assert_eq!( 374 | get_array_lenses( 375 | &[ 376 | Lens::new(&[Token::KeySelector("a")], Some(LensValue::Number(1))), 377 | Lens::new(&[Token::KeySelector("a")], Some(LensValue::Number(2))), 378 | ], 379 | &mut value.clone() 380 | ), 381 | Ok(json!([ 382 | { "a": 1, "b": 2 }, 383 | { "a": 2, "b": "some" }, 384 | { "a": 2, "b": null }, 385 | { "a": 2, "b": true }, 386 | ])) 387 | ); 388 | assert_eq!( 389 | get_array_lenses( 390 | &[ 391 | Lens::new(&[Token::KeySelector("a")], Some(LensValue::Number(1))), 392 | Lens::new(&[Token::KeySelector("b")], Some(LensValue::Number(2))), 393 | ], 394 | &mut value.clone() 395 | ), 396 | Ok(json!([ 397 | { "a": 1, "b": 2 }, 398 | ])) 399 | ); 400 | assert_eq!( 401 | get_array_lenses( 402 | &[ 403 | Lens::new(&[Token::KeySelector("a")], Some(LensValue::Number(1))), 404 | Lens::new(&[Token::KeySelector("b")], Some(LensValue::String("some"))), 405 | ], 406 | &mut value.clone() 407 | ), 408 | Ok(json!([ 409 | { "a": 1, "b": 2 }, 410 | { "a": 2, "b": "some" }, 411 | ])) 412 | ); 413 | assert_eq!( 414 | get_array_lenses( 415 | &[ 416 | Lens::new(&[Token::KeySelector("a")], Some(LensValue::Number(1))), 417 | Lens::new(&[Token::KeySelector("b")], Some(LensValue::Bool(true))), 418 | ], 419 | &mut value.clone() 420 | ), 421 | Ok(json!([ 422 | { "a": 1, "b": 2 }, 423 | { "a": 2, "b": true }, 424 | ])) 425 | ); 426 | assert_eq!( 427 | get_array_lenses( 428 | &[ 429 | Lens::new(&[Token::KeySelector("a")], Some(LensValue::Number(1))), 430 | Lens::new(&[Token::KeySelector("b")], Some(LensValue::Null)), 431 | ], 432 | &mut value 433 | ), 434 | Ok(json!([ 435 | { "a": 1, "b": 2 }, 436 | { "a": 2, "b": null }, 437 | ])) 438 | ); 439 | } 440 | } 441 | -------------------------------------------------------------------------------- /crates/jql-runner/src/errors.rs: -------------------------------------------------------------------------------- 1 | use jql_parser::errors::JqlParserError; 2 | use serde_json::Value; 3 | use thiserror::Error; 4 | 5 | static SLICE_SEP: &str = " ... "; 6 | static SLICE_LEN: usize = 7; 7 | static SEP: &str = ", "; 8 | 9 | /// Joins multiple `String`. 10 | fn join(values: &[String]) -> String { 11 | values.join(SEP) 12 | } 13 | 14 | /// Shortens a JSON `Value` for error injection. 15 | fn shorten(json: &Value) -> String { 16 | let full_json_string = json.to_string(); 17 | 18 | if full_json_string.len() < SLICE_LEN * 2 + SLICE_SEP.len() { 19 | return full_json_string; 20 | } 21 | 22 | let start_slice = &full_json_string[..SLICE_LEN]; 23 | let end_slice = &full_json_string[full_json_string.len() - SLICE_LEN..]; 24 | 25 | [start_slice, end_slice].join(SLICE_SEP) 26 | } 27 | 28 | /// Returns the type of a JSON `Value`. 29 | fn get_json_type(json: &Value) -> &str { 30 | match json { 31 | Value::Array(_) => "array", 32 | Value::Bool(_) => "boolean", 33 | Value::Null => "null", 34 | Value::Number(_) => "number", 35 | Value::Object(_) => "object", 36 | Value::String(_) => "string", 37 | } 38 | } 39 | 40 | /// Error type returned by the runner. 41 | #[derive(Debug, Error, PartialEq)] 42 | pub enum JqlRunnerError { 43 | /// Empty query error. 44 | #[error("Query is empty")] 45 | EmptyQueryError, 46 | 47 | /// Flatten error. 48 | #[error("Value {0} is neither an array nor an object and can't be flattened")] 49 | FlattenError(Value), 50 | 51 | /// Index out of bounds error. 52 | #[error("Index {index} in parent {parent} is out of bounds")] 53 | IndexOutOfBoundsError { 54 | /// Index. 55 | index: usize, 56 | /// Parent value. 57 | parent: Value, 58 | }, 59 | 60 | /// Invalid array error. 61 | #[error("Value {} is not a JSON array ({})", .0, get_json_type(.0))] 62 | InvalidArrayError(Value), 63 | 64 | /// Invalid object error. 65 | #[error("Value {} is not a JSON object ({})", .0, get_json_type(.0))] 66 | InvalidObjectError(Value), 67 | 68 | /// Key not found error. 69 | #[error(r#"Key "{key}" doesn't exist in parent {}"#, shorten(parent))] 70 | KeyNotFoundError { 71 | /// Key not found. 72 | key: String, 73 | /// Parent value. 74 | parent: Value, 75 | }, 76 | 77 | /// Keys not found error. 78 | #[error("Keys {} don't exist in parent {}", join(keys), shorten(parent))] 79 | MultiKeyNotFoundError { 80 | /// Keys not found. 81 | keys: Vec, 82 | /// Parent value. 83 | parent: Value, 84 | }, 85 | 86 | /// Parsing error. 87 | #[error(transparent)] 88 | ParsingError(#[from] JqlParserError), 89 | 90 | /// Pipe in error. 91 | #[error("Pipe in operator used on {0} which is not an array")] 92 | PipeInError(Value), 93 | 94 | /// Pipe in error. 95 | #[error("Pipe out operator used without a preceding pipe in operator")] 96 | PipeOutError, 97 | 98 | /// Range out of bounds error. 99 | #[error("Range [{start}:{end}] in parent {parent} is out of bounds")] 100 | RangeOutOfBoundsError { 101 | /// Start range. 102 | start: usize, 103 | /// End range. 104 | end: usize, 105 | /// parent value. 106 | parent: Value, 107 | }, 108 | 109 | /// Unknown error. 110 | #[error("Unknown error")] 111 | UnknownError, 112 | } 113 | 114 | #[cfg(test)] 115 | mod tests { 116 | 117 | use serde_json::json; 118 | 119 | use super::{ 120 | get_json_type, 121 | join, 122 | shorten, 123 | }; 124 | 125 | #[test] 126 | fn check_get_json_type() { 127 | assert_eq!(get_json_type(&json!([])), "array"); 128 | assert_eq!(get_json_type(&json!(true)), "boolean"); 129 | assert_eq!(get_json_type(&json!(null)), "null"); 130 | assert_eq!(get_json_type(&json!(1)), "number"); 131 | assert_eq!(get_json_type(&json!({})), "object"); 132 | assert_eq!(get_json_type(&json!("a")), "string"); 133 | } 134 | 135 | #[test] 136 | fn check_join() { 137 | assert_eq!( 138 | join(&["a".to_string(), "b".to_string(), "c".to_string()]), 139 | "a, b, c".to_string() 140 | ); 141 | } 142 | 143 | #[test] 144 | fn check_shorten() { 145 | assert_eq!(shorten(&json!("thismakesnosense")), r#""thismakesnosense""#); 146 | assert_eq!( 147 | shorten(&json!({ "a": { "b": { "c": [1, 2 ,3, 4, 5, 6, 7, 8, 9] } } })), 148 | r#"{"a":{" ... 8,9]}}}"#.to_string() 149 | ); 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /crates/jql-runner/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![doc = include_str!("../README.md")] 2 | #![doc( 3 | html_logo_url = "https://raw.githubusercontent.com/yamafaktory/jql/a438ab0039faf64c3329fb09bae399ae95601000/jql.svg" 4 | )] 5 | 6 | /// Array utilities. 7 | mod array; 8 | /// Parser errors. 9 | pub mod errors; 10 | /// Object utilities. 11 | mod object; 12 | /// Runner utilities. 13 | pub mod runner; 14 | -------------------------------------------------------------------------------- /crates/jql-runner/src/object.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | num::NonZeroUsize, 3 | string::ToString, 4 | }; 5 | 6 | use indexmap::{ 7 | IndexMap, 8 | IndexSet, 9 | }; 10 | use jql_parser::tokens::{ 11 | Index, 12 | Range, 13 | }; 14 | use rayon::prelude::*; 15 | use serde_json::{ 16 | Map, 17 | Value, 18 | json, 19 | }; 20 | 21 | use crate::errors::JqlRunnerError; 22 | 23 | /// Takes a reference of a JSON `Value` and returns a reference of a JSON `Map` 24 | /// or an error. 25 | fn as_object_mut(json: &mut Value) -> Result<&mut Map, JqlRunnerError> { 26 | if json.is_object() { 27 | // We can safely unwrap here since this is an object. 28 | Ok(json.as_object_mut().unwrap()) 29 | } else { 30 | Err(JqlRunnerError::InvalidObjectError(json.clone())) 31 | } 32 | } 33 | 34 | /// Takes a key as a string slice and a reference of a JSON `Value`. 35 | /// Returns a JSON `Value` or an error. 36 | pub(crate) fn get_object_key(key: &str, json: &Value) -> Result { 37 | if !json.is_object() { 38 | return Err(JqlRunnerError::InvalidObjectError(json.clone())); 39 | } 40 | 41 | json.get(key) 42 | .ok_or_else(|| JqlRunnerError::KeyNotFoundError { 43 | key: key.to_string(), 44 | parent: json.clone(), 45 | }) 46 | .cloned() 47 | } 48 | 49 | /// Takes a key as a string slice and a reference of a JSON `Value`. 50 | /// Returns a JSON `Value` or an error. 51 | pub(crate) fn get_object_multi_key( 52 | keys: &[&str], 53 | json: &mut Value, 54 | ) -> Result { 55 | let len = keys.len(); 56 | 57 | let (mut result, found_keys) = as_object_mut(json)? 58 | .iter_mut() 59 | .par_bridge() 60 | .try_fold_with( 61 | (IndexMap::with_capacity(len), IndexSet::with_capacity(len)), 62 | |mut acc: (IndexMap, IndexSet), (key, value)| { 63 | if let Some(index) = keys.iter().position(|s| s == key) { 64 | acc.0.insert(index, value.clone()); 65 | acc.1.insert(key.to_string()); 66 | } 67 | 68 | Ok::<(IndexMap, IndexSet), JqlRunnerError>(acc) 69 | }, 70 | ) 71 | .try_reduce( 72 | || (IndexMap::with_capacity(len), IndexSet::with_capacity(len)), 73 | |mut a, b| { 74 | a.0.extend(b.0); 75 | a.1.extend(b.1); 76 | 77 | Ok(a) 78 | }, 79 | )?; 80 | 81 | let keys_set: IndexSet = keys.iter().map(ToString::to_string).collect(); 82 | let mut keys_not_found: Vec = found_keys 83 | .symmetric_difference(&keys_set) 84 | .map(ToString::to_string) 85 | .collect(); 86 | 87 | if !keys_not_found.is_empty() { 88 | keys_not_found.sort(); 89 | return Err(JqlRunnerError::MultiKeyNotFoundError { 90 | keys: keys_not_found, 91 | parent: json.clone(), 92 | }); 93 | } 94 | 95 | // Restore the original order. 96 | result.par_sort_keys(); 97 | 98 | let new_map = result 99 | .into_iter() 100 | .fold(Map::with_capacity(len), |mut acc, (index, value)| { 101 | acc.insert(keys[index].to_owned(), value); 102 | 103 | acc 104 | }); 105 | 106 | Ok(json!(new_map)) 107 | } 108 | 109 | /// Takes a mutable reference of a JSON `Value`. 110 | /// Returns a flattened object as a JSON `Value`. 111 | pub(crate) fn get_flattened_object(json: &Value) -> Value { 112 | let mut flattened = Map::::new(); 113 | 114 | flatten_value(json, String::new(), 0, &mut flattened); 115 | 116 | json!(flattened) 117 | } 118 | 119 | /// Internal utility for `flatten_json_object`. 120 | fn flatten_value( 121 | json: &Value, 122 | parent_key: String, 123 | depth: usize, 124 | flattened: &mut Map, 125 | ) { 126 | if let Some(value) = json.as_object() { 127 | flatten_object(value, &parent_key, depth, flattened); 128 | } else { 129 | flattened.insert(parent_key, json.clone()); 130 | } 131 | } 132 | 133 | /// Internal utility for `flatten_json_object`. 134 | fn flatten_object( 135 | map: &Map, 136 | parent_key: &str, 137 | depth: usize, 138 | flattened: &mut Map, 139 | ) { 140 | for (k, v) in map { 141 | let parent_key = if depth > 0 { 142 | format!("{}{}{}", parent_key, ".", k) 143 | } else { 144 | k.to_string() 145 | }; 146 | 147 | flatten_value(v, parent_key, depth + 1, flattened); 148 | } 149 | } 150 | 151 | /// Takes a slice of `Index` and a mutable reference of a JSON `Value`. 152 | /// Returns a reference of a JSON `Value` or an error. 153 | pub(crate) fn get_object_indexes( 154 | indexes: &[Index], 155 | json: &mut Value, 156 | ) -> Result { 157 | let mut_object = as_object_mut(json)?; 158 | 159 | if mut_object.is_empty() { 160 | return Ok(json!({})); 161 | } 162 | 163 | let len = indexes.len(); 164 | // We can safely unwrap since indexes can't be empty. 165 | let max: usize = (*indexes.iter().max().unwrap()).into(); 166 | 167 | if max + 1 > mut_object.len() { 168 | return Err(JqlRunnerError::IndexOutOfBoundsError { 169 | index: max, 170 | parent: json.clone(), 171 | }); 172 | } 173 | 174 | let mut result = mut_object 175 | .iter_mut() 176 | .enumerate() 177 | .par_bridge() 178 | .try_fold_with( 179 | IndexMap::with_capacity(len), 180 | |mut acc: IndexMap, (index, (key, value))| { 181 | if let Some(index) = indexes.iter().position(|i| { 182 | let num: usize = (*i).into(); 183 | 184 | num == index 185 | }) { 186 | acc.insert(index, (key.to_string(), value.clone())); 187 | } 188 | 189 | Ok::, JqlRunnerError>(acc) 190 | }, 191 | ) 192 | .try_reduce( 193 | || IndexMap::with_capacity(len), 194 | |mut a, b| { 195 | a.extend(b); 196 | 197 | Ok(a) 198 | }, 199 | )?; 200 | 201 | // Restore the original order. 202 | result.par_sort_keys(); 203 | 204 | let new_map = result 205 | .into_iter() 206 | .fold(Map::with_capacity(len), |mut acc, (_, (key, value))| { 207 | acc.insert(key, value); 208 | 209 | acc 210 | }); 211 | 212 | Ok(json!(new_map)) 213 | } 214 | 215 | /// Takes a reference of a `Range` and a mutable reference of a JSON `Value`. 216 | /// Returns a reference of a JSON `Value` or an error. 217 | pub(crate) fn get_object_range(range: &Range, json: &mut Value) -> Result { 218 | let mut_object = as_object_mut(json)?; 219 | 220 | if mut_object.is_empty() { 221 | return Ok(json!({})); 222 | } 223 | 224 | let len = mut_object.len(); 225 | // Object's length can't be zero so we can safely unwrap here. 226 | let non_zero_len = NonZeroUsize::new(len).unwrap(); 227 | let (start, end) = range.to_boundaries(non_zero_len); 228 | 229 | // Out of bounds. 230 | if start + 1 > len || end + 1 > len { 231 | return Err(JqlRunnerError::RangeOutOfBoundsError { 232 | start, 233 | end, 234 | parent: json.clone(), 235 | }); 236 | } 237 | 238 | let is_natural_order = start < end; 239 | 240 | let mut result = mut_object 241 | .iter_mut() 242 | .enumerate() 243 | .par_bridge() 244 | .try_fold_with( 245 | IndexMap::with_capacity(len), 246 | |mut acc: IndexMap, (index, (key, value))| { 247 | if (is_natural_order && index >= start && index <= end) 248 | || (!is_natural_order && index >= end && index <= start) 249 | { 250 | acc.insert(index, (key.to_string(), value.clone())); 251 | } 252 | 253 | Ok::, JqlRunnerError>(acc) 254 | }, 255 | ) 256 | .try_reduce( 257 | || IndexMap::with_capacity(len), 258 | |mut a, b| { 259 | a.extend(b); 260 | 261 | Ok(a) 262 | }, 263 | )?; 264 | 265 | // Restore the original order. 266 | result.par_sort_keys(); 267 | 268 | // Reverse if not in natural order. 269 | if !is_natural_order { 270 | result.reverse(); 271 | } 272 | 273 | let new_map = result 274 | .into_iter() 275 | .fold(Map::with_capacity(len), |mut acc, (_, (key, value))| { 276 | acc.insert(key, value); 277 | 278 | acc 279 | }); 280 | 281 | Ok(json!(new_map)) 282 | } 283 | 284 | /// Takes a mutable reference of a JSON `Value`. 285 | /// Converts the original object as an array of its keys and returns a JSON `Value` or an error. 286 | /// Note: the runner checks that the input is a JSON object. 287 | pub(crate) fn get_object_as_keys(json: &mut Value) -> Result { 288 | let mut_object = json.as_object_mut().unwrap(); 289 | let mut result = mut_object 290 | .iter_mut() 291 | .par_bridge() 292 | .try_fold_with(Vec::new(), |mut acc: Vec, (k, _)| { 293 | acc.push(json!(k)); 294 | 295 | Ok::, JqlRunnerError>(acc) 296 | }) 297 | .try_reduce(Vec::new, |mut a, b| { 298 | a.extend(b); 299 | 300 | Ok(a) 301 | })?; 302 | 303 | // Restore the original order. 304 | // We can safely unwrap here since the key is a string. 305 | result.par_sort_by_key(|v| String::from(v.as_str().unwrap())); 306 | 307 | Ok(json!(result)) 308 | } 309 | 310 | #[cfg(test)] 311 | mod tests { 312 | use jql_parser::tokens::{ 313 | Index, 314 | Range, 315 | }; 316 | use serde_json::{ 317 | Value, 318 | json, 319 | }; 320 | 321 | use super::{ 322 | get_flattened_object, 323 | get_object_as_keys, 324 | get_object_indexes, 325 | get_object_key, 326 | get_object_multi_key, 327 | get_object_range, 328 | }; 329 | use crate::errors::JqlRunnerError; 330 | 331 | /// If we perform a direct comparison between the processed value and 332 | /// the expected value from the `json!` macro, we might get a false 333 | /// positive since the order of the keys is not checked on equality. 334 | fn assert_string_eq(processed: Result, expected: Value) { 335 | let processed = processed.unwrap(); 336 | let processed = serde_json::to_string(&processed).unwrap(); 337 | let expected = serde_json::to_string(&expected).unwrap(); 338 | 339 | assert_eq!(processed, expected); 340 | } 341 | 342 | #[test] 343 | fn check_get_object_key() { 344 | let value = json!({ "a": 1 }); 345 | 346 | assert_eq!(get_object_key("a", &value), Ok(json!(1))); 347 | assert_eq!( 348 | get_object_key("b", &value), 349 | Err(JqlRunnerError::KeyNotFoundError { 350 | key: "b".to_string(), 351 | parent: value 352 | }) 353 | ); 354 | } 355 | 356 | #[test] 357 | fn check_get_object_multi_key() { 358 | let value = json!({ "a": 1, "b": 2, "c": 3, "d": 4, "e": 5 }); 359 | assert_eq!( 360 | get_object_multi_key(&["a", "b", "c"], &mut value.clone()), 361 | Ok(json!({ "a": 1, "b": 2, "c": 3 })) 362 | ); 363 | assert_string_eq( 364 | get_object_multi_key(&["c", "a", "b"], &mut value.clone()), 365 | json!({"c": 3, "a": 1, "b": 2}), 366 | ); 367 | assert_eq!( 368 | get_object_multi_key(&["w", "a", "t"], &mut value.clone()), 369 | Err(JqlRunnerError::MultiKeyNotFoundError { 370 | keys: vec!["t".to_string(), "w".to_string()], 371 | parent: value, 372 | }) 373 | ); 374 | 375 | let value = json!(1); 376 | assert_eq!( 377 | get_object_multi_key(&["a", "b", "c"], &mut value.clone()), 378 | Err(JqlRunnerError::InvalidObjectError(value)) 379 | ); 380 | } 381 | 382 | #[test] 383 | fn check_get_flattened_object() { 384 | assert_eq!( 385 | get_flattened_object( 386 | &json!({ "a": { "c": false }, "b": { "d": { "e": { "f": 1, "g": { "h": 2 }} } } }) 387 | ), 388 | json!({ 389 | "a.c": false, 390 | "b.d.e.f": 1, 391 | "b.d.e.g.h": 2 392 | }) 393 | ); 394 | } 395 | 396 | #[test] 397 | fn check_get_object_indexes() { 398 | let value = json!({ "a": 1, "b": 2, "c": 3, "d": 4, "e": 5 }); 399 | 400 | assert_string_eq( 401 | get_object_indexes( 402 | &[Index::new(4), Index::new(2), Index::new(0)], 403 | &mut value.clone(), 404 | ), 405 | json!({ "e": 5, "c": 3, "a": 1 }), 406 | ); 407 | assert_eq!( 408 | get_object_indexes( 409 | &[Index::new(4), Index::new(2), Index::new(10)], 410 | &mut value.clone() 411 | ), 412 | Err(JqlRunnerError::IndexOutOfBoundsError { 413 | index: 10, 414 | parent: value, 415 | }) 416 | ); 417 | } 418 | 419 | #[test] 420 | fn check_get_object_as_keys() { 421 | let value = json!({ "a": 1, "b": 2, "c": 3, "d": 4, "e": 5 }); 422 | 423 | assert_string_eq( 424 | get_object_as_keys(&mut value.clone()), 425 | json!(["a", "b", "c", "d", "e"]), 426 | ); 427 | } 428 | 429 | #[test] 430 | fn check_get_object_range() { 431 | let value = json!({ "a": 1, "b": 2, "c": 3, "d": 4, "e": 5 }); 432 | 433 | assert_eq!( 434 | get_object_range( 435 | &Range::new(Some(Index::new(0)), Some(Index::new(2))), 436 | &mut json!({}) 437 | ), 438 | Ok(json!({})) 439 | ); 440 | 441 | assert_string_eq( 442 | get_object_range( 443 | &Range::new(Some(Index::new(0)), Some(Index::new(2))), 444 | &mut value.clone(), 445 | ), 446 | json!({ "a": 1, "b": 2, "c": 3 }), 447 | ); 448 | assert_string_eq( 449 | get_object_range( 450 | &Range::new(Some(Index::new(2)), Some(Index::new(0))), 451 | &mut value.clone(), 452 | ), 453 | json!({ "c": 3, "b": 2, "a": 1 }), 454 | ); 455 | assert_eq!( 456 | get_object_range( 457 | &Range::new(Some(Index::new(0)), Some(Index::new(0))), 458 | &mut value.clone() 459 | ), 460 | Ok(json!({ "a": 1 })) 461 | ); 462 | assert_string_eq( 463 | get_object_range(&Range::new(None, Some(Index::new(4))), &mut value.clone()), 464 | json!({ "a": 1, "b": 2, "c": 3, "d": 4, "e": 5 }), 465 | ); 466 | assert_eq!( 467 | get_object_range(&Range::new(Some(Index::new(4)), None), &mut value.clone()), 468 | Ok(json!({ "e": 5 })) 469 | ); 470 | assert_eq!( 471 | get_object_range(&Range::new(None, Some(Index::new(5))), &mut value.clone()), 472 | Err(JqlRunnerError::RangeOutOfBoundsError { 473 | start: 0, 474 | end: 5, 475 | parent: value 476 | }) 477 | ); 478 | 479 | let value = json!(1); 480 | assert_eq!( 481 | get_object_range(&Range::new(None, Some(Index::new(5))), &mut value.clone()), 482 | Err(JqlRunnerError::InvalidObjectError(value)) 483 | ); 484 | } 485 | } 486 | -------------------------------------------------------------------------------- /crates/jql-runner/src/runner.rs: -------------------------------------------------------------------------------- 1 | use jql_parser::{ 2 | group::split, 3 | parser::parse, 4 | tokens::Token, 5 | }; 6 | use rayon::prelude::*; 7 | use serde_json::{ 8 | Value, 9 | json, 10 | }; 11 | 12 | use crate::{ 13 | array::{ 14 | get_array_as_indexes, 15 | get_array_indexes, 16 | get_array_lenses, 17 | get_array_range, 18 | get_flattened_array, 19 | }, 20 | errors::JqlRunnerError, 21 | object::{ 22 | get_flattened_object, 23 | get_object_as_keys, 24 | get_object_indexes, 25 | get_object_key, 26 | get_object_multi_key, 27 | get_object_range, 28 | }, 29 | }; 30 | 31 | /// Takes a raw input as a slice string to parse and a reference of a JSON 32 | /// `Value`. 33 | /// Returns a JSON `Value`. 34 | /// 35 | /// # Errors 36 | /// 37 | /// Returns a `JqlRunnerError` on failure. 38 | pub fn raw(input: &str, json: &Value) -> Result { 39 | if input.is_empty() { 40 | return Err(JqlRunnerError::EmptyQueryError); 41 | } 42 | 43 | let tokens = parse(input)?; 44 | 45 | token(&tokens, json) 46 | } 47 | 48 | /// Takes a slice of `Tokens` to parse and a reference of a JSON 49 | /// `Value`. 50 | /// Returns a JSON `Value`. 51 | /// 52 | /// # Errors 53 | /// 54 | /// Returns a `JqlRunnerError` on failure. 55 | pub fn token(tokens: &[Token], json: &Value) -> Result { 56 | let groups = split(tokens); 57 | 58 | let result = groups 59 | .par_iter() 60 | .try_fold_with(vec![], |mut acc: Vec, group| { 61 | acc.push(group_runner(group, json)?); 62 | 63 | Ok::, JqlRunnerError>(acc) 64 | }) 65 | .try_reduce(Vec::new, |mut a, b| { 66 | a.extend(b); 67 | 68 | Ok(a) 69 | }); 70 | 71 | result.map(|group| { 72 | if groups.len() == 1 { 73 | json!(group[0]) 74 | } else { 75 | json!(group) 76 | } 77 | }) 78 | } 79 | 80 | /// Takes a slice of references of `Token` and a reference of a JSON `Value`. 81 | /// Returns a JSON `Value` or an error. 82 | /// Note: the `GroupSeparator` enum variant is unreachable at this point since 83 | /// it has been filtered out by any of the public `runner` functions. 84 | pub(crate) fn group_runner(tokens: &[&Token], json: &Value) -> Result { 85 | tokens 86 | .iter() 87 | // At this level we can use rayon since every token is applied 88 | // sequentially. 89 | .try_fold((json.clone(), false), |mut outer_acc, &token| { 90 | if outer_acc.1 { 91 | let result = outer_acc 92 | .0 93 | .as_array_mut() 94 | // We can safely unwrap since `outer_acc.1` is truthy. 95 | .unwrap() 96 | .par_iter() 97 | .try_fold_with( 98 | (vec![], outer_acc.1), 99 | |mut inner_acc: (Vec, bool), inner_value| { 100 | let result = matcher((inner_value.clone(), outer_acc.1), token)?; 101 | 102 | inner_acc.0.push(result.0); 103 | inner_acc.1 = result.1; 104 | 105 | Ok::<(Vec, bool), JqlRunnerError>(inner_acc) 106 | }, 107 | ) 108 | .try_reduce( 109 | || (vec![], false), 110 | |mut a, b| { 111 | a.0.extend(b.0); 112 | 113 | Ok((a.0, b.1)) 114 | }, 115 | )?; 116 | 117 | Ok((json!(result.0), result.1)) 118 | } else { 119 | matcher(outer_acc, token) 120 | } 121 | }) 122 | // Drop the `pipe` boolean flag. 123 | .map(|(value, _)| value) 124 | } 125 | 126 | /// Internal matcher consumed by the `group_runner` to apply a selection based 127 | /// on the provided mutable JSON `Value` and the reference of a `Token`. 128 | /// A `piped` flag is used to keep track of the pipe operators. 129 | fn matcher( 130 | (mut acc, mut piped): (Value, bool), 131 | token: &Token, 132 | ) -> Result<(Value, bool), JqlRunnerError> { 133 | let result = match token { 134 | Token::ArrayIndexSelector(indexes) => get_array_indexes(indexes, &acc), 135 | Token::ArrayRangeSelector(range) => get_array_range(range, &mut acc), 136 | Token::FlattenOperator => match acc { 137 | Value::Array(_) => get_flattened_array(&acc), 138 | Value::Object(_) => Ok(get_flattened_object(&acc)), 139 | _ => Err(JqlRunnerError::FlattenError(acc)), 140 | }, 141 | Token::KeyOperator => match acc { 142 | Value::Array(_) => get_array_as_indexes(&acc), 143 | Value::Object(_) => get_object_as_keys(&mut acc), 144 | // Return the original value for Null, Bool, Number and String. 145 | Value::Bool(bool) => Ok(json!(bool)), 146 | Value::Number(number) => Ok(json!(number)), 147 | Value::String(string) => Ok(json!(string)), 148 | Value::Null => Ok(json!(null)), 149 | }, 150 | Token::GroupSeparator => unreachable!(), 151 | Token::KeySelector(key) => get_object_key(key, &acc), 152 | Token::LensSelector(lenses) => get_array_lenses(lenses, &mut acc), 153 | Token::MultiKeySelector(keys) => get_object_multi_key(keys, &mut acc), 154 | Token::ObjectIndexSelector(indexes) => get_object_indexes(indexes, &mut acc), 155 | Token::ObjectRangeSelector(range) => get_object_range(range, &mut acc), 156 | Token::PipeInOperator => { 157 | if !acc.is_array() { 158 | return Err(JqlRunnerError::PipeInError(acc)); 159 | } 160 | 161 | piped = true; 162 | 163 | Ok(acc) 164 | } 165 | Token::PipeOutOperator => { 166 | if !piped { 167 | return Err(JqlRunnerError::PipeOutError); 168 | } 169 | 170 | piped = false; 171 | 172 | Ok(acc) 173 | } 174 | Token::TruncateOperator => match acc { 175 | Value::Array(_) => Ok(json!([])), 176 | Value::Object(_) => Ok(json!({})), 177 | Value::Bool(_) | Value::Number(_) | Value::String(_) | Value::Null => Ok(acc), 178 | }, 179 | }; 180 | 181 | result.map(|value| (value, piped)) 182 | } 183 | 184 | #[cfg(test)] 185 | mod tests { 186 | use jql_parser::{ 187 | errors::JqlParserError, 188 | tokens::{ 189 | Token, 190 | View, 191 | }, 192 | }; 193 | use serde_json::json; 194 | 195 | use super::raw; 196 | use crate::errors::JqlRunnerError; 197 | 198 | #[test] 199 | fn check_runner_empty_input_error() { 200 | assert_eq!(raw("", &json!("")), Err(JqlRunnerError::EmptyQueryError)); 201 | } 202 | 203 | #[test] 204 | fn check_runner_parsing_error() { 205 | assert_eq!( 206 | raw(r#""a"b"#, &json!({ "a": 1 })), 207 | Err(JqlRunnerError::ParsingError(JqlParserError::ParsingError { 208 | tokens: [Token::KeySelector("a")].stringify(), 209 | unparsed: "b".to_string(), 210 | })) 211 | ); 212 | } 213 | 214 | #[test] 215 | fn check_runner_no_key_found_error() { 216 | let parent = json!({ "a": 1 }); 217 | 218 | assert_eq!( 219 | raw(r#""b""#, &parent), 220 | Err(JqlRunnerError::KeyNotFoundError { 221 | key: "b".to_string(), 222 | parent 223 | }) 224 | ); 225 | } 226 | 227 | #[test] 228 | fn check_runner_index_not_found_error() { 229 | let parent = json!(["a"]); 230 | 231 | assert_eq!( 232 | raw("[1]", &parent), 233 | Err(JqlRunnerError::IndexOutOfBoundsError { index: 1, parent }) 234 | ); 235 | } 236 | 237 | #[test] 238 | fn check_runner_success() { 239 | assert_eq!( 240 | raw(r#""a","b""#, &json!({ "a": 1, "b": 2 })), 241 | Ok(json!([1, 2])) 242 | ); 243 | assert_eq!(raw(r#""a""b""#, &json!({ "a": { "b": 2 } })), Ok(json!(2))); 244 | assert_eq!( 245 | raw("[4,2,0]", &json!(["a", "b", "c", "d", "e"])), 246 | Ok(json!(["e", "c", "a"])) 247 | ); 248 | } 249 | 250 | #[test] 251 | fn check_runner_pipes() { 252 | let value = json!({ "a": [{ "b": { "c": 1 } }, { "b": { "c": 2 }}]}); 253 | 254 | assert_eq!(raw(r#""a"|>"b""c"<|[1]"#, &value), Ok(json!(2))); 255 | } 256 | 257 | #[test] 258 | fn check_runner_truncate() { 259 | assert_eq!(raw(r#""a"!"#, &json!({ "a": [1, 2, 3] })), Ok(json!([]))); 260 | assert_eq!(raw(r#""a"!"#, &json!({ "a": { "b": 1 } })), Ok(json!({}))); 261 | assert_eq!(raw(r#""a"!"#, &json!({ "a": true })), Ok(json!(true))); 262 | assert_eq!(raw(r#""a"!"#, &json!({ "a": 1 })), Ok(json!(1))); 263 | assert_eq!(raw(r#""a"!"#, &json!({ "a": "b" })), Ok(json!("b"))); 264 | assert_eq!(raw(r#""a"!"#, &json!({ "a": null })), Ok(json!(null))); 265 | assert_eq!(raw("!", &json!({ "a": null })), Ok(json!({}))); 266 | assert_eq!( 267 | raw(r#""a"!"b""#, &json!({ "a": [1, 2, 3] })), 268 | Err(JqlRunnerError::ParsingError(JqlParserError::TruncateError( 269 | [ 270 | Token::KeySelector("a"), 271 | Token::TruncateOperator, 272 | Token::KeySelector("b") 273 | ] 274 | .stringify(), 275 | ))) 276 | ); 277 | } 278 | 279 | #[test] 280 | fn check_runner_lens() { 281 | let value = json!([ 282 | { "a": { "b": { "c": 1 }}}, 283 | { "a": { "b": { "c": 2 }}}, 284 | ]); 285 | 286 | assert_eq!( 287 | raw(r#"|={"a""b""c"=2}"#, &value), 288 | Ok(json!([ 289 | { "a": { "b": { "c": 2 }}} 290 | ])) 291 | ); 292 | } 293 | 294 | #[test] 295 | fn check_runner_keys() { 296 | let value = json!({ "a": { "b": { "c": { "d": 1 }}}}); 297 | 298 | assert_eq!(raw(r#""a""b""c"@"#, &value), Ok(json!(["d"]))); 299 | } 300 | } 301 | -------------------------------------------------------------------------------- /crates/jql-runner/tests/integration.rs: -------------------------------------------------------------------------------- 1 | use jql_parser::tokens::Token; 2 | use jql_runner::runner::{ 3 | raw, 4 | token, 5 | }; 6 | use serde_json::json; 7 | 8 | #[test] 9 | fn check_raw_integration() { 10 | assert_eq!( 11 | raw(r#""a","b""#, &json!({ "a": 1, "b": 2 })), 12 | Ok(json!([1, 2])) 13 | ); 14 | } 15 | 16 | #[test] 17 | fn check_token_integration() { 18 | assert_eq!( 19 | token( 20 | &[ 21 | Token::KeySelector("a"), 22 | Token::GroupSeparator, 23 | Token::KeySelector("b") 24 | ], 25 | &json!({ "a": 1, "b": 2 }) 26 | ), 27 | Ok(json!([1, 2])) 28 | ); 29 | } 30 | -------------------------------------------------------------------------------- /crates/jql/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | authors.workspace = true 3 | categories.workspace = true 4 | description = "jql - JSON Query Language - is a fast and simple command-line tool to manipulate JSON data." 5 | edition.workspace = true 6 | keywords.workspace = true 7 | license.workspace = true 8 | name = "jql" 9 | readme.workspace = true 10 | repository.workspace = true 11 | version.workspace = true 12 | 13 | [dependencies] 14 | anyhow = "1.0.98" 15 | clap = { version = "4.5.37", features = ["derive"] } 16 | colored_json = { version = "5.0.0" } 17 | jql-runner = { path = "../jql-runner", version = "8.0.6" } 18 | serde = "1.0.219" 19 | serde_stacker = "0.1.12" 20 | serde_json.workspace = true 21 | tokio = { version = "1.44.2", features = ["fs", "io-std", "io-util", "macros", "rt-multi-thread"] } 22 | 23 | [package.metadata.binstall] 24 | pkg-url = "{ repo }/releases/download/{ name }-v{ version }/{ name }-v{ version }-{ target }{ archive-suffix }" 25 | -------------------------------------------------------------------------------- /crates/jql/LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | ../../LICENSE-APACHE -------------------------------------------------------------------------------- /crates/jql/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | ../../LICENSE-MIT -------------------------------------------------------------------------------- /crates/jql/README.md: -------------------------------------------------------------------------------- 1 | # jql 2 | 3 | ## About 4 | 5 | This crate is the [jql](https://github.com/yamafaktory/jql) binary tool. 6 | 7 | ## License 8 | 9 | For licensing information, please check the [workspace root](https://github.com/yamafaktory/jql). 10 | -------------------------------------------------------------------------------- /crates/jql/src/args.rs: -------------------------------------------------------------------------------- 1 | use std::path::PathBuf; 2 | 3 | use clap::{ 4 | Parser, 5 | ValueHint, 6 | }; 7 | 8 | static QUERY_HELP: &str = r#" 9 | A query is sequence of tokens used to make a selection on a JSON input. 10 | 11 | A query must be enclosed by single quotation marks. 12 | 13 | The following tokens are available to build up a query: 14 | 15 | == Separators == 16 | 17 | Group separator , 18 | ┬ 19 | ╰→ query '"a","b","c"' will build up an array from sub-queries 20 | 21 | == Selectors == 22 | 23 | -- Arrays -- 24 | 25 | Array index selector [0,2,1] 26 | ┬ 27 | ╰→ indexes can be used in arbitrary order 28 | 29 | Array range selector [2:0] 30 | ┬ 31 | ╰→ range can be in natural order [0:2], reversed [2:0], 32 | without lower [:2] or upper bound [0:] 33 | 34 | Lens selector |={"a","b"=true,"c"=null,"d"=1,"e"="string"} 35 | ┬ 36 | ╰→ lens can be a combination of one or more selectors with an optional value, 37 | a value being any of boolean | null | number | string 38 | 39 | -- Objects -- 40 | 41 | Key selector "a" 42 | ┬ 43 | ╰→ any valid JSON key 44 | 45 | Multi key selector {"a","c","b"} 46 | ┬ 47 | ╰→ keys can be used in arbitrary order 48 | 49 | Object index selector {0,2,1} 50 | ┬ 51 | ╰→ indexes can be used in arbitrary order 52 | 53 | Object range selector {2:0} 54 | ┬ 55 | ╰→ range can be in natural order {0:2}, reversed {2:0}, 56 | without lower {:2} or upper bound {0:} 57 | 58 | == Operators == 59 | 60 | Flatten operator .. 61 | ┬ 62 | ╰→ flattens arrays and objects 63 | 64 | Keys operator @ 65 | ┬ 66 | ╰→ returns the keys of an object or the indexes of an array, 67 | other primitives are returned as is 68 | 69 | Pipe in operator |> 70 | ┬ 71 | ╰→ applies the next tokens in parallel on each element of an array 72 | 73 | Pipe out operator <| 74 | ┬ 75 | ╰→ stops the parallelization initiated by the pipe in operator 76 | 77 | Truncate operator ! 78 | ┬ 79 | ╰→ maps the output into simple JSON primitives 80 | boolean | null | number | string | [] | {} 81 | "#; 82 | 83 | #[allow(clippy::struct_excessive_bools)] 84 | #[derive(Debug, Parser)] 85 | #[command( 86 | about, 87 | author, 88 | long_about = None, 89 | version 90 | )] 91 | pub(crate) struct Args { 92 | /// Query argument. 93 | #[arg( 94 | conflicts_with = "validate", 95 | help = "Query to apply to the JSON data", 96 | index = 1, 97 | long_help = QUERY_HELP, 98 | required_unless_present_any = ["no-query"] 99 | )] 100 | pub(crate) query: Option, 101 | 102 | /// JSON file argument. 103 | #[arg( 104 | help = "JSON file to use", 105 | index = 2, 106 | value_hint = ValueHint::FilePath, 107 | value_name = "OPTIONAL_FILE" 108 | )] 109 | pub(crate) json_file: Option, 110 | 111 | /// Inline JSON flag. 112 | #[arg( 113 | conflicts_with = "validate", 114 | help = "Inline the JSON output", 115 | long = "inline", 116 | short = 'i' 117 | )] 118 | pub(crate) inline: bool, 119 | 120 | /// Query from file flag. 121 | #[arg( 122 | group = "no-query", 123 | help = "Read the query from file", 124 | long = "query", 125 | long_help = QUERY_HELP, 126 | short = 'q', 127 | value_hint = ValueHint::FilePath, 128 | value_name = "FILE" 129 | )] 130 | pub(crate) query_from_file: Option, 131 | 132 | /// Raw string flag. 133 | #[arg( 134 | help = "Write to stdout without JSON double-quotes (string only)", 135 | long = "raw-string", 136 | short = 'r' 137 | )] 138 | pub(crate) raw_string: bool, 139 | 140 | /// Stream flag. 141 | #[arg( 142 | help = "Read a stream of JSON data line by line", 143 | long = "stream", 144 | short = 's' 145 | )] 146 | pub(crate) stream: bool, 147 | 148 | /// Validate JSON data flag. 149 | #[arg( 150 | group = "no-query", 151 | help = "Validate the JSON data", 152 | long = "validate", 153 | short = 'v' 154 | )] 155 | pub(crate) validate: bool, 156 | } 157 | 158 | #[test] 159 | fn check_args() { 160 | use clap::CommandFactory; 161 | 162 | Args::command().debug_assert(); 163 | } 164 | -------------------------------------------------------------------------------- /crates/jql/src/errors.rs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yamafaktory/jql/2a98b2e3e6272c0007eacdd4f24aa2f952c0a8a3/crates/jql/src/errors.rs -------------------------------------------------------------------------------- /crates/jql/src/main.rs: -------------------------------------------------------------------------------- 1 | #![doc = include_str!("../README.md")] 2 | 3 | mod args; 4 | mod panic; 5 | 6 | use std::{ 7 | path::Path, 8 | process::exit, 9 | }; 10 | 11 | use anyhow::{ 12 | Context, 13 | Result, 14 | anyhow, 15 | }; 16 | use args::Args; 17 | use clap::Parser; 18 | use colored_json::{ 19 | ColoredFormatter, 20 | CompactFormatter, 21 | PrettyFormatter, 22 | }; 23 | use jql_runner::runner; 24 | use panic::use_custom_panic_hook; 25 | use serde::Deserialize; 26 | use serde_json::Value; 27 | use serde_stacker::Deserializer; 28 | use tokio::{ 29 | fs::File, 30 | io::{ 31 | AsyncBufReadExt, 32 | AsyncReadExt, 33 | AsyncWriteExt, 34 | BufReader, 35 | stdin, 36 | stdout, 37 | }, 38 | }; 39 | 40 | /// Reads a file from `path`. 41 | async fn read_file(path: impl AsRef) -> Result { 42 | let display_path = path.as_ref().display(); 43 | let mut file = File::open(&path) 44 | .await 45 | .with_context(|| format!("Failed to open file {display_path}"))?; 46 | let mut contents = vec![]; 47 | 48 | file.read_to_end(&mut contents) 49 | .await 50 | .with_context(|| format!("Failed to read from file {display_path}"))?; 51 | 52 | Ok(String::from_utf8_lossy(&contents).into_owned()) 53 | } 54 | 55 | /// Renders the output or the error and exits. 56 | fn render(result: Result) { 57 | match result { 58 | Ok(output) => println!("{output}"), 59 | Err(error) => { 60 | eprintln!("{error}"); 61 | exit(1); 62 | } 63 | } 64 | } 65 | 66 | /// Processes the JSON content based on the arguments. 67 | async fn process_json(json: &str, args: &Args) -> Result { 68 | if args.validate { 69 | return serde_json::from_str::(json).map_or_else( 70 | |_| Err(anyhow!("Invalid JSON file or content")), 71 | |_| Ok("Valid JSON file or content".to_string()), 72 | ); 73 | } 74 | 75 | let query = match args.query_from_file.as_deref() { 76 | Some(path) => read_file(path).await?, 77 | // We can safely unwrap since clap is taking care of the validation. 78 | None => args.query.as_deref().unwrap().to_string(), 79 | }; 80 | 81 | let mut deserializer = serde_json::Deserializer::from_str(json); 82 | 83 | deserializer.disable_recursion_limit(); 84 | 85 | let deserializer = Deserializer::new(&mut deserializer); 86 | let value: Value = Value::deserialize(deserializer) 87 | .with_context(|| "Failed to deserialize the JSON data".to_string())?; 88 | let result: Value = runner::raw(&query, &value)?; 89 | 90 | if args.inline { 91 | return ColoredFormatter::new(CompactFormatter {}) 92 | .to_colored_json_auto(&result) 93 | .with_context(|| "Failed to inline the JSON data".to_string()); 94 | } 95 | 96 | if args.raw_string && result.is_string() { 97 | // We can safely unwrap since the result is a string. 98 | return Ok(String::from(result.as_str().unwrap())); 99 | } 100 | 101 | ColoredFormatter::new(PrettyFormatter::new()) 102 | .to_colored_json_auto(&result) 103 | .with_context(|| "Failed to format the JSON data".to_string()) 104 | } 105 | 106 | #[tokio::main] 107 | async fn main() -> Result<()> { 108 | // Use a custom panic hook. 109 | use_custom_panic_hook(); 110 | 111 | let args = Args::parse(); 112 | 113 | if let Some(path) = args.json_file.as_deref() { 114 | let contents = read_file(path).await?; 115 | 116 | render(process_json(&contents, &args).await); 117 | 118 | return Ok(()); 119 | } 120 | 121 | let mut stdout = stdout(); 122 | 123 | if args.stream { 124 | let mut reader = BufReader::new(stdin()).lines(); 125 | 126 | while let Some(mut line) = reader 127 | .next_line() 128 | .await 129 | .with_context(|| "Failed to read stream".to_string())? 130 | { 131 | render(process_json(&line, &args).await); 132 | 133 | stdout 134 | .flush() 135 | .await 136 | .with_context(|| "Failed to flush stdout".to_string())?; 137 | 138 | line.clear(); 139 | } 140 | 141 | return Ok(()); 142 | } 143 | 144 | let mut buffer = Vec::new(); 145 | let mut stdin = stdin(); 146 | 147 | // By default, read the whole piped content from stdin. 148 | stdin 149 | .read_to_end(&mut buffer) 150 | .await 151 | .with_context(|| "Failed to read piped content from stdin".to_string())?; 152 | 153 | let lines = String::from_utf8(buffer) 154 | .with_context(|| "Failed to convert piped content from stdin".to_string())?; 155 | 156 | render(process_json(&lines, &args).await); 157 | 158 | Ok(()) 159 | } 160 | -------------------------------------------------------------------------------- /crates/jql/src/panic.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | panic, 3 | process::exit, 4 | }; 5 | 6 | /// Use a custom hook to manage broken pipe errors. 7 | /// See #86. 8 | pub fn use_custom_panic_hook() { 9 | // Take the hook. 10 | let hook = panic::take_hook(); 11 | 12 | // Register a custom panic hook. 13 | panic::set_hook(Box::new(move |panic_info| { 14 | let panic_message = panic_info.to_string(); 15 | 16 | // Exit on broken pipe message. 17 | if panic_message.contains("Broken pipe") || panic_message.contains("os error 32") { 18 | exit(0); 19 | } 20 | 21 | // Hook back to default. 22 | (hook)(panic_info); 23 | })); 24 | } 25 | -------------------------------------------------------------------------------- /fuzz/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "arbitrary" 7 | version = "1.3.0" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "e2d098ff73c1ca148721f37baad5ea6a465a13f9573aba8641fbbbae8164a54e" 10 | 11 | [[package]] 12 | name = "cc" 13 | version = "1.2.11" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "e4730490333d58093109dc02c23174c3f4d490998c3fed3cc8e82d57afedb9cf" 16 | dependencies = [ 17 | "jobserver", 18 | "libc", 19 | "shlex", 20 | ] 21 | 22 | [[package]] 23 | name = "jobserver" 24 | version = "0.1.32" 25 | source = "registry+https://github.com/rust-lang/crates.io-index" 26 | checksum = "48d1dbcbbeb6a7fec7e059840aa538bd62aaccf972c7346c4d9d2059312853d0" 27 | dependencies = [ 28 | "libc", 29 | ] 30 | 31 | [[package]] 32 | name = "jql-parser" 33 | version = "8.0.2" 34 | dependencies = [ 35 | "thiserror", 36 | "winnow", 37 | ] 38 | 39 | [[package]] 40 | name = "jql-parser-fuzz" 41 | version = "0.0.0" 42 | dependencies = [ 43 | "jql-parser", 44 | "libfuzzer-sys", 45 | ] 46 | 47 | [[package]] 48 | name = "libc" 49 | version = "0.2.142" 50 | source = "registry+https://github.com/rust-lang/crates.io-index" 51 | checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" 52 | 53 | [[package]] 54 | name = "libfuzzer-sys" 55 | version = "0.4.9" 56 | source = "registry+https://github.com/rust-lang/crates.io-index" 57 | checksum = "cf78f52d400cf2d84a3a973a78a592b4adc535739e0a5597a0da6f0c357adc75" 58 | dependencies = [ 59 | "arbitrary", 60 | "cc", 61 | ] 62 | 63 | [[package]] 64 | name = "memchr" 65 | version = "2.5.0" 66 | source = "registry+https://github.com/rust-lang/crates.io-index" 67 | checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" 68 | 69 | [[package]] 70 | name = "proc-macro2" 71 | version = "1.0.92" 72 | source = "registry+https://github.com/rust-lang/crates.io-index" 73 | checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" 74 | dependencies = [ 75 | "unicode-ident", 76 | ] 77 | 78 | [[package]] 79 | name = "quote" 80 | version = "1.0.35" 81 | source = "registry+https://github.com/rust-lang/crates.io-index" 82 | checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" 83 | dependencies = [ 84 | "proc-macro2", 85 | ] 86 | 87 | [[package]] 88 | name = "shlex" 89 | version = "1.3.0" 90 | source = "registry+https://github.com/rust-lang/crates.io-index" 91 | checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" 92 | 93 | [[package]] 94 | name = "syn" 95 | version = "2.0.90" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" 98 | dependencies = [ 99 | "proc-macro2", 100 | "quote", 101 | "unicode-ident", 102 | ] 103 | 104 | [[package]] 105 | name = "thiserror" 106 | version = "2.0.11" 107 | source = "registry+https://github.com/rust-lang/crates.io-index" 108 | checksum = "d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc" 109 | dependencies = [ 110 | "thiserror-impl", 111 | ] 112 | 113 | [[package]] 114 | name = "thiserror-impl" 115 | version = "2.0.11" 116 | source = "registry+https://github.com/rust-lang/crates.io-index" 117 | checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2" 118 | dependencies = [ 119 | "proc-macro2", 120 | "quote", 121 | "syn", 122 | ] 123 | 124 | [[package]] 125 | name = "unicode-ident" 126 | version = "1.0.8" 127 | source = "registry+https://github.com/rust-lang/crates.io-index" 128 | checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" 129 | 130 | [[package]] 131 | name = "winnow" 132 | version = "0.7.0" 133 | source = "registry+https://github.com/rust-lang/crates.io-index" 134 | checksum = "7e49d2d35d3fad69b39b94139037ecfb4f359f08958b9c11e7315ce770462419" 135 | dependencies = [ 136 | "memchr", 137 | ] 138 | -------------------------------------------------------------------------------- /fuzz/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "jql-parser-fuzz" 3 | publish = false 4 | version = "0.0.0" 5 | 6 | [package.metadata] 7 | cargo-fuzz = true 8 | 9 | [dependencies] 10 | libfuzzer-sys = "0.4.9" 11 | 12 | [dependencies.jql-parser] 13 | path = "../crates/jql-parser" 14 | 15 | # Prevent this from interfering with workspaces. 16 | [workspace] 17 | members = ["."] 18 | 19 | [profile.release] 20 | debug = 1 21 | 22 | [[bin]] 23 | doc = false 24 | name = "fuzz_parser" 25 | path = "fuzz_targets/fuzz_parser.rs" 26 | test = false 27 | -------------------------------------------------------------------------------- /fuzz/fuzz_targets/fuzz_parser.rs: -------------------------------------------------------------------------------- 1 | #![no_main] 2 | 3 | extern crate libfuzzer_sys; 4 | 5 | use libfuzzer_sys::fuzz_target; 6 | 7 | fuzz_target!(|data: &[u8]| { 8 | if let Ok(s) = std::str::from_utf8(data) { 9 | let _ = jql_parser::parser::parse(s); 10 | } 11 | }); 12 | -------------------------------------------------------------------------------- /justfile: -------------------------------------------------------------------------------- 1 | # Audit. 2 | audit: 3 | cargo audit 4 | 5 | # Clippy. 6 | clippy: 7 | cargo clippy 8 | 9 | # Format. 10 | fmt: 11 | cargo fmt --all 12 | 13 | # Fuzz parser. 14 | fuzz: 15 | cargo fuzz run fuzz_parser 16 | 17 | # Run all tests. 18 | test: 19 | cargo nextest run 20 | 21 | # Run binary tests. 22 | test-bin: 23 | cargo nextest run -p jql 24 | 25 | # Run parser tests. 26 | test-parser: 27 | cargo nextest run -p jql-parser 28 | 29 | # Run runner tests. 30 | test-runner: 31 | cargo nextest run -p jql-runner 32 | -------------------------------------------------------------------------------- /performance.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | REPORT=$(pwd)/PERFORMANCE.md 4 | PERFORMANCE_TMP_DIR=$(pwd)/performance_tmp 5 | MD_FILES="$PERFORMANCE_TMP_DIR/"*".md" 6 | LARGE_JSON_FILE=$(pwd)/assets/github-repositories.json 7 | MIN_RUNS=1000 8 | 9 | # Remove export file if present. 10 | rm -f $REPORT 11 | 12 | # Create the directory. 13 | mkdir $PERFORMANCE_TMP_DIR 14 | 15 | # Run the benchmarks. 16 | hyperfine \ 17 | --export-markdown "$PERFORMANCE_TMP_DIR/OBJECT.md" \ 18 | --min-runs $MIN_RUNS \ 19 | "echo '{ \"foo\": \"bar\" }' | jq '.foo'" \ 20 | "echo '{ \"foo\": \"bar\" }' | jql '\"foo\"'" 21 | 22 | hyperfine \ 23 | --export-markdown "$PERFORMANCE_TMP_DIR/ARRAY_INDEX.md" \ 24 | --min-runs $MIN_RUNS \ 25 | "echo '[1, 2, 3]' | jq '.[0]'" \ 26 | "echo '[1, 2, 3]' | jql '[0]'" 27 | 28 | hyperfine \ 29 | --export-markdown "$PERFORMANCE_TMP_DIR/ARRAY_FLATTEN.md" \ 30 | --min-runs $MIN_RUNS \ 31 | "echo '[1, [2], [[3]]]' | jq 'flatten'" \ 32 | "echo '[1, [2], [[3]]]' | jql '..'" 33 | 34 | hyperfine \ 35 | --export-markdown "$PERFORMANCE_TMP_DIR/PROPERTY_SELECTION_LARGE_JSON.md" \ 36 | --min-runs $MIN_RUNS \ 37 | "cat $LARGE_JSON_FILE | jq -r '[.[] | {name: .name, url: .url, language: .language, stargazers_count: .stargazers_count, watchers_count: .watchers_count}]' > /dev/null" \ 38 | "cat $LARGE_JSON_FILE | jql '|>{\"name\", \"url\", \"language\", \"stargazers_count\", \"watchers_count\"}' > /dev/null" 39 | 40 | # Merge all the markdown files into the performance one. 41 | for md_file in $MD_FILES; do (cat "${md_file}"; echo) >> $REPORT; done 42 | 43 | # Remove the directory. 44 | rm -R -f $PERFORMANCE_TMP_DIR 45 | -------------------------------------------------------------------------------- /release.toml: -------------------------------------------------------------------------------- 1 | consolidate-commits = true 2 | shared-version = true 3 | sign-commit = true 4 | sign-tag = true 5 | tag = true 6 | -------------------------------------------------------------------------------- /rustfmt.toml: -------------------------------------------------------------------------------- 1 | edition = "2021" 2 | group_imports = "StdExternalCrate" 3 | imports_granularity = "Crate" 4 | imports_layout="Vertical" 5 | use_field_init_shorthand = true 6 | style_edition = "2024" 7 | --------------------------------------------------------------------------------