tui-canvas-validation-core 0.8.2

Validation core for the tui-canvas
Documentation
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// src/validation/limits.rs
//! Character limits validation implementation

use crate::ValidationResult;
use serde::{Deserialize, Serialize};
use unicode_width::UnicodeWidthStr;

/// Character limits configuration for a field
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CharacterLimits {
    /// Maximum number of characters allowed (None = unlimited)
    max_length: Option<usize>,

    /// Minimum number of characters required (None = no minimum)
    min_length: Option<usize>,

    /// Warning threshold (warn when approaching max limit)
    warning_threshold: Option<usize>,

    /// Count mode: characters vs display width
    count_mode: CountMode,
}

/// How to count characters for limit checking
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub enum CountMode {
    /// Count actual characters (default)
    #[default]
    Characters,

    /// Count display width (useful for CJK characters)
    DisplayWidth,

    /// Count bytes (rarely used, but available)
    Bytes,
}

/// Result of a character limit check
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LimitCheckResult {
    /// Within limits
    Ok,

    /// Approaching limit (warning)
    Warning { current: usize, max: usize },

    /// At or exceeding limit (error)
    Exceeded { current: usize, max: usize },

    /// Below minimum length
    TooShort { current: usize, min: usize },
}

impl CharacterLimits {
    /// Create new character limits with just max length
    pub fn new(max_length: usize) -> Self {
        Self {
            max_length: Some(max_length),
            min_length: None,
            warning_threshold: None,
            count_mode: CountMode::default(),
        }
    }

    /// Create new character limits with min and max
    pub fn new_range(min_length: usize, max_length: usize) -> Self {
        Self {
            max_length: Some(max_length),
            min_length: Some(min_length),
            warning_threshold: None,
            count_mode: CountMode::default(),
        }
    }

    /// Create new character limits with just minimum length
    pub fn new_min(min_length: usize) -> Self {
        Self {
            max_length: None,
            min_length: Some(min_length),
            warning_threshold: None,
            count_mode: CountMode::default(),
        }
    }

    /// Create new character limits with only a warning threshold.
    pub fn new_warning(threshold: usize) -> Self {
        Self {
            max_length: None,
            min_length: None,
            warning_threshold: Some(threshold),
            count_mode: CountMode::default(),
        }
    }

    /// Set warning threshold (when to show warning before hitting limit)
    pub fn with_warning_threshold(mut self, threshold: usize) -> Self {
        self.warning_threshold = Some(threshold);
        self
    }

    /// Set count mode (characters vs display width vs bytes)
    pub fn with_count_mode(mut self, mode: CountMode) -> Self {
        self.count_mode = mode;
        self
    }

    /// Get maximum length
    pub fn max_length(&self) -> Option<usize> {
        self.max_length
    }

    /// Get minimum length
    pub fn min_length(&self) -> Option<usize> {
        self.min_length
    }

    /// Get warning threshold
    pub fn warning_threshold(&self) -> Option<usize> {
        self.warning_threshold
    }

    /// Get count mode
    pub fn count_mode(&self) -> CountMode {
        self.count_mode
    }

    /// Count characters/width/bytes according to the configured mode
    fn count(&self, text: &str) -> usize {
        match self.count_mode {
            CountMode::Characters => text.chars().count(),
            CountMode::DisplayWidth => text.width(),
            CountMode::Bytes => text.len(),
        }
    }

    /// Check if inserting a character would exceed limits
    pub fn validate_insertion(
        &self,
        current_text: &str,
        position: usize,
        character: char,
    ) -> Option<ValidationResult> {
        let mut new_text = String::with_capacity(current_text.len() + character.len_utf8());
        let mut chars = current_text.chars();

        let clamped_pos = position.min(current_text.chars().count());
        for _ in 0..clamped_pos {
            if let Some(ch) = chars.next() {
                new_text.push(ch);
            }
        }

        new_text.push(character);

        for ch in chars {
            new_text.push(ch);
        }

        let new_count = self.count(&new_text);
        let current_count = self.count(current_text);

        if let Some(max) = self.max_length {
            if new_count > max {
                return Some(ValidationResult::error(format!(
                    "Character limit exceeded: {new_count}/{max}"
                )));
            }

            if let Some(warning_threshold) = self.warning_threshold {
                if new_count >= warning_threshold && current_count < warning_threshold {
                    return Some(ValidationResult::warning(format!(
                        "Approaching character limit: {new_count}/{max}"
                    )));
                }
            }
        }

        None // No validation issues
    }

