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
//! Trait for polymorphic query iterator results.
//!
//! This module provides the `QueryResult` trait that allows `QueryIterator`
//! to return different result types (just the term, or term + distance) without
//! code duplication or performance overhead.
//!
//! This design mirrors the C++ template specialization approach and Java's
//! factory pattern, providing zero-cost abstraction through Rust's
//! monomorphization.
use Candidate;
/// Trait for converting a match (term + distance) into a result type.
///
/// This enables `QueryIterator<N, R>` to be generic over the result type,
/// allowing it to return either:
/// - Just the term (`String`)
/// - Term with distance (`Candidate`)
/// - Custom user-defined types
///
/// The distance is computed once during automaton traversal, then converted
/// to the appropriate result type via this trait.
///
/// # Examples
///
/// ```no_run
/// use liblevenshtein::prelude::*;
/// use liblevenshtein::transducer::{QueryIterator, Candidate};
///
/// let dict = DoubleArrayTrie::from_terms(vec!["test", "testing"]);
/// let root = dict.root();
///
/// // Iterator that returns just strings
/// let iter: QueryIterator<_, String> = QueryIterator::new(
/// root.clone(),
/// "tset".to_string(),
/// 2,
/// Algorithm::Standard
/// );
/// for term in iter {
/// println!("{}", term);
/// }
///
/// // Iterator that returns Candidate (term + distance)
/// let iter: QueryIterator<_, Candidate> = QueryIterator::new(
/// root,
/// "tset".to_string(),
/// 2,
/// Algorithm::Standard
/// );
/// for candidate in iter {
/// println!("{}: distance {}", candidate.term, candidate.distance);
/// }
/// ```
/// Implementation for String: returns just the term, ignoring distance.
/// Implementation for Candidate: returns both term and distance.