Add system-query
This commit is contained in:
2
packages/system-query/.gitignore
vendored
Normal file
2
packages/system-query/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.idea/
|
||||
target/
|
244
packages/system-query/Cargo.lock
generated
Normal file
244
packages/system-query/Cargo.lock
generated
Normal file
@ -0,0 +1,244 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "console_error_panic_hook"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.64"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c5f195fe497f702db0f318b07fdd68edb16955aed830df8363d837542f8f935a"
|
||||
dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.18.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.66"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scoped-tls"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.188"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde-wasm-bindgen"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f3b143e2833c57ab9ad3ea280d21fd34e285a42837aeb0ee301f4f41890fa00e"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"serde",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.188"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "718fa2415bcb8d8bd775917a1bf12a7931b6dfa890753378538118181e0cb398"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "system-query"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
"serde",
|
||||
"serde-wasm-bindgen",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-test",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.87"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.87"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"log",
|
||||
"once_cell",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-futures"
|
||||
version = "0.4.37"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c02dbc21516f9f1f04f187958890d7e6026df8d16540b7ad9492bc34a67cea03"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
"web-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.87"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.87"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.87"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-test"
|
||||
version = "0.3.37"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e6e302a7ea94f83a6d09e78e7dc7d9ca7b186bc2829c24a22d0753efd680671"
|
||||
dependencies = [
|
||||
"console_error_panic_hook",
|
||||
"js-sys",
|
||||
"scoped-tls",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-futures",
|
||||
"wasm-bindgen-test-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-test-macro"
|
||||
version = "0.3.37"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ecb993dd8c836930ed130e020e77d9b2e65dd0fbab1b67c790b0f5d80b11a575"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "web-sys"
|
||||
version = "0.3.64"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b85cbef8c220a6abc02aefd892dfc0fc23afb1c6a426316ec33253a3877249b"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
]
|
17
packages/system-query/Cargo.toml
Normal file
17
packages/system-query/Cargo.toml
Normal file
@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "system-query"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.11.0"
|
||||
serde = { version = "1.0.188", features = ["derive"] }
|
||||
serde-wasm-bindgen = "0.5.0"
|
||||
wasm-bindgen = "0.2.87"
|
||||
|
||||
[dev-dependencies]
|
||||
wasm-bindgen-test = "0.3.37"
|
1
packages/system-query/README.md
Normal file
1
packages/system-query/README.md
Normal file
@ -0,0 +1 @@
|
||||
# system-query
|
18
packages/system-query/package.json
Normal file
18
packages/system-query/package.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "@snort/system-query",
|
||||
"version": "0.1.0",
|
||||
"packageManager": "yarn@3.6.3",
|
||||
"scripts": {
|
||||
"build": "npx -y wasm-pack build -t web -s snort"
|
||||
},
|
||||
"files": [
|
||||
"pkg/system_query_bg.wasm",
|
||||
"pkg/system_query.js",
|
||||
"pkg/system_query.d.ts"
|
||||
],
|
||||
"module": "pkg/system_query.js",
|
||||
"types": "pkg/system_query.d.ts",
|
||||
"devDependencies": {
|
||||
"wasm-pack": "^0.12.1"
|
||||
}
|
||||
}
|
180
packages/system-query/src/diff.rs
Normal file
180
packages/system-query/src/diff.rs
Normal file
@ -0,0 +1,180 @@
|
||||
use itertools::Itertools;
|
||||
use crate::FlatReqFilter;
|
||||
|
||||
pub fn diff_filter(prev: &Vec<FlatReqFilter>, next: &Vec<FlatReqFilter>) -> Vec::<FlatReqFilter> {
|
||||
let mut added: Vec::<FlatReqFilter> = vec![];
|
||||
|
||||
for n in next.iter() {
|
||||
if !prev.iter().contains(&n) {
|
||||
added.push(n.clone())
|
||||
}
|
||||
}
|
||||
|
||||
added
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn simple_diff_same() {
|
||||
let prev = vec![
|
||||
FlatReqFilter {
|
||||
id: Some("a".to_owned()),
|
||||
author: None,
|
||||
kind: None,
|
||||
e_tag: None,
|
||||
p_tag: None,
|
||||
t_tag: None,
|
||||
d_tag: None,
|
||||
r_tag: None,
|
||||
search: None,
|
||||
since: None,
|
||||
until: None,
|
||||
limit: None,
|
||||
}
|
||||
];
|
||||
let next = vec![
|
||||
FlatReqFilter {
|
||||
id: Some("a".to_owned()),
|
||||
author: None,
|
||||
kind: None,
|
||||
e_tag: None,
|
||||
p_tag: None,
|
||||
t_tag: None,
|
||||
d_tag: None,
|
||||
r_tag: None,
|
||||
search: None,
|
||||
since: None,
|
||||
until: None,
|
||||
limit: None,
|
||||
}
|
||||
];
|
||||
|
||||
let result = diff_filter(&prev, &next);
|
||||
assert_eq!(result, vec![])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple_diff_add() {
|
||||
let prev = vec![
|
||||
FlatReqFilter {
|
||||
id: Some("a".to_owned()),
|
||||
author: None,
|
||||
kind: None,
|
||||
e_tag: None,
|
||||
p_tag: None,
|
||||
t_tag: None,
|
||||
d_tag: None,
|
||||
r_tag: None,
|
||||
search: None,
|
||||
since: None,
|
||||
until: None,
|
||||
limit: None,
|
||||
}
|
||||
];
|
||||
let next = vec![
|
||||
FlatReqFilter {
|
||||
id: Some("a".to_owned()),
|
||||
author: None,
|
||||
kind: None,
|
||||
e_tag: None,
|
||||
p_tag: None,
|
||||
t_tag: None,
|
||||
d_tag: None,
|
||||
r_tag: None,
|
||||
search: None,
|
||||
since: None,
|
||||
until: None,
|
||||
limit: None,
|
||||
},
|
||||
FlatReqFilter {
|
||||
id: Some("b".to_owned()),
|
||||
author: None,
|
||||
kind: None,
|
||||
e_tag: None,
|
||||
p_tag: None,
|
||||
t_tag: None,
|
||||
d_tag: None,
|
||||
r_tag: None,
|
||||
search: None,
|
||||
since: None,
|
||||
until: None,
|
||||
limit: None,
|
||||
},
|
||||
];
|
||||
|
||||
let result = diff_filter(&prev, &next);
|
||||
assert_eq!(result, vec![
|
||||
FlatReqFilter {
|
||||
id: Some("b".to_owned()),
|
||||
author: None,
|
||||
kind: None,
|
||||
e_tag: None,
|
||||
p_tag: None,
|
||||
t_tag: None,
|
||||
d_tag: None,
|
||||
r_tag: None,
|
||||
search: None,
|
||||
since: None,
|
||||
until: None,
|
||||
limit: None,
|
||||
}
|
||||
])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple_diff_replace() {
|
||||
let prev = vec![
|
||||
FlatReqFilter {
|
||||
id: Some("a".to_owned()),
|
||||
author: None,
|
||||
kind: None,
|
||||
e_tag: None,
|
||||
p_tag: None,
|
||||
t_tag: None,
|
||||
d_tag: None,
|
||||
r_tag: None,
|
||||
search: None,
|
||||
since: None,
|
||||
until: None,
|
||||
limit: None,
|
||||
}
|
||||
];
|
||||
let next = vec![
|
||||
FlatReqFilter {
|
||||
id: Some("b".to_owned()),
|
||||
author: None,
|
||||
kind: None,
|
||||
e_tag: None,
|
||||
p_tag: None,
|
||||
t_tag: None,
|
||||
d_tag: None,
|
||||
r_tag: None,
|
||||
search: None,
|
||||
since: None,
|
||||
until: None,
|
||||
limit: None,
|
||||
},
|
||||
];
|
||||
|
||||
let result = diff_filter(&prev, &next);
|
||||
assert_eq!(result, vec![
|
||||
FlatReqFilter {
|
||||
id: Some("b".to_owned()),
|
||||
author: None,
|
||||
kind: None,
|
||||
e_tag: None,
|
||||
p_tag: None,
|
||||
t_tag: None,
|
||||
d_tag: None,
|
||||
r_tag: None,
|
||||
search: None,
|
||||
since: None,
|
||||
until: None,
|
||||
limit: None,
|
||||
}
|
||||
])
|
||||
}
|
||||
}
|
182
packages/system-query/src/expand.rs
Normal file
182
packages/system-query/src/expand.rs
Normal file
@ -0,0 +1,182 @@
|
||||
use itertools::Itertools;
|
||||
use crate::{FlatReqFilter, ReqFilter};
|
||||
|
||||
#[derive(Clone)]
|
||||
enum StringOrNumberEntry<'a> {
|
||||
String((&'static str, &'a String)),
|
||||
Number((&'static str, &'a i32)),
|
||||
}
|
||||
|
||||
pub fn expand_filter(filter: &ReqFilter) -> Vec<FlatReqFilter> {
|
||||
let mut ret: Vec<FlatReqFilter> = Vec::new();
|
||||
|
||||
let mut inputs: Vec<Vec<StringOrNumberEntry>> = vec![];
|
||||
if let Some(ids) = &filter.ids {
|
||||
let t_ids = ids.iter().map(|z| StringOrNumberEntry::String(("id", z))).collect_vec();
|
||||
inputs.push(t_ids);
|
||||
}
|
||||
if let Some(authors) = &filter.authors {
|
||||
let t_ids = authors.iter().map(|z| StringOrNumberEntry::String(("author", z))).collect_vec();
|
||||
inputs.push(t_ids);
|
||||
}
|
||||
if let Some(kinds) = &filter.kinds {
|
||||
let t_ids = kinds.iter().map(|z| StringOrNumberEntry::Number(("kind", z))).collect_vec();
|
||||
inputs.push(t_ids);
|
||||
}
|
||||
if let Some(e_tags) = &filter.e_tag {
|
||||
let t_ids = e_tags.iter().map(|z| StringOrNumberEntry::String(("e_tag", z))).collect_vec();
|
||||
inputs.push(t_ids);
|
||||
}
|
||||
if let Some(p_tags) = &filter.p_tag {
|
||||
let t_ids = p_tags.iter().map(|z| StringOrNumberEntry::String(("p_tag", z))).collect_vec();
|
||||
inputs.push(t_ids);
|
||||
}
|
||||
if let Some(d_tags) = &filter.d_tag {
|
||||
let t_ids = d_tags.iter().map(|z| StringOrNumberEntry::String(("d_tag", z))).collect_vec();
|
||||
inputs.push(t_ids);
|
||||
}
|
||||
if let Some(t_tags) = &filter.t_tag {
|
||||
let t_ids = t_tags.iter().map(|z| StringOrNumberEntry::String(("t_tag", z))).collect_vec();
|
||||
inputs.push(t_ids);
|
||||
}
|
||||
if let Some(r_tags) = &filter.r_tag {
|
||||
let t_ids = r_tags.iter().map(|z| StringOrNumberEntry::String(("r_tag", z))).collect_vec();
|
||||
inputs.push(t_ids);
|
||||
}
|
||||
if let Some(search) = &filter.search {
|
||||
let t_ids = search.iter().map(|z| StringOrNumberEntry::String(("search", z))).collect_vec();
|
||||
inputs.push(t_ids);
|
||||
}
|
||||
|
||||
for p in inputs.iter().multi_cartesian_product() {
|
||||
ret.push(FlatReqFilter {
|
||||
id: p.iter().find_map(|q| {
|
||||
if let StringOrNumberEntry::String((k, v)) = q {
|
||||
if (*k).eq("id") {
|
||||
return Some((*v).to_string());
|
||||
}
|
||||
}
|
||||
None
|
||||
}),
|
||||
author: p.iter().find_map(|q| {
|
||||
if let StringOrNumberEntry::String((k, v)) = q {
|
||||
if (*k).eq("author") {
|
||||
return Some((*v).to_string());
|
||||
}
|
||||
}
|
||||
None
|
||||
}),
|
||||
kind: p.iter().find_map(|q| {
|
||||
if let StringOrNumberEntry::Number((k, v)) = q {
|
||||
if (*k).eq("kind") {
|
||||
return Some((*v).clone());
|
||||
}
|
||||
}
|
||||
None
|
||||
}),
|
||||
e_tag: p.iter().find_map(|q| {
|
||||
if let StringOrNumberEntry::String((k, v)) = q {
|
||||
if (*k).eq("e_tag") {
|
||||
return Some((*v).to_string());
|
||||
}
|
||||
}
|
||||
None
|
||||
}),
|
||||
p_tag: p.iter().find_map(|q| {
|
||||
if let StringOrNumberEntry::String((k, v)) = q {
|
||||
if (*k).eq("p_tag") {
|
||||
return Some((*v).to_string());
|
||||
}
|
||||
}
|
||||
None
|
||||
}),
|
||||
t_tag: p.iter().find_map(|q| {
|
||||
if let StringOrNumberEntry::String((k, v)) = q {
|
||||
if (*k).eq("t_tag") {
|
||||
return Some((*v).to_string());
|
||||
}
|
||||
}
|
||||
None
|
||||
}),
|
||||
d_tag: p.iter().find_map(|q| {
|
||||
if let StringOrNumberEntry::String((k, v)) = q {
|
||||
if (*k).eq("d_tag") {
|
||||
return Some((*v).to_string());
|
||||
}
|
||||
}
|
||||
None
|
||||
}),
|
||||
r_tag: p.iter().find_map(|q| {
|
||||
if let StringOrNumberEntry::String((k, v)) = q {
|
||||
if (*k).eq("r_tag") {
|
||||
return Some((*v).to_string());
|
||||
}
|
||||
}
|
||||
None
|
||||
}),
|
||||
search: p.iter().find_map(|q| {
|
||||
if let StringOrNumberEntry::String((k, v)) = q {
|
||||
if (*k).eq("search") {
|
||||
return Some((*v).to_string());
|
||||
}
|
||||
}
|
||||
None
|
||||
}),
|
||||
since: filter.since,
|
||||
until: filter.until,
|
||||
limit: filter.limit,
|
||||
})
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ReqFilter;
|
||||
|
||||
#[test]
|
||||
fn test_expand_filter() {
|
||||
let input = ReqFilter {
|
||||
authors: Some(vec!["a".to_owned(), "b".to_owned(), "c".to_owned()]),
|
||||
kinds: Some(vec![1, 2, 3]),
|
||||
ids: Some(vec!["x".to_owned(), "y".to_owned()]),
|
||||
p_tag: Some(vec!["a".to_owned()]),
|
||||
t_tag: None,
|
||||
d_tag: None,
|
||||
r_tag: None,
|
||||
search: None,
|
||||
since: Some(99),
|
||||
until: None,
|
||||
limit: Some(10),
|
||||
e_tag: None,
|
||||
};
|
||||
|
||||
let output = expand_filter(&input);
|
||||
output.iter().take(5).for_each(|x| println!("{:?}", x));
|
||||
let expected = vec![
|
||||
FlatReqFilter { author: Some("a".to_owned()), kind: Some(1), id: Some("x".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("a".to_owned()), kind: Some(1), id: Some("y".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("a".to_owned()), kind: Some(2), id: Some("x".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("a".to_owned()), kind: Some(2), id: Some("y".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("a".to_owned()), kind: Some(3), id: Some("x".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("a".to_owned()), kind: Some(3), id: Some("y".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("b".to_owned()), kind: Some(1), id: Some("x".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("b".to_owned()), kind: Some(1), id: Some("y".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("b".to_owned()), kind: Some(2), id: Some("x".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("b".to_owned()), kind: Some(2), id: Some("y".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("b".to_owned()), kind: Some(3), id: Some("x".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("b".to_owned()), kind: Some(3), id: Some("y".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("c".to_owned()), kind: Some(1), id: Some("x".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("c".to_owned()), kind: Some(1), id: Some("y".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("c".to_owned()), kind: Some(2), id: Some("x".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("c".to_owned()), kind: Some(2), id: Some("y".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("c".to_owned()), kind: Some(3), id: Some("x".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
FlatReqFilter { author: Some("c".to_owned()), kind: Some(3), id: Some("y".to_owned()), p_tag: Some("a".to_owned()), t_tag: None, d_tag: None, r_tag: None, search: None, since: Some(99), until: None, limit: Some(10), e_tag: None },
|
||||
];
|
||||
assert_eq!(output.len(), expected.len());
|
||||
output
|
||||
.iter()
|
||||
.for_each(|a| assert!(expected.contains(a)));
|
||||
}
|
||||
}
|
78
packages/system-query/src/lib.rs
Normal file
78
packages/system-query/src/lib.rs
Normal file
@ -0,0 +1,78 @@
|
||||
extern crate wasm_bindgen;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
mod expand;
|
||||
mod diff;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
|
||||
pub struct ReqFilter {
|
||||
#[serde(rename = "ids", skip_serializing_if = "Option::is_none")]
|
||||
pub ids: Option<Vec<String>>,
|
||||
#[serde(rename = "authors", skip_serializing_if = "Option::is_none")]
|
||||
pub authors: Option<Vec<String>>,
|
||||
#[serde(rename = "kinds", skip_serializing_if = "Option::is_none")]
|
||||
pub kinds: Option<Vec<i32>>,
|
||||
#[serde(rename = "#e", skip_serializing_if = "Option::is_none")]
|
||||
pub e_tag: Option<Vec<String>>,
|
||||
#[serde(rename = "#p", skip_serializing_if = "Option::is_none")]
|
||||
pub p_tag: Option<Vec<String>>,
|
||||
#[serde(rename = "#t", skip_serializing_if = "Option::is_none")]
|
||||
pub t_tag: Option<Vec<String>>,
|
||||
#[serde(rename = "#d", skip_serializing_if = "Option::is_none")]
|
||||
pub d_tag: Option<Vec<String>>,
|
||||
#[serde(rename = "#r", skip_serializing_if = "Option::is_none")]
|
||||
pub r_tag: Option<Vec<String>>,
|
||||
#[serde(rename = "search", skip_serializing_if = "Option::is_none")]
|
||||
pub search: Option<Vec<String>>,
|
||||
#[serde(rename = "since", skip_serializing_if = "Option::is_none")]
|
||||
pub since: Option<i32>,
|
||||
#[serde(rename = "until", skip_serializing_if = "Option::is_none")]
|
||||
pub until: Option<i32>,
|
||||
#[serde(rename = "limit", skip_serializing_if = "Option::is_none")]
|
||||
pub limit: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
|
||||
pub struct FlatReqFilter {
|
||||
#[serde(rename = "ids", skip_serializing_if = "Option::is_none")]
|
||||
id: Option<String>,
|
||||
#[serde(rename = "authors", skip_serializing_if = "Option::is_none")]
|
||||
author: Option<String>,
|
||||
#[serde(rename = "kinds", skip_serializing_if = "Option::is_none")]
|
||||
kind: Option<i32>,
|
||||
#[serde(rename = "#e", skip_serializing_if = "Option::is_none")]
|
||||
e_tag: Option<String>,
|
||||
#[serde(rename = "#p", skip_serializing_if = "Option::is_none")]
|
||||
p_tag: Option<String>,
|
||||
#[serde(rename = "#t", skip_serializing_if = "Option::is_none")]
|
||||
t_tag: Option<String>,
|
||||
#[serde(rename = "#d", skip_serializing_if = "Option::is_none")]
|
||||
d_tag: Option<String>,
|
||||
#[serde(rename = "#r", skip_serializing_if = "Option::is_none")]
|
||||
r_tag: Option<String>,
|
||||
#[serde(rename = "search", skip_serializing_if = "Option::is_none")]
|
||||
search: Option<String>,
|
||||
#[serde(rename = "since", skip_serializing_if = "Option::is_none")]
|
||||
since: Option<i32>,
|
||||
#[serde(rename = "until", skip_serializing_if = "Option::is_none")]
|
||||
until: Option<i32>,
|
||||
#[serde(rename = "limit", skip_serializing_if = "Option::is_none")]
|
||||
limit: Option<i32>,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn diff_filters(prev: JsValue, next: JsValue) -> Result<JsValue, JsValue> {
|
||||
let prev_parsed: Vec<FlatReqFilter> = serde_wasm_bindgen::from_value(prev)?;
|
||||
let next_parsed: Vec<FlatReqFilter> = serde_wasm_bindgen::from_value(next)?;
|
||||
let result = diff::diff_filter(&prev_parsed, &next_parsed);
|
||||
Ok(serde_wasm_bindgen::to_value(&result)?)
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn expand_filter(val: JsValue) -> Result<JsValue, JsValue> {
|
||||
let parsed: ReqFilter = serde_wasm_bindgen::from_value(val)?;
|
||||
let result = expand::expand_filter(&parsed);
|
||||
Ok(serde_wasm_bindgen::to_value(&result)?)
|
||||
}
|
0
packages/system-query/src/utils.rs
Normal file
0
packages/system-query/src/utils.rs
Normal file
Reference in New Issue
Block a user