    /// Validate the current content
    pub fn validate_content(&self, text: &str) -> Option<ValidationResult> {
        let count = self.count(text);

        if let Some(min) = self.min_length {
            if count < min {
                return Some(ValidationResult::warning(format!(
                    "Minimum length not met: {count}/{min}"
                )));
            }
        }

        if let Some(max) = self.max_length {
            if count > max {
                return Some(ValidationResult::error(format!(
                    "Character limit exceeded: {count}/{max}"
                )));
            }

            if let Some(warning_threshold) = self.warning_threshold {
                if count >= warning_threshold {
                    return Some(ValidationResult::warning(format!(
                        "Approaching character limit: {count}/{max}"
                    )));
                }
            }
        }

        None // No validation issues
    }

    /// Get the current status of the text against limits
    pub fn check_limits(&self, text: &str) -> LimitCheckResult {
        let count = self.count(text);

        if let Some(max) = self.max_length {
            if count > max {
                return LimitCheckResult::Exceeded {
                    current: count,
                    max,
                };
            }

            if let Some(warning_threshold) = self.warning_threshold {
                if count >= warning_threshold {
                    return LimitCheckResult::Warning {
                        current: count,
                        max,
                    };
                }
            }
        }

        // Check min length
        if let Some(min) = self.min_length {
            if count < min {
                return LimitCheckResult::TooShort {
                    current: count,
                    min,
                };
            }
        }

        LimitCheckResult::Ok
    }

    /// Get a human-readable status string
    pub fn status_text(&self, text: &str) -> Option<String> {
        match self.check_limits(text) {
            LimitCheckResult::Ok => {
                // Show current/max if we have a max limit
                self.max_length
                    .map(|max| format!("{}/{}", self.count(text), max))
            }
            LimitCheckResult::Warning { current, max } => {
                Some(format!("{current}/{max} (approaching limit)"))
            }
            LimitCheckResult::Exceeded { current, max } => {
                Some(format!("{current}/{max} (exceeded)"))
            }
            LimitCheckResult::TooShort { current, min } => Some(format!("{current}/{min} minimum")),
        }
    }
    pub fn allows_field_switch(&self, text: &str) -> bool {
        if let Some(min) = self.min_length {
            let count = self.count(text);
            // Allow switching if field is empty OR meets minimum requirement
            count == 0 || count >= min
        } else {
            true // No minimum requirement, always allow switching
        }
    }

    /// Get reason why field switching is not allowed (if any)
    pub fn field_switch_block_reason(&self, text: &str) -> Option<String> {
        if let Some(min) = self.min_length {
            let count = self.count(text);
            if count > 0 && count < min {
                return Some(format!(
                    "Field must be empty or have at least {min} characters (currently: {count})"
                ));
            }
        }
        None
    }
}

pub fn count_text(text: &str, mode: CountMode) -> usize {
    match mode {
        CountMode::Characters => text.chars().count(),
        CountMode::DisplayWidth => text.width(),
        CountMode::Bytes => text.len(),
    }
}

impl Default for CharacterLimits {
    fn default() -> Self {
        Self {
            max_length: Some(30), // Default 30 character limit as specified
            min_length: None,
            warning_threshold: None,
            count_mode: CountMode::default(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_character_limits_creation() {
        let limits = CharacterLimits::new(10);
        assert_eq!(limits.max_length(), Some(10));
        assert_eq!(limits.min_length(), None);

        let range_limits = CharacterLimits::new_range(5, 15);
        assert_eq!(range_limits.min_length(), Some(5));
        assert_eq!(range_limits.max_length(), Some(15));
    }

    #[test]
    fn test_default_limits() {
        let limits = CharacterLimits::default();
        assert_eq!(limits.max_length(), Some(30));
    }

    #[test]
    fn test_character_counting() {
        let limits = CharacterLimits::new(5);

        // Test character mode (default)
        assert_eq!(limits.count("hello"), 5);
        assert_eq!(limits.count("héllo"), 5); // Accented character counts as 1

        // Test display width mode
        let limits = limits.with_count_mode(CountMode::DisplayWidth);
        assert_eq!(limits.count("hello"), 5);

        // Test bytes mode
        let limits = limits.with_count_mode(CountMode::Bytes);
        assert_eq!(limits.count("hello"), 5);
        assert_eq!(limits.count("héllo"), 6); // é takes 2 bytes in UTF-8
    }

    #[test]
    fn test_insertion_validation() {
        let limits = CharacterLimits::new(5);

        // Valid insertion
        let result = limits.validate_insertion("test", 4, 'x');
        assert!(result.is_none()); // No validation issues

        // Invalid insertion (would exceed limit)
        let result = limits.validate_insertion("tests", 5, 'x');
        assert!(result.is_some());
        assert!(!result.unwrap().is_acceptable());
    }

    #[test]
    fn test_content_validation() {
        let limits = CharacterLimits::new_range(3, 10);

        // Too short
        let result = limits.validate_content("hi");
        assert!(result.is_some());
        assert!(result.unwrap().is_acceptable()); // Warning, not error

        // Just right
        let result = limits.validate_content("hello");
        assert!(result.is_none());

        // Too long
        let result = limits.validate_content("hello world!");
        assert!(result.is_some());
        assert!(!result.unwrap().is_acceptable()); // Error
    }

    #[test]
    fn test_warning_threshold() {
        let limits = CharacterLimits::new(10).with_warning_threshold(8);

        // Below warning threshold
        let result = limits.validate_insertion("123456", 6, 'x');
        assert!(result.is_none());

        // At warning threshold
        let result = limits.validate_insertion("1234567", 7, 'x');
        assert!(result.is_some()); // This brings us to 8 chars
        assert!(result.unwrap().is_acceptable()); // Warning, not error

        let result = limits.validate_insertion("12345678", 8, 'x');
        assert!(result.is_none());
    }

    #[test]
    fn test_status_text() {
        let limits = CharacterLimits::new(10);

        assert_eq!(limits.status_text("hello"), Some("5/10".to_string()));

        let limits = limits.with_warning_threshold(8);
        assert_eq!(
            limits.status_text("12345678"),
            Some("8/10 (approaching limit)".to_string())
        );
        assert_eq!(
            limits.status_text("1234567890x"),
            Some("11/10 (exceeded)".to_string())
        );
    }

    #[test]
    fn test_field_switch_blocking() {
        let limits = CharacterLimits::new_range(3, 10);

        // Empty field: should allow switching
        assert!(limits.allows_field_switch(""));
        assert!(limits.field_switch_block_reason("").is_none());

        // Field with content below minimum: should block switching
        assert!(!limits.allows_field_switch("hi"));
        assert!(limits.field_switch_block_reason("hi").is_some());
        assert!(limits
            .field_switch_block_reason("hi")
            .unwrap()
            .contains("at least 3 characters"));

        // Field meeting minimum: should allow switching
        assert!(limits.allows_field_switch("hello"));
        assert!(limits.field_switch_block_reason("hello").is_none());

        // Field exceeding maximum: should still allow switching (validation shows error but doesn't block)
        assert!(limits.allows_field_switch("this is way too long"));
        assert!(limits
            .field_switch_block_reason("this is way too long")
            .is_none());
    }

    #[test]
    fn test_field_switch_no_minimum() {
        let limits = CharacterLimits::new(10); // Only max, no minimum

        // Should always allow switching when there's no minimum
        assert!(limits.allows_field_switch(""));
        assert!(limits.allows_field_switch("a"));
        assert!(limits.allows_field_switch("hello"));

        assert!(limits.field_switch_block_reason("").is_none());
        assert!(limits.field_switch_block_reason("a").is_none());
    }
}