askr 0.1.7

Interactive CLI input tool with real-time validation and choice menus
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
use super::super::{PartialValidationResult, Priority, ValidationResult, Validator};
use once_cell::sync::Lazy;
use regex::Regex;
use std::net::{Ipv4Addr, Ipv6Addr};

// Email validation regex - comprehensive RFC-compliant pattern
static EMAIL_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$").unwrap()
});

// Hostname validation regex - RFC-compliant
static HOSTNAME_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$").unwrap()
});

// URL validation regex - basic but comprehensive
static URL_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"^https?://[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*(/[^\s]*)?$").unwrap()
});

/// Email address validator
#[derive(Debug)]
pub struct EmailValidator {
    priority: Priority,
    custom_message: Option<String>,
}

impl EmailValidator {
    pub fn new() -> Self {
        Self {
            priority: Priority::High,
            custom_message: None,
        }
    }

    pub fn with_priority(mut self, priority: Priority) -> Self {
        self.priority = priority;
        self
    }

    pub fn with_message(mut self, message: impl Into<String>) -> Self {
        self.custom_message = Some(message.into());
        self
    }
}

impl Validator for EmailValidator {
    fn validate(&self, input: &str) -> ValidationResult {
        if EMAIL_REGEX.is_match(input) && input.len() <= 254 {
            // RFC 5321 limit
            ValidationResult::success("email")
        } else {
            let message = self
                .custom_message
                .as_deref()
                .unwrap_or("Must be a valid email address");
            ValidationResult::failure("email", self.priority, message)
        }
    }

    fn partial_validate(&self, input: &str, _cursor_pos: usize) -> PartialValidationResult {
        if input.is_empty() {
            return PartialValidationResult::valid();
        }

        // Basic checks during typing
        if input.contains('@') {
            let parts: Vec<&str> = input.split('@').collect();
            if parts.len() > 2 {
                // Multiple @ symbols
                if let Some(pos) = input.rfind('@') {
                    return PartialValidationResult::error_at(pos);
                }
            }
        }

        PartialValidationResult::valid()
    }

    fn priority(&self) -> Priority {
        self.priority
    }

    fn name(&self) -> &str {
        "email"
    }
}

/// Hostname/domain validator
#[derive(Debug)]
pub struct HostnameValidator {
    priority: Priority,
    custom_message: Option<String>,
}

impl HostnameValidator {
    pub fn new() -> Self {
        Self {
            priority: Priority::High,
            custom_message: None,
        }
    }

    pub fn with_priority(mut self, priority: Priority) -> Self {
        self.priority = priority;
        self
    }

    pub fn with_message(mut self, message: impl Into<String>) -> Self {
        self.custom_message = Some(message.into());
        self
    }
}

impl Validator for HostnameValidator {
    fn validate(&self, input: &str) -> ValidationResult {
        if input.len() > 253 {
            // RFC 1035 limit
            let message = self
                .custom_message
                .as_deref()
                .unwrap_or("Hostname too long (max 253 characters)");
            return ValidationResult::failure("hostname", self.priority, message);
        }

        if HOSTNAME_REGEX.is_match(input) {
            ValidationResult::success("hostname")
        } else {
            let message = self
                .custom_message
                .as_deref()
                .unwrap_or("Must be a valid hostname");
            ValidationResult::failure("hostname", self.priority, message)
        }
    }

    fn partial_validate(&self, input: &str, _cursor_pos: usize) -> PartialValidationResult {
        if input.is_empty() {
            return PartialValidationResult::valid();
        }

        // Check for invalid characters
        for (i, ch) in input.char_indices() {
            if !ch.is_ascii_alphanumeric() && ch != '.' && ch != '-' {
                return PartialValidationResult::error_at(i);
            }
        }

        // Check for consecutive dots
        if input.contains("..") {
            if let Some(pos) = input.find("..") {
                return PartialValidationResult::error_at(pos);
            }
        }

        PartialValidationResult::valid()
    }

    fn priority(&self) -> Priority {
        self.priority
    }

    fn name(&self) -> &str {
        "hostname"
    }
}

/// URL validator
#[derive(Debug)]
pub struct UrlValidator {
    priority: Priority,
    custom_message: Option<String>,
}

impl UrlValidator {
    pub fn new() -> Self {
        Self {
            priority: Priority::High,
            custom_message: None,
        }
    }

    pub fn with_priority(mut self, priority: Priority) -> Self {
        self.priority = priority;
        self
    }

    pub fn with_message(mut self, message: impl Into<String>) -> Self {
        self.custom_message = Some(message.into());
        self
    }
}

impl Validator for UrlValidator {
    fn validate(&self, input: &str) -> ValidationResult {
        if URL_REGEX.is_match(input) {
            ValidationResult::success("url")
        } else {
            let message = self
                .custom_message
                .as_deref()
                .unwrap_or("Must be a valid URL (http:// or https://)");
            ValidationResult::failure("url", self.priority, message)
        }
    }

    fn partial_validate(&self, input: &str, _cursor_pos: usize) -> PartialValidationResult {
        if input.is_empty() {
            return PartialValidationResult::valid();
        }

        // Must start with http:// or https://
        if !input.starts_with("http://") && !input.starts_with("https://") {
            // If we have enough characters to rule out http/https, it's an error
            if input.len() >= 7 && !input.starts_with("http") {
                return PartialValidationResult::error_at(0);
            }
            // Also check for other common protocols that we don't support
            if input.starts_with("ftp://")
                || input.starts_with("ssh://")
                || input.starts_with("file://")
            {
                return PartialValidationResult::error_at(0);
            }
        }

        PartialValidationResult::valid()
    }

    fn priority(&self) -> Priority {
        self.priority
    }

    fn name(&self) -> &str {
        "url"
    }
}

/// IPv4 address validator
#[derive(Debug)]
pub struct Ipv4Validator {
    priority: Priority,
    custom_message: Option<String>,
}

impl Ipv4Validator {
    pub fn new() -> Self {
        Self {
            priority: Priority::High,
            custom_message: None,
        }
    }

    pub fn with_priority(mut self, priority: Priority) -> Self {
        self.priority = priority;
        self
    }

    pub fn with_message(mut self, message: impl Into<String>) -> Self {
        self.custom_message = Some(message.into());
        self
    }
}

impl Validator for Ipv4Validator {
    fn validate(&self, input: &str) -> ValidationResult {
        if input.parse::<Ipv4Addr>().is_ok() {
            ValidationResult::success("ipv4")
        } else {
            let message = self
                .custom_message
                .as_deref()
                .unwrap_or("Must be a valid IPv4 address");
            ValidationResult::failure("ipv4", self.priority, message)
        }
    }

    fn partial_validate(&self, input: &str, _cursor_pos: usize) -> PartialValidationResult {
        if input.is_empty() {
            return PartialValidationResult::valid();
        }

        // Check for invalid characters
        for (i, ch) in input.char_indices() {
            if !ch.is_ascii_digit() && ch != '.' {
                return PartialValidationResult::error_at(i);
            }
        }

        // Check for too many dots
        let dot_count = input.chars().filter(|&c| c == '.').count();
        if dot_count > 3 {
            if let Some(pos) = input.rfind('.') {
                return PartialValidationResult::error_at(pos);
            }
        }

        PartialValidationResult::valid()
    }

    fn priority(&self) -> Priority {
        self.priority
    }

    fn name(&self) -> &str {
        "ipv4"
    }
}

/// IPv6 address validator
#[derive(Debug)]
pub struct Ipv6Validator {
    priority: Priority,
    custom_message: Option<String>,
}

impl Ipv6Validator {
    pub fn new() -> Self {
        Self {
            priority: Priority::High,
            custom_message: None,
        }
    }

    pub fn with_priority(mut self, priority: Priority) -> Self {
        self.priority = priority;
        self
    }

    pub fn with_message(mut self, message: impl Into<String>) -> Self {
        self.custom_message = Some(message.into());
        self
    }
}

impl Validator for Ipv6Validator {
    fn validate(&self, input: &str) -> ValidationResult {
        if input.parse::<Ipv6Addr>().is_ok() {
            ValidationResult::success("ipv6")
        } else {
            let message = self
                .custom_message
                .as_deref()
                .unwrap_or("Must be a valid IPv6 address");
            ValidationResult::failure("ipv6", self.priority, message)
        }
    }

    fn partial_validate(&self, input: &str, _cursor_pos: usize) -> PartialValidationResult {
        if input.is_empty() {
            return PartialValidationResult::valid();
        }

        // Check for invalid characters (IPv6 uses hex digits and colons)
        for (i, ch) in input.char_indices() {
            if !ch.is_ascii_hexdigit() && ch != ':' {
                return PartialValidationResult::error_at(i);
            }
        }

        PartialValidationResult::valid()
    }

    fn priority(&self) -> Priority {
        self.priority
    }

    fn name(&self) -> &str {
        "ipv6"
    }
}

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

    #[test]
    fn test_email_validator_valid() {
        let validator = EmailValidator::new();

        let valid_emails = [
            "user@example.com",
            "test.email@domain.co.uk",
            "user+tag@example.org",
            "user_name@domain-name.com",
            "123@456.com",
            "a@b.co",
        ];

        for email in valid_emails {
            let result = validator.validate(email);
            assert!(result.passed, "Email should be valid: {}", email);
        }
    }

    #[test]
    fn test_email_validator_invalid() {
        let validator = EmailValidator::new();

        let invalid_emails = [
            "not-an-email",
            "@example.com",
            "user@",
            "user@.example.com",
            "user@example.",
            "user name@example.com",
            "",
        ];

        for email in invalid_emails {
            let result = validator.validate(email);
            assert!(!result.passed, "Email should be invalid: {}", email);
            assert!(result.message.is_some());
        }
    }

    #[test]
    fn test_hostname_validator_valid() {
        let validator = HostnameValidator::new();

        let valid_hostnames = [
            "example.com",
            "subdomain.example.com",
            "test-server.local",
            "server123.example.org",
            "a.b.c.d",
            "localhost",
        ];

        for hostname in valid_hostnames {
            let result = validator.validate(hostname);
            assert!(result.passed, "Hostname should be valid: {}", hostname);
        }
    }

    #[test]
    fn test_hostname_validator_invalid() {
        let validator = HostnameValidator::new();

        let invalid_hostnames = [
            "",
            ".example.com",
            "example..com",
            "example.com.",
            "-example.com",
            "example-.com",
            "ex ample.com",
            "very-long-hostname-that-exceeds-the-maximum-allowed-length-for-a-single-label-in-dns.com",
        ];

        for hostname in invalid_hostnames {
            let result = validator.validate(hostname);
            assert!(!result.passed, "Hostname should be invalid: {}", hostname);
        }
    }

    #[test]
    fn test_url_validator_valid() {
        let validator = UrlValidator::new();

        let valid_urls = [
            "https://example.com",
            "http://subdomain.example.org",
            "https://example.com/path/to/resource",
            "https://example.com/path",
        ];

        for url in valid_urls {
            let result = validator.validate(url);
            assert!(result.passed, "URL should be valid: {}", url);
        }
    }

    #[test]
    fn test_url_validator_invalid() {
        let validator = UrlValidator::new();

        let invalid_urls = [
            "",
            "not-a-url",
            "ftp://example.com", // Only http/https supported
            "https://",
            "http://",
            "example.com",          // Missing protocol
            "https://exam ple.com", // Space in hostname
        ];

        for url in invalid_urls {
            let result = validator.validate(url);
            assert!(!result.passed, "URL should be invalid: {}", url);
        }
    }

    #[test]
    fn test_ipv4_validator_valid() {
        let validator = Ipv4Validator::new();

        let valid_ips = [
            "192.168.1.1",
            "10.0.0.1",
            "172.16.255.254",
            "127.0.0.1",
            "0.0.0.0",
            "255.255.255.255",
        ];

        for ip in valid_ips {
            let result = validator.validate(ip);
            assert!(result.passed, "IPv4 should be valid: {}", ip);
        }
    }

    #[test]
    fn test_ipv4_validator_invalid() {
        let validator = Ipv4Validator::new();

        let invalid_ips = [
            "",
            "192.168.1",
            "192.168.1.256",
            "192.168.1.1.1",
            "192.168.1.-1",
            "192.168.01.1",   // Leading zeros
            "192.168.1.1/24", // CIDR notation
            "not.an.ip.address",
        ];

        for ip in invalid_ips {
            let result = validator.validate(ip);
            assert!(!result.passed, "IPv4 should be invalid: {}", ip);
        }
    }

    #[test]
    fn test_ipv6_validator_valid() {
        let validator = Ipv6Validator::new();

        let valid_ips = [
            "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
            "2001:db8:85a3::8a2e:370:7334",
            "::1",
            "::",
            "2001:db8::1",
            "fe80::1",
        ];

        for ip in valid_ips {
            let result = validator.validate(ip);
            assert!(result.passed, "IPv6 should be valid: {}", ip);
        }
    }

    #[test]
    fn test_ipv6_validator_invalid() {
        let validator = Ipv6Validator::new();

        let invalid_ips = [
            "",
            "192.168.1.1",                                   // IPv4
            "2001:0db8:85a3::8a2e:370g:7334",                // Invalid character 'g'
            "2001:0db8:85a3:::8a2e:370:7334",                // Triple colon
            "2001:0db8:85a3:0000:0000:8a2e:0370:7334:extra", // Too many groups
            "not:an:ipv6:address",
        ];

        for ip in invalid_ips {
            let result = validator.validate(ip);
            assert!(!result.passed, "IPv6 should be invalid: {}", ip);
        }
    }

    #[test]
    fn test_custom_messages() {
        let email_validator = EmailValidator::new().with_message("Please enter a valid email");
        let result = email_validator.validate("invalid");
        assert!(!result.passed);
        assert_eq!(result.message.unwrap(), "Please enter a valid email");

        let hostname_validator = HostnameValidator::new().with_message("Invalid hostname");
        let result = hostname_validator.validate("invalid..hostname");
        assert!(!result.passed);
        assert_eq!(result.message.unwrap(), "Invalid hostname");
    }

    #[test]
    fn test_custom_priorities() {
        let validator = EmailValidator::new().with_priority(Priority::Low);
        assert_eq!(validator.priority(), Priority::Low);

        let result = validator.validate("invalid");
        assert!(!result.passed);
        assert_eq!(result.priority, Priority::Low);
    }

    #[test]
    fn test_partial_validation() {
        let email_validator = EmailValidator::new();

        // Valid partial input
        let result = email_validator.partial_validate("user@exam", 8);
        assert!(result.first_error_pos.is_none());

        // Invalid partial input (multiple @ symbols)
        let result = email_validator.partial_validate("user@exam@ple", 10);
        assert!(result.first_error_pos.is_some());

        let url_validator = UrlValidator::new();

        // Valid partial URL
        let result = url_validator.partial_validate("https://exa", 11);
        assert!(result.first_error_pos.is_none());

        // Invalid URL start
        let result = url_validator.partial_validate("ftp://", 6);
        assert!(result.first_error_pos.is_some());
    }

    #[test]
    fn test_edge_cases() {
        // Very long email
        let long_local = "a".repeat(64);
        let long_domain = "b".repeat(63);
        let long_email = format!("{}@{}.com", long_local, long_domain);

        let email_validator = EmailValidator::new();
        let result = email_validator.validate(&long_email);
        // Should handle long emails gracefully (may pass or fail depending on implementation)

        // IPv4 edge cases
        let ipv4_validator = Ipv4Validator::new();
        assert!(ipv4_validator.validate("0.0.0.0").passed);
        assert!(ipv4_validator.validate("255.255.255.255").passed);

        // IPv6 edge cases
        let ipv6_validator = Ipv6Validator::new();
        assert!(ipv6_validator.validate("::").passed);
        assert!(ipv6_validator.validate("::1").passed);
    }

    #[test]
    fn test_international_domains() {
        let hostname_validator = HostnameValidator::new();

        // Note: These tests depend on how the hostname validator handles IDN
        // Basic ASCII domains should work
        assert!(hostname_validator.validate("example.com").passed);
        assert!(hostname_validator.validate("subdomain.example.org").passed);
    }

    #[test]
    fn test_validator_names() {
        assert_eq!(EmailValidator::new().name(), "email");
        assert_eq!(HostnameValidator::new().name(), "hostname");
        assert_eq!(UrlValidator::new().name(), "url");
        assert_eq!(Ipv4Validator::new().name(), "ipv4");
        assert_eq!(Ipv6Validator::new().name(), "ipv6");
    }

    #[test]
    fn test_validator_priorities() {
        assert_eq!(EmailValidator::new().priority(), Priority::High);
        assert_eq!(HostnameValidator::new().priority(), Priority::High);
        assert_eq!(UrlValidator::new().priority(), Priority::High);
        assert_eq!(Ipv4Validator::new().priority(), Priority::High);
        assert_eq!(Ipv6Validator::new().priority(), Priority::High);
    }
}