Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ fn main() -> Result<(), Box<dyn Error>> {
for result in &single_results {
println!(
" • Result: {} | exact: {} | key: '{}' | dist: {}",
result, result.is_exact, result.key, result.distance
result,
result.is_exact(),
result.key,
result.distance
);
}

Expand Down
6 changes: 5 additions & 1 deletion examples/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ fn main() -> Result<(), DictionaryError> {
} else {
println!("Found {} matches in {duration:?}", results.len());
for (i, result) in results.into_iter().enumerate() {
let exact_marker = if result.is_exact { " ✨ [EXACT]" } else { "" };
let exact_marker = if result.is_exact() {
" ✨ [EXACT]"
} else {
""
};
println!(" {}. {}{}", i + 1, result.key, exact_marker);
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ impl<'a> SearchBuilder<'a> {
.into_iter()
.map(|(dist, _char_diff, bytes)| {
Ok(SearchResult {
is_exact: dist == 0,
key: String::from_utf8(bytes)?,
distance: dist,
})
Expand All @@ -451,14 +450,19 @@ impl<'a> SearchBuilder<'a> {
/// A matched item from a fuzzy search.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SearchResult {
/// True if Levenshtein distance is 0.
pub is_exact: bool,
/// The matched string.
pub key: String,
/// Levenshtein distance to the query.
pub distance: u8,
}

impl SearchResult {
#[inline]
pub fn is_exact(&self) -> bool {
self.distance == 0
}
}

impl Display for SearchResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} (distance: {})", self.key, self.distance)
Expand Down
2 changes: 1 addition & 1 deletion src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ mod tests {
// "baxana" should successfully resolve to "banana"
assert_eq!(matches.len(), 1, "Expected exactly 1 match for index {}", i);
assert_eq!(matches[0].key, "banana");
assert_eq!(matches[0].is_exact, false);
assert_eq!(matches[0].is_exact(), false);
}
}
}
Expand Down
Loading