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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! Builder pattern for creating Transducer instances.
//!
//! The `TransducerBuilder` provides a fluent API for constructing
//! `Transducer` instances with optional configuration and validation.
use crate::transducer::{Algorithm, Transducer};
use libdictenstein::Dictionary;
/// Builder for constructing a `Transducer` with a fluent API.
///
/// # Example
///
/// ```rust,ignore
/// use liblevenshtein::prelude::*;
///
/// let dict = DoubleArrayTrie::from_terms(vec!["test", "testing"]);
/// let transducer = TransducerBuilder::new()
/// .dictionary(dict)
/// .algorithm(Algorithm::Transposition)
/// .build()?;
/// ```
pub struct TransducerBuilder<D: Dictionary> {
dictionary: Option<D>,
algorithm: Option<Algorithm>,
}
/// Error type for builder validation failures.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum BuilderError {
/// No dictionary was provided
#[error("Dictionary is required. Use .dictionary() to set it.")]
MissingDictionary,
/// No algorithm was provided
#[error("Algorithm is required. Use .algorithm() to set it.")]
MissingAlgorithm,
}
impl<D: Dictionary> TransducerBuilder<D> {
/// Create a new empty builder.
pub fn new() -> Self {
TransducerBuilder {
dictionary: None,
algorithm: None,
}
}
/// Set the dictionary to use for approximate string matching.
///
/// # Arguments
///
/// * `dictionary` - The dictionary containing the terms to match against
///
/// # Example
///
/// ```rust,ignore
/// let dict = DoubleArrayTrie::from_terms(vec!["test"]);
/// let builder = TransducerBuilder::new()
/// .dictionary(dict);
/// ```
pub fn dictionary(mut self, dictionary: D) -> Self {
self.dictionary = Some(dictionary);
self
}
/// Set the Levenshtein distance algorithm to use.
///
/// # Arguments
///
/// * `algorithm` - The distance algorithm (Standard, Transposition, or MergeAndSplit)
///
/// # Example
///
/// ```rust,ignore
/// let builder = TransducerBuilder::new()
/// .algorithm(Algorithm::Transposition);
/// ```
pub fn algorithm(mut self, algorithm: Algorithm) -> Self {
self.algorithm = Some(algorithm);
self
}
/// Build the `Transducer`.
///
/// # Returns
///
/// * `Ok(Transducer)` if all required fields are set
/// * `Err(BuilderError)` if any required fields are missing
///
/// # Errors
///
/// Returns an error if:
/// - Dictionary was not set (use `.dictionary()`)
/// - Algorithm was not set (use `.algorithm()`)
///
/// # Example
///
/// ```rust,ignore
/// let transducer = TransducerBuilder::new()
/// .dictionary(dict)
/// .algorithm(Algorithm::Standard)
/// .build()?;
/// ```
pub fn build(self) -> Result<Transducer<D>, BuilderError> {
let dictionary = self.dictionary.ok_or(BuilderError::MissingDictionary)?;
let algorithm = self.algorithm.ok_or(BuilderError::MissingAlgorithm)?;
Ok(Transducer::new(dictionary, algorithm))
}
}
impl<D: Dictionary> Default for TransducerBuilder<D> {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use libdictenstein::double_array_trie::DoubleArrayTrie;
#[test]
fn test_builder_complete() {
let dict = DoubleArrayTrie::from_terms(vec!["test", "testing"]);
let transducer = TransducerBuilder::new()
.dictionary(dict)
.algorithm(Algorithm::Standard)
.build()
.expect("test fixture: builder with dict and algorithm");
assert_eq!(transducer.algorithm(), Algorithm::Standard);
assert_eq!(transducer.dictionary().len(), Some(2));
}
#[test]
fn test_builder_missing_dictionary() {
let result: Result<Transducer<DoubleArrayTrie>, _> = TransducerBuilder::new()
.algorithm(Algorithm::Standard)
.build();
assert!(result.is_err());
assert_eq!(result.unwrap_err(), BuilderError::MissingDictionary);
}
#[test]
fn test_builder_missing_algorithm() {
let dict = DoubleArrayTrie::from_terms(vec!["test"]);
let result = TransducerBuilder::new().dictionary(dict).build();
assert!(result.is_err());
assert_eq!(result.unwrap_err(), BuilderError::MissingAlgorithm);
}
#[test]
fn test_builder_order_independence() {
let dict1 = DoubleArrayTrie::from_terms(vec!["test"]);
let dict2 = DoubleArrayTrie::from_terms(vec!["test"]);
// Algorithm first
let t1 = TransducerBuilder::new()
.algorithm(Algorithm::Transposition)
.dictionary(dict1)
.build()
.expect("test fixture: builder with dict and algorithm");
// Dictionary first
let t2 = TransducerBuilder::new()
.dictionary(dict2)
.algorithm(Algorithm::Transposition)
.build()
.expect("test fixture: builder with dict and algorithm");
assert_eq!(t1.algorithm(), t2.algorithm());
}
#[test]
fn test_builder_all_algorithms() {
for algo in [
Algorithm::Standard,
Algorithm::Transposition,
Algorithm::MergeAndSplit,
] {
let dict = DoubleArrayTrie::from_terms(vec!["test"]);
let transducer = TransducerBuilder::new()
.dictionary(dict)
.algorithm(algo)
.build()
.expect("test fixture: builder with dict and algorithm");
assert_eq!(transducer.algorithm(), algo);
}
}
#[test]
fn test_builder_with_double_array_trie() {
use libdictenstein::double_array_trie::DoubleArrayTrie;
let dict = DoubleArrayTrie::from_terms(vec!["test", "testing"]);
let transducer = TransducerBuilder::new()
.dictionary(dict)
.algorithm(Algorithm::Standard)
.build()
.expect("test fixture: builder with dict and algorithm");
let results: Vec<_> = transducer.query("test", 0).collect();
assert_eq!(results.len(), 1);
assert_eq!(results[0], "test");
}
#[test]
fn test_builder_error_display() {
let err1 = BuilderError::MissingDictionary;
let err2 = BuilderError::MissingAlgorithm;
assert!(err1.to_string().contains("Dictionary"));
assert!(err2.to_string().contains("Algorithm"));
}
}