Struct fuse_rust::Fuse

source ·
pub struct Fuse {
    pub location: i32,
    pub distance: i32,
    pub threshold: f64,
    pub max_pattern_length: i32,
    pub is_case_sensitive: bool,
    pub tokenize: bool,
}
Expand description

Creates a new fuse object with given config settings Use to create patterns and access the search methods. Also implements a default method to quickly get a fuse object ready with the default config.

Examples:

Basic Usage:

let fuse = Fuse{
    location: 0,
    distance: 100,
    threshold: 0.6,
    max_pattern_length: 32,
    is_case_sensitive: false,
    tokenize: false,
};

Fields§

§location: i32

location to starting looking for patterns

§distance: i32

maximum distance to look away from the location

§threshold: f64

threshold for the search algorithm to give up at, 0.0 is perfect match 1.0 is imperfect match

§max_pattern_length: i32

maximum allowed pattern length

§is_case_sensitive: bool

check for lowercase and uppercase seperately

§tokenize: bool

tokenize search patterns

Implementations§

source§

impl Fuse

source

pub fn create_pattern(&self, string: &str) -> Option<Pattern>

Creates a pattern object from input string.

  • Parameter string: A string from which to create the pattern object
  • Returns: A tuple containing pattern metadata
source

pub fn search( &self, pattern: Option<&Pattern>, string: &str ) -> Option<ScoreResult>

Searches for a pattern in a given string.

  • Parameters:
    • pattern: The pattern to search for. This is created by calling createPattern
    • string: The string in which to search for the pattern
  • Returns: Some(ScoreResult) if a match is found containing a score between 0.0 (exact match) and 1 (not a match), and ranges of the matched characters. If no match is found or if search pattern was empty will return None.
Example:
use fuse_rust::{ Fuse };
let fuse = Fuse::default();
let pattern = fuse.create_pattern("some text");
fuse.search(pattern.as_ref(), "some string");
source§

impl Fuse

source

pub fn search_text_in_string( &self, text: &str, string: &str ) -> Option<ScoreResult>

Searches for a text pattern in a given string.

  • Parameters:
    • text: the text string to search for.
    • string: The string in which to search for the pattern
  • Returns: Some(ScoreResult) if a match is found, containing a score between 0.0 (exact match) and 1 (not a match), and ranges of the matched characters. Otherwise if a match is not found, returns None.
Examples:
use fuse_rust::{ Fuse };
let fuse = Fuse::default();
fuse.search_text_in_string("some text", "some string");

Note: if the same text needs to be searched across many strings, consider creating the pattern once via createPattern, and then use the other search function. This will improve performance, as the pattern object would only be created once, and re-used across every search call:

use fuse_rust::{ Fuse };
let fuse = Fuse::default();
let pattern = fuse.create_pattern("some text");
fuse.search(pattern.as_ref(), "some string");
fuse.search(pattern.as_ref(), "another string");
fuse.search(pattern.as_ref(), "yet another string");
Examples found in repository?
examples/simple-search.rs (line 8)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn main() {
    let fuse = Fuse::default();
    let text = "Old Man's War";
    let search_text = "od mn war";

    let result = fuse.search_text_in_string(search_text, text);
    assert_eq!(
        result,
        Some(ScoreResult {
            score: 0.4444444444444444,
            ranges: vec!((0..1), (2..7), (9..13)),
        }),
        "Simple search returned incorrect results"
    );
    dbg!(result);
}
source

pub fn search_text_in_iterable<It>( &self, text: &str, list: It ) -> Vec<SearchResult>where It: IntoIterator, It::Item: AsRef<str>,

Searches for a text pattern in an iterable containing string references.

  • Parameters:
    • text: The pattern string to search for
    • list: Iterable over string references
  • Returns: Vec containing Search results corresponding to matches found, with its index, its score, and the ranges of the matched characters.
Example:
use fuse_rust::{ Fuse };
let fuse = Fuse::default();
let books = [
    "The Silmarillion",
    "The Lock Artist",
    "The Lost Symbol"
];

let results = fuse.search_text_in_iterable("Te silm", books.iter());
Examples found in repository?
examples/iterable-search.rs (line 7)
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
fn main() {
    let fuse = Fuse::default();
    let books = ["The Silmarillion", "The Lock Artist", "The Lost Symbol"];

    let results = fuse.search_text_in_iterable("Te silm", books.iter());
    assert_eq!(
        results,
        vec!(
            SearchResult {
                index: 0,
                score: 0.14285714285714285,
                ranges: vec!((0..1), (2..8), (10..14)),
            },
            SearchResult {
                index: 2,
                score: 0.49857142857142855,
                ranges: vec!((0..1), (2..5), (6..10), (11..12), (14..15)),
            },
            SearchResult {
                index: 1,
                score: 0.5714285714285714,
                ranges: vec!((0..1), (2..5), (8..9), (11..15)),
            },
        ),
        "Iterable search returned incorrect results"
    );
    dbg!(results);
}
source

pub fn search_text_in_fuse_list( &self, text: &str, list: &[impl Fuseable] ) -> Vec<FuseableSearchResult>

Searches for a text pattern in an array of Fuseable objects.

  • Parameters:
    • text: The pattern string to search for
    • list: A list of Fuseable objects, i.e. structs implementing the Fuseable trait in which to search
  • Returns: A list of FuseableSearchResult objects Each Fuseable object contains a properties method which returns FuseProperty array. Each FuseProperty is a struct containing a value (the name of the field which should be included in the search), and a weight (how much “weight” to assign to the score)
Example

struct Book<'a> {
   title: &'a str,
   author: &'a str,
}

impl Fuseable for Book<'_>{
    fn properties(&self) -> Vec<FuseProperty> {
        return vec!(
            FuseProperty{value: String::from("title"), weight: 0.3},
            FuseProperty{value: String::from("author"), weight: 0.7},
        )
    }

    fn lookup(&self, key: &str) -> Option<&str> {
        return match key {
            "title" => Some(self.title),
            "author" => Some(self.author),
            _ => None
        }
    }
}   
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);
Examples found in repository?
examples/fuseable-search.rs (line 44)
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
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
        )
    });
}

Trait Implementations§

source§

impl Default for Fuse

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Fuse

§

impl Send for Fuse

§

impl Sync for Fuse

§

impl Unpin for Fuse

§

impl UnwindSafe for Fuse

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.