fuseable_search/
fuseable-search.rs

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