-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfuseable-search.rs
More file actions
81 lines (75 loc) · 1.94 KB
/
fuseable-search.rs
File metadata and controls
81 lines (75 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use fuse_rust::{FResult, Fuse, FuseProperty, Fuseable, FuseableSearchResult};
#[derive(Debug)]
struct Book<'a> {
title: &'a str,
author: &'a str,
}
impl Fuseable for Book<'_> {
fn properties(&self) -> Vec<FuseProperty> {
vec![
FuseProperty {
value: String::from("title"),
weight: 0.3,
},
FuseProperty {
value: String::from("author"),
weight: 0.7,
},
]
}
fn lookup(&self, key: &str) -> Option<&str> {
match key {
"title" => Some(self.title),
"author" => Some(self.author),
_ => None,
}
}
}
fn main() {
let books = [
Book {
author: "John X",
title: "Old Man's War fiction",
},
Book {
author: "P.D. Mans",
title: "Right Ho Jeeves",
},
];
let fuse = Fuse::default();
let results = fuse.search_text_in_fuse_list("man", &books);
assert_eq!(
results,
vec!(
FuseableSearchResult {
index: 1,
score: 0.015000000000000003,
results: vec!(FResult {
value: String::from("author"),
score: 0.015000000000000003,
ranges: vec!((5..8)),
}),
},
FuseableSearchResult {
index: 0,
score: 0.027999999999999997,
results: vec!(FResult {
value: String::from("title"),
score: 0.027999999999999997,
ranges: vec!((4..7)),
})
}
),
"Fuseable Search returned incorrect results"
);
results.iter().for_each(|result| {
println!(
r#"
index: {}
score: {}
results: {:?}
---------------"#,
result.index, result.score, result.results
)
});
}