Skip to content

Commit

Permalink
chore(utils): add trie match all base path
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Sep 10, 2024
1 parent e4d1514 commit 2e299c4
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 20 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_examples"
version = "2.4.0"
version = "2.4.1"
authors = [
"j-mendez <[email protected]>",
]
Expand Down
2 changes: 1 addition & 1 deletion spider/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider"
version = "2.4.0"
version = "2.4.1"
authors = [
"j-mendez <[email protected]>"
]
Expand Down
26 changes: 21 additions & 5 deletions spider/src/utils/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ impl<V: std::fmt::Debug> TrieNode<V> {
pub struct Trie<V: Debug> {
/// A new trie node.
pub root: TrieNode<V>,
/// Contains a match all segment to default to.
pub match_all: bool,
}

impl<V: Debug> Trie<V> {
/// A new trie node.
pub fn new() -> Self {
Self {
root: TrieNode::new(),
match_all: false,
}
}

Expand Down Expand Up @@ -83,18 +86,27 @@ impl<V: Debug> Trie<V> {
.or_insert_with(TrieNode::new);
}

if path == "/" {
self.match_all = true;
}

node.value = Some(value);
}

/// Search for a path in the trie.
pub fn search(&self, input: &str) -> Option<&V> {
let normalized_path = Self::normalize_path(input);
let mut node = &self.root;

if node.children.is_empty() {
return None;
}

let normalized_path = Self::normalize_path(input);

for segment in normalized_path.split('/').filter(|s| !s.is_empty()) {
if let Some(child) = node.children.get(segment) {
node = child;
} else {
} else if !self.match_all {
return None;
}
}
Expand Down Expand Up @@ -125,13 +137,17 @@ mod tests {
fn test_insert_and_search() {
let mut trie: Trie<usize> = Trie::new();
trie.insert("/path/to/node", 42);
trie.insert("https://mywebsite/path/to/node", 42);
trie.insert("https://mywebsite/path/to/node", 22);

assert_eq!(trie.search("https://mywebsite/path/to/node"), Some(&42));
assert_eq!(trie.search("/path/to/node"), Some(&42));
assert_eq!(trie.search("https://mywebsite/path/to/node"), Some(&22));
assert_eq!(trie.search("/path/to/node"), Some(&22));
assert_eq!(trie.search("/path"), None);
assert_eq!(trie.search("/path/to"), None);
assert_eq!(trie.search("/path/to/node/extra"), None);

// insert match all context
trie.insert("/", 11);
assert_eq!(trie.search("/random"), Some(&11));
}

#[test]
Expand Down
6 changes: 1 addition & 5 deletions spider/src/website.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,7 @@ async fn perform_intercept(
}

/// Setup interception for auth challenges. This does nothing without the 'chrome_intercept' flag.
#[cfg(all(
feature = "chrome",
feature = "chrome_intercept",
not(feature = "adblock")
))]
#[cfg(all(feature = "chrome", feature = "chrome_intercept",))]
async fn setup_auth_challenge_response(
page: &chromiumoxide::Page,
chrome_intercept: bool,
Expand Down
2 changes: 1 addition & 1 deletion spider_cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_cli"
version = "2.4.0"
version = "2.4.1"
authors = [
"j-mendez <[email protected]>"
]
Expand Down
2 changes: 1 addition & 1 deletion spider_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_utils"
version = "2.4.0"
version = "2.4.1"
authors = [
"j-mendez <[email protected]>"
]
Expand Down
2 changes: 1 addition & 1 deletion spider_worker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_worker"
version = "2.4.0"
version = "2.4.1"
authors = [
"j-mendez <[email protected]>"
]
Expand Down

0 comments on commit 2e299c4

Please sign in to comment.