Skip to content

Commit

Permalink
add: exact_search
Browse files Browse the repository at this point in the history
  • Loading branch information
ynqa committed Apr 21, 2024
1 parent 4cdf0d3 commit 0900de3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/jnv.rs
Expand Up @@ -98,6 +98,10 @@ impl Jnv {
indent: usize,
) -> Result<Prompt<Self>> {
let input_stream = deserialize_json(&input)?;

let mut trie = FilterTrie::default();
trie.insert(".", input_stream.clone());

let all_kinds = JsonStream::new(input_stream.clone(), None).flatten_kinds();
let suggest = Suggest::from_iter(all_kinds.iter().filter_map(|kind| kind.path()).map(
|segments| {
Expand Down Expand Up @@ -157,7 +161,7 @@ impl Jnv {
indent,
},
},
trie: FilterTrie::default(),
trie,
suggest,
expand_depth,
no_hint,
Expand Down
55 changes: 54 additions & 1 deletion src/trie.rs
Expand Up @@ -10,6 +10,10 @@ impl FilterTrie {
self.0.insert(query.to_string(), json_nodes);
}

pub fn exact_search(&self, query: &str) -> Option<&Vec<serde_json::Value>> {

Check warning on line 13 in src/trie.rs

View workflow job for this annotation

GitHub Actions / test

method `exact_search` is never used

Check warning on line 13 in src/trie.rs

View workflow job for this annotation

GitHub Actions / test

method `exact_search` is never used
self.0.get(query)
}

pub fn prefix_search(&self, query: &str) -> Option<&Vec<serde_json::Value>> {
self.0
.get_ancestor(query)
Expand All @@ -21,12 +25,40 @@ impl FilterTrie {
mod tests {
use super::*;

mod exact_search {
use super::*;
use serde_json::json;

#[test]
fn test_exact_match() {
let mut trie = FilterTrie::default();
trie.insert("apple", vec![json!({"type": "fruit"})]);
trie.insert("app", vec![json!({"type": "abbreviation"})]);

let result = trie.exact_search("app");
assert!(result.is_some());
let values = result.unwrap();
assert_eq!(values.len(), 1);
assert_eq!(values[0], json!({"type": "abbreviation"}));
}

#[test]
fn test_no_match() {
let mut trie = FilterTrie::default();
trie.insert("apple", vec![json!({"type": "fruit"})]);
trie.insert("app", vec![json!({"type": "abbreviation"})]);

let result = trie.exact_search("application");
assert!(result.is_none());
}
}

mod prefix_search {
use super::*;
use serde_json::json;

#[test]
fn test() {
fn test_with_exact_prefix() {
let mut trie = FilterTrie::default();
trie.insert("apple", vec![json!({"type": "fruit"})]);
trie.insert("app", vec![json!({"type": "abbreviation"})]);
Expand All @@ -36,18 +68,39 @@ mod tests {
let values = result.unwrap();
assert_eq!(values.len(), 1);
assert_eq!(values[0], json!({"type": "abbreviation"}));
}

#[test]
fn test_with_longer_query_than_keys() {
let mut trie = FilterTrie::default();
trie.insert("apple", vec![json!({"type": "fruit"})]);
trie.insert("app", vec![json!({"type": "abbreviation"})]);

let result = trie.prefix_search("application");
assert!(result.is_some());
let values = result.unwrap();
assert_eq!(values.len(), 1);
assert_eq!(values[0], json!({"type": "abbreviation"}));
}

#[test]
fn test_with_full_key_match() {
let mut trie = FilterTrie::default();
trie.insert("apple", vec![json!({"type": "fruit"})]);
trie.insert("app", vec![json!({"type": "abbreviation"})]);

let result = trie.prefix_search("apple");
assert!(result.is_some());
let values = result.unwrap();
assert_eq!(values.len(), 1);
assert_eq!(values[0], json!({"type": "fruit"}));
}

#[test]
fn test_with_shorter_query_than_any_key() {
let mut trie = FilterTrie::default();
trie.insert("apple", vec![json!({"type": "fruit"})]);
trie.insert("app", vec![json!({"type": "abbreviation"})]);

let result = trie.prefix_search("ap");
assert!(result.is_none());
Expand Down

0 comments on commit 0900de3

Please sign in to comment.