Skip to content

Commit 28d6100

Browse files
committed
fmt & clippy
1 parent 80efc3c commit 28d6100

1 file changed

Lines changed: 121 additions & 59 deletions

File tree

src/lib.rs

Lines changed: 121 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,12 @@ struct BufferCache {
471471
///
472472
/// # Returns
473473
/// `Some(distance)` if a match is found, `None` otherwise
474-
fn is_fuzzy_prefix_match(search_term: &[u8], target: &[u8], max_distance: u8, cache: &mut BufferCache) -> Option<usize> {
474+
fn is_fuzzy_prefix_match(
475+
search_term: &[u8],
476+
target: &[u8],
477+
max_distance: u8,
478+
cache: &mut BufferCache,
479+
) -> Option<usize> {
475480
if search_term.is_empty() {
476481
return Some(0); // Empty search matches everything with distance 0
477482
}
@@ -537,6 +542,7 @@ fn levenshtein_distance(a: &[u8], b: &[u8], cache: &mut BufferCache) -> usize {
537542
}
538543

539544
// Initialize first row
545+
#[allow(clippy::needless_range_loop)]
540546
for j in 0..=n {
541547
prev_row[j] = j;
542548
}
@@ -550,8 +556,8 @@ fn levenshtein_distance(a: &[u8], b: &[u8], cache: &mut BufferCache) -> usize {
550556

551557
curr_row[j] = std::cmp::min(
552558
std::cmp::min(
553-
prev_row[j] + 1, // Deletion
554-
curr_row[j - 1] + 1, // Insertion
559+
prev_row[j] + 1, // Deletion
560+
curr_row[j - 1] + 1, // Insertion
555561
),
556562
prev_row[j - 1] + cost, // Substitution
557563
);
@@ -824,9 +830,12 @@ impl<'a, T> Iterator for TypoTolerantSearchIterator<'a, T> {
824830
if child_index == 0 {
825831
if let Some(ref value) = node.value {
826832
// Check if current_key is a fuzzy match for our search
827-
if let Some(distance) =
828-
is_fuzzy_prefix_match(&self.search_key, &current_key, self.max_distance, &mut self.cache)
829-
{
833+
if let Some(distance) = is_fuzzy_prefix_match(
834+
&self.search_key,
835+
&current_key,
836+
self.max_distance,
837+
&mut self.cache,
838+
) {
830839
// Found a match! Convert to String and return
831840
let key_string = String::from_utf8_lossy(&current_key).to_string();
832841

@@ -852,7 +861,8 @@ impl<'a, T> Iterator for TypoTolerantSearchIterator<'a, T> {
852861
if current_child_idx < node.children.len() {
853862
// Push next sibling
854863
if current_child_idx + 1 < node.children.len() {
855-
self.stack.push((node, child_index + 1, current_key.clone(), 0));
864+
self.stack
865+
.push((node, child_index + 1, current_key.clone(), 0));
856866
}
857867

858868
// Process current child
@@ -2190,10 +2200,7 @@ mod tests {
21902200
); // Exact match
21912201

21922202
// Too far (no match)
2193-
assert_eq!(
2194-
is_fuzzy_prefix_match(b"xyz", b"abc", 2, &mut cache),
2195-
None
2196-
);
2203+
assert_eq!(is_fuzzy_prefix_match(b"xyz", b"abc", 2, &mut cache), None);
21972204
assert_eq!(
21982205
is_fuzzy_prefix_match(b"hello", b"world", 2, &mut cache),
21992206
None
@@ -2211,10 +2218,7 @@ mod tests {
22112218
let mut cache = BufferCache::default();
22122219

22132220
// Search term longer than target
2214-
assert_eq!(
2215-
is_fuzzy_prefix_match(b"hello", b"hel", 1, &mut cache),
2216-
None
2217-
);
2221+
assert_eq!(is_fuzzy_prefix_match(b"hello", b"hel", 1, &mut cache), None);
22182222

22192223
// Distance 2 allows more flexibility
22202224
assert_eq!(
@@ -2240,21 +2244,26 @@ mod tests {
22402244
tree.insert("hero", 4);
22412245

22422246
// Search with distance 1 - "helo" typo
2243-
let results: Vec<_> = tree.search_typo_tolerant("helo", 1)
2244-
.map(|(s, k, t)| (s, *k, t)).collect();
2247+
let results: Vec<_> = tree
2248+
.search_typo_tolerant("helo", 1)
2249+
.map(|(s, k, t)| (s, *k, t))
2250+
.collect();
22452251

22462252
// All words actually have distance 1 from "helo":
22472253
// - "hell": hel matches, then distance("o", "") = 1
22482254
// - "hello": hello matches, distance("helo", "hello") = 1 (insert 'l')
22492255
// - "help": help matches, distance("helo", "help") = 1 (substitute 'o' with 'p')
22502256
// - "hero": hero matches, distance("helo", "hero") = 1 (substitute 'l' with 'r')
22512257
assert_eq!(results.len(), 4);
2252-
assert_eq!(results, vec![
2253-
("hell".to_string(), 3, 1),
2254-
("hello".to_string(), 1, 1),
2255-
("help".to_string(), 2, 1),
2256-
("hero".to_string(), 4, 1)
2257-
]);
2258+
assert_eq!(
2259+
results,
2260+
vec![
2261+
("hell".to_string(), 3, 1),
2262+
("hello".to_string(), 1, 1),
2263+
("help".to_string(), 2, 1),
2264+
("hero".to_string(), 4, 1)
2265+
]
2266+
);
22582267
}
22592268

22602269
#[test]
@@ -2282,15 +2291,19 @@ mod tests {
22822291
tree.insert("hell", 3);
22832292

22842293
// Distance 0 should match exact prefixes only
2285-
let results: Vec<_> = tree.search_typo_tolerant("hel", 0)
2294+
let results: Vec<_> = tree
2295+
.search_typo_tolerant("hel", 0)
22862296
.map(|(s, k, t)| (s, *k, t))
22872297
.collect();
22882298

2289-
assert_eq!(results, vec![
2290-
("hell".to_string(), 3, 0),
2291-
("hello".to_string(), 1, 0),
2292-
("help".to_string(), 2, 0),
2293-
]);
2299+
assert_eq!(
2300+
results,
2301+
vec![
2302+
("hell".to_string(), 3, 0),
2303+
("hello".to_string(), 1, 0),
2304+
("help".to_string(), 2, 0),
2305+
]
2306+
);
22942307
}
22952308

22962309
#[test]
@@ -2302,11 +2315,19 @@ mod tests {
23022315

23032316
// Note: "café" has multi-byte chars, so byte-level distance may be > 1
23042317
// Search for "caf" should match all with low distances
2305-
let results: Vec<_> = tree.search_typo_tolerant("caf", 1)
2318+
let results: Vec<_> = tree
2319+
.search_typo_tolerant("caf", 1)
23062320
.map(|(s, k, t)| (s, *k, t))
23072321
.collect();
23082322

2309-
assert_eq!(results, vec![("café".to_string(), 1, 0), ("cake".to_string(), 2, 1), ("care".to_string(), 3, 1)]);
2323+
assert_eq!(
2324+
results,
2325+
vec![
2326+
("café".to_string(), 1, 0),
2327+
("cake".to_string(), 2, 1),
2328+
("care".to_string(), 3, 1)
2329+
]
2330+
);
23102331
}
23112332

23122333
#[test]
@@ -2317,13 +2338,19 @@ mod tests {
23172338
tree.insert("abc", 3);
23182339

23192340
// Empty search term should match all with distance 0
2320-
let results: Vec<_> = tree.search_typo_tolerant("", 1)
2341+
let results: Vec<_> = tree
2342+
.search_typo_tolerant("", 1)
23212343
.map(|(s, k, t)| (s, *k, t))
23222344
.collect();
23232345

2324-
assert_eq!(results, vec![
2325-
("a".to_string(), 1, 0), ("ab".to_string(), 2, 0), ("abc".to_string(), 3, 0)
2326-
]);
2346+
assert_eq!(
2347+
results,
2348+
vec![
2349+
("a".to_string(), 1, 0),
2350+
("ab".to_string(), 2, 0),
2351+
("abc".to_string(), 3, 0)
2352+
]
2353+
);
23272354
}
23282355

23292356
#[test]
@@ -2340,7 +2367,7 @@ mod tests {
23402367
fn test_typo_tolerant_search_iterator_lazy() {
23412368
let mut tree = RadixTree::new();
23422369
for i in 0..100 {
2343-
tree.insert(&format!("test{}", i), i);
2370+
tree.insert(&format!("test{i}"), i);
23442371
}
23452372

23462373
// Verify lazy evaluation - take only first 5
@@ -2363,44 +2390,79 @@ mod tests {
23632390
tree.insert("zz", 5);
23642391

23652392
// Single character search
2366-
let results: Vec<_> = tree.search_typo_tolerant("a", 0)
2393+
let results: Vec<_> = tree
2394+
.search_typo_tolerant("a", 0)
23672395
.map(|(s, k, t)| (s, *k, t))
23682396
.collect();
2369-
assert_eq!(results, vec![
2370-
2371-
("a".to_string(), 1, 0), ("ab".to_string(), 2, 0), ("abc".to_string(), 3, 0), ("abcd".to_string(), 4, 0)
2372-
2373-
]);
2397+
assert_eq!(
2398+
results,
2399+
vec![
2400+
("a".to_string(), 1, 0),
2401+
("ab".to_string(), 2, 0),
2402+
("abc".to_string(), 3, 0),
2403+
("abcd".to_string(), 4, 0)
2404+
]
2405+
);
23742406

23752407
// Search with distance allowing variations
2376-
let results: Vec<_> = tree.search_typo_tolerant("a", 1)
2408+
let results: Vec<_> = tree
2409+
.search_typo_tolerant("a", 1)
23772410
.map(|(s, k, t)| (s, *k, t))
23782411
.collect();
2379-
assert_eq!(results, vec![
2380-
("a".to_string(), 1, 0), ("ab".to_string(), 2, 0), ("abc".to_string(), 3, 0), ("abcd".to_string(), 4, 0), ("zz".to_string(), 5, 1)
2381-
]);
2412+
assert_eq!(
2413+
results,
2414+
vec![
2415+
("a".to_string(), 1, 0),
2416+
("ab".to_string(), 2, 0),
2417+
("abc".to_string(), 3, 0),
2418+
("abcd".to_string(), 4, 0),
2419+
("zz".to_string(), 5, 1)
2420+
]
2421+
);
23822422

23832423
// Search with distance allowing variations
2384-
let results: Vec<_> = tree.search_typo_tolerant("b", 1)
2424+
let results: Vec<_> = tree
2425+
.search_typo_tolerant("b", 1)
23852426
.map(|(s, k, t)| (s, *k, t))
23862427
.collect();
2387-
assert_eq!(results, vec![
2388-
("a".to_string(), 1, 1), ("ab".to_string(), 2, 1), ("abc".to_string(), 3, 1), ("abcd".to_string(), 4, 1), ("zz".to_string(), 5, 1)
2389-
]);
2428+
assert_eq!(
2429+
results,
2430+
vec![
2431+
("a".to_string(), 1, 1),
2432+
("ab".to_string(), 2, 1),
2433+
("abc".to_string(), 3, 1),
2434+
("abcd".to_string(), 4, 1),
2435+
("zz".to_string(), 5, 1)
2436+
]
2437+
);
23902438

2391-
let results: Vec<_> = tree.search_typo_tolerant("bb", 1)
2439+
let results: Vec<_> = tree
2440+
.search_typo_tolerant("bb", 1)
23922441
.map(|(s, k, t)| (s, *k, t))
23932442
.collect();
2394-
assert_eq!(results, vec![
2395-
("ab".to_string(), 2, 1), ("abc".to_string(), 3, 1), ("abcd".to_string(), 4, 1)
2396-
]);
2443+
assert_eq!(
2444+
results,
2445+
vec![
2446+
("ab".to_string(), 2, 1),
2447+
("abc".to_string(), 3, 1),
2448+
("abcd".to_string(), 4, 1)
2449+
]
2450+
);
23972451

2398-
let results: Vec<_> = tree.search_typo_tolerant("bb", 2)
2452+
let results: Vec<_> = tree
2453+
.search_typo_tolerant("bb", 2)
23992454
.map(|(s, k, t)| (s, *k, t))
24002455
.collect();
2401-
assert_eq!(results, vec![
2402-
("a".to_string(), 1, 2), ("ab".to_string(), 2, 1), ("abc".to_string(), 3, 1), ("abcd".to_string(), 4, 1), ("zz".to_string(), 5, 2)
2403-
]);
2456+
assert_eq!(
2457+
results,
2458+
vec![
2459+
("a".to_string(), 1, 2),
2460+
("ab".to_string(), 2, 1),
2461+
("abc".to_string(), 3, 1),
2462+
("abcd".to_string(), 4, 1),
2463+
("zz".to_string(), 5, 2)
2464+
]
2465+
);
24042466
}
24052467

24062468
#[test]
@@ -2440,7 +2502,7 @@ mod tests {
24402502
"hello" => assert_eq!(**value, 100),
24412503
"hell" => assert_eq!(**value, 200),
24422504
"help" => assert_eq!(**value, 300),
2443-
_ => panic!("Unexpected key: {}", key),
2505+
_ => panic!("Unexpected key: {key}"),
24442506
}
24452507
}
24462508
}

0 commit comments

Comments
 (0)