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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// (C) 2025 - Enzo Lombardi
//! LookupValidator - validates input against a list of allowed values.
// LookupValidator - Validates input against a list of valid values
//
// Matches Borland: TLookupValidator (validate.h)
//
// Validates that input matches one of a predefined list of valid strings.
// Supports both case-sensitive and case-insensitive matching.
use super::validator::Validator;
/// LookupValidator - Validates against a list of valid values
///
/// Matches Borland: TLookupValidator
pub struct LookupValidator {
valid_values: Vec<String>,
case_sensitive: bool,
}
impl LookupValidator {
/// Create a new lookup validator with a list of valid values
pub fn new(valid_values: Vec<String>) -> Self {
Self {
valid_values,
case_sensitive: true,
}
}
/// Create a case-insensitive lookup validator
pub fn new_case_insensitive(valid_values: Vec<String>) -> Self {
Self {
valid_values,
case_sensitive: false,
}
}
/// Set case sensitivity
pub fn set_case_sensitive(&mut self, case_sensitive: bool) {
self.case_sensitive = case_sensitive;
}
/// Get the list of valid values
pub fn valid_values(&self) -> &[String] {
&self.valid_values
}
/// Add a valid value
pub fn add_value(&mut self, value: String) {
self.valid_values.push(value);
}
/// Remove a valid value
pub fn remove_value(&mut self, value: &str) -> bool {
if let Some(pos) = self.find_value(value) {
self.valid_values.remove(pos);
true
} else {
false
}
}
/// Find the position of a value in the list
fn find_value(&self, value: &str) -> Option<usize> {
self.valid_values.iter().position(|v| {
if self.case_sensitive {
v == value
} else {
v.eq_ignore_ascii_case(value)
}
})
}
/// Check if a value is in the list
pub fn contains(&self, value: &str) -> bool {
self.find_value(value).is_some()
}
}
impl Validator for LookupValidator {
/// Check if input is in the list of valid values
/// Matches Borland's TLookupValidator::IsValid()
fn is_valid(&self, input: &str) -> bool {
if input.is_empty() {
// Empty input is allowed - validation happens on non-empty input
return true;
}
self.contains(input)
}
/// All characters are allowed - validation is on the complete string
fn is_valid_input(&self, _input: &str, _append: bool) -> bool {
true
}
/// Display error message when validation fails
/// Matches Borland's TLookupValidator::Error()
fn error(&self) {
// In a full implementation, this would show a message box
// For now, just a no-op (the InputLine will handle visual feedback)
}
}
/// Builder for creating lookup validators with a fluent API.
///
/// # Examples
///
/// ```ignore
/// use turbo_vision::views::lookup_validator::LookupValidatorBuilder;
///
/// // Create a case-sensitive validator
/// let validator = LookupValidatorBuilder::new()
/// .values(vec!["Red".to_string(), "Green".to_string(), "Blue".to_string()])
/// .build();
///
/// // Create a case-insensitive validator
/// let validator = LookupValidatorBuilder::new()
/// .add_value("Apple")
/// .add_value("Banana")
/// .add_value("Orange")
/// .case_sensitive(false)
/// .build();
/// ```
pub struct LookupValidatorBuilder {
valid_values: Vec<String>,
case_sensitive: bool,
}
impl LookupValidatorBuilder {
/// Creates a new LookupValidatorBuilder with default values.
pub fn new() -> Self {
Self {
valid_values: Vec::new(),
case_sensitive: true,
}
}
/// Sets the list of valid values.
#[must_use]
pub fn values(mut self, valid_values: Vec<String>) -> Self {
self.valid_values = valid_values;
self
}
/// Adds a single valid value to the list.
#[must_use]
pub fn add_value(mut self, value: impl Into<String>) -> Self {
self.valid_values.push(value.into());
self
}
/// Sets whether the validator is case-sensitive (default: true).
#[must_use]
pub fn case_sensitive(mut self, case_sensitive: bool) -> Self {
self.case_sensitive = case_sensitive;
self
}
/// Builds the LookupValidator.
///
/// # Panics
///
/// Panics if no valid values have been added.
pub fn build(self) -> LookupValidator {
if self.valid_values.is_empty() {
panic!("LookupValidator must have at least one valid value");
}
LookupValidator {
valid_values: self.valid_values,
case_sensitive: self.case_sensitive,
}
}
}
impl Default for LookupValidatorBuilder {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_lookup_validator_case_sensitive() {
let validator = LookupValidator::new(vec![
"Red".to_string(),
"Green".to_string(),
"Blue".to_string(),
]);
// Valid values
assert!(validator.is_valid("Red"));
assert!(validator.is_valid("Green"));
assert!(validator.is_valid("Blue"));
// Invalid values
assert!(!validator.is_valid("red"));
assert!(!validator.is_valid("RED"));
assert!(!validator.is_valid("Yellow"));
// Empty is allowed
assert!(validator.is_valid(""));
}
#[test]
fn test_lookup_validator_case_insensitive() {
let validator = LookupValidator::new_case_insensitive(vec![
"Red".to_string(),
"Green".to_string(),
"Blue".to_string(),
]);
// Valid values (any case)
assert!(validator.is_valid("Red"));
assert!(validator.is_valid("red"));
assert!(validator.is_valid("RED"));
assert!(validator.is_valid("Green"));
assert!(validator.is_valid("green"));
assert!(validator.is_valid("BLUE"));
// Invalid values
assert!(!validator.is_valid("Yellow"));
assert!(!validator.is_valid("yellow"));
// Empty is allowed
assert!(validator.is_valid(""));
}
#[test]
fn test_lookup_validator_add_remove() {
let mut validator = LookupValidator::new(vec![
"Apple".to_string(),
"Banana".to_string(),
]);
// Initial state
assert!(validator.is_valid("Apple"));
assert!(!validator.is_valid("Orange"));
// Add value
validator.add_value("Orange".to_string());
assert!(validator.is_valid("Orange"));
// Remove value
assert!(validator.remove_value("Banana"));
assert!(!validator.is_valid("Banana"));
// Remove non-existent value
assert!(!validator.remove_value("NonExistent"));
}
#[test]
fn test_lookup_validator_contains() {
let validator = LookupValidator::new(vec![
"One".to_string(),
"Two".to_string(),
"Three".to_string(),
]);
assert!(validator.contains("One"));
assert!(validator.contains("Two"));
assert!(validator.contains("Three"));
assert!(!validator.contains("Four"));
assert!(!validator.contains("one")); // Case-sensitive
}
#[test]
fn test_lookup_validator_contains_case_insensitive() {
let validator = LookupValidator::new_case_insensitive(vec![
"One".to_string(),
"Two".to_string(),
"Three".to_string(),
]);
assert!(validator.contains("One"));
assert!(validator.contains("one"));
assert!(validator.contains("ONE"));
assert!(validator.contains("Two"));
assert!(validator.contains("two"));
assert!(!validator.contains("Four"));
}
#[test]
fn test_lookup_validator_set_case_sensitive() {
let mut validator = LookupValidator::new(vec![
"Test".to_string(),
]);
// Initially case-sensitive
assert!(validator.is_valid("Test"));
assert!(!validator.is_valid("test"));
// Switch to case-insensitive
validator.set_case_sensitive(false);
assert!(validator.is_valid("Test"));
assert!(validator.is_valid("test"));
assert!(validator.is_valid("TEST"));
// Switch back to case-sensitive
validator.set_case_sensitive(true);
assert!(validator.is_valid("Test"));
assert!(!validator.is_valid("test"));
}
#[test]
fn test_lookup_validator_valid_values() {
let validator = LookupValidator::new(vec![
"A".to_string(),
"B".to_string(),
"C".to_string(),
]);
let values = validator.valid_values();
assert_eq!(values.len(), 3);
assert_eq!(values[0], "A");
assert_eq!(values[1], "B");
assert_eq!(values[2], "C");
}
#[test]
fn test_lookup_validator_is_valid_input() {
let validator = LookupValidator::new(vec!["Test".to_string()]);
// All characters are allowed (validation is on complete string)
assert!(validator.is_valid_input("a", true));
assert!(validator.is_valid_input("Z", true));
assert!(validator.is_valid_input("0", true));
assert!(validator.is_valid_input(" ", true));
assert!(validator.is_valid_input("!", true));
}
#[test]
fn test_lookup_validator_builder() {
let validator = LookupValidatorBuilder::new()
.add_value("Red")
.add_value("Green")
.add_value("Blue")
.build();
assert!(validator.is_valid("Red"));
assert!(validator.is_valid("Green"));
assert!(validator.is_valid("Blue"));
assert!(!validator.is_valid("Yellow"));
}
#[test]
fn test_lookup_validator_builder_case_insensitive() {
let validator = LookupValidatorBuilder::new()
.values(vec!["One".to_string(), "Two".to_string()])
.case_sensitive(false)
.build();
assert!(validator.is_valid("One"));
assert!(validator.is_valid("one"));
assert!(validator.is_valid("TWO"));
}
#[test]
#[should_panic(expected = "must have at least one valid value")]
fn test_lookup_validator_builder_empty() {
LookupValidatorBuilder::new().build();
}
}