envro 0.11.0

A crate to load environment variables from a .env file into the process environment variables
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
//! Composable value validation.
//!
//! Validation is **decoupled** from `.env` loading: it works on any
//! `HashMap<String, String>` ([`EnvroVars`]) and on the live process
//! environment. Use whichever entry point matches where your env vars come
//! from:
//!
//! - [`validate`] — validate any already-built `EnvroVars` map (from a `.env`
//!   file, a CLI, a config service, tests, …).
//! - [`validate_env`] — validate the current process environment directly.
//! - [`load_dotenv_validated`] — one-shot helper: load a `.env` file and
//!   validate the resulting map.
//!
//! # Examples
//!
//! Validate an arbitrary map:
//!
//! ```
//! use envro::*;
//!
//! let mut vars = EnvroVars::new();
//! vars.insert("APP_NAME".to_string(), "envro".to_string());
//! vars.insert("PORT".to_string(), "8080".to_string());
//! vars.insert("LOG_LEVEL".to_string(), "info".to_string());
//!
//! let schema = Schema::new()
//!     .field("APP_NAME", Field::required().min_len(3).max_len(64))
//!     .field("PORT", Field::required().port())
//!     .field("ADMIN_EMAIL", Field::optional().email())
//!     .field("LOG_LEVEL", Field::required().one_of(&["debug", "info", "warn", "error"]));
//!
//! validate(&vars, &schema).unwrap();
//! ```

use std::net::{IpAddr, Ipv4Addr};
use std::path::Path;
use std::str::FromStr;

use crate::{load_dotenv, EnvroError, EnvroVars};

/// A single validation failure for one key (or list element).
#[derive(Debug, Clone)]
pub struct ValidationIssue {
    /// Env var name; list elements use `KEY[i]`.
    pub key: String,
    /// Rule name that failed (e.g. `required`, `min_len`, `port`).
    pub rule: &'static str,
    /// Human-readable reason.
    pub reason: String,
}

impl std::fmt::Display for ValidationIssue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}[{}]: {}", self.key, self.rule, self.reason)
    }
}

/// Format a list of issues for [`EnvroError::Validation`]'s `Display`.
pub(crate) fn format_issues(issues: &[ValidationIssue]) -> String {
    issues
        .iter()
        .map(|i| i.to_string())
        .collect::<Vec<_>>()
        .join("; ")
}

#[derive(Debug, Clone, Copy)]
enum Presence {
    Required,
    Optional,
}

#[derive(Debug)]
enum Rule {
    // string shape
    MinLen(usize),
    MaxLen(usize),
    ExactLen(usize),
    Alpha,
    Alphanumeric,
    Digits,
    Ascii,
    Lowercase,
    Uppercase,
    StartsWith(String),
    EndsWith(String),
    Contains(String),
    OneOf(Vec<String>),
    NotOneOf(Vec<String>),
    // numbers
    Integer,
    PositiveInteger,
    NonNegativeInteger,
    Float,
    PositiveFloat,
    NonNegativeFloat,
    IntRange(i64, i64),
    FloatRange(f64, f64),
    Port,
    // boolean
    Boolean,
    // formats
    Email,
    Url,
    Uuid,
    Ipv4,
    Ip,
    Hex,
    // list
    List {
        delim: char,
        item: Box<Field>,
        min_items: Option<usize>,
        max_items: Option<usize>,
    },
}

/// A composable field spec.
///
/// Start with [`Field::required`] or [`Field::optional`], then chain rules.
/// See the module docs for an example.
#[derive(Debug)]
pub struct Field {
    presence: Presence,
    rules: Vec<Rule>,
}

impl Field {
    /// Field must be present in the map and have a non-empty value.
    pub fn required() -> Self {
        Self {
            presence: Presence::Required,
            rules: Vec::new(),
        }
    }

    /// Field may be missing or empty; other rules are skipped when so.
    pub fn optional() -> Self {
        Self {
            presence: Presence::Optional,
            rules: Vec::new(),
        }
    }

    // --- string shape ---

    /// Minimum length in characters (not bytes).
    pub fn min_len(mut self, n: usize) -> Self {
        self.rules.push(Rule::MinLen(n));
        self
    }
    /// Maximum length in characters (not bytes).
    pub fn max_len(mut self, n: usize) -> Self {
        self.rules.push(Rule::MaxLen(n));
        self
    }
    /// Exact length in characters (not bytes).
    pub fn exact_len(mut self, n: usize) -> Self {
        self.rules.push(Rule::ExactLen(n));
        self
    }
    /// Only alphabetic characters.
    pub fn alpha(mut self) -> Self {
        self.rules.push(Rule::Alpha);
        self
    }
    /// Only alphanumeric characters.
    pub fn alphanumeric(mut self) -> Self {
        self.rules.push(Rule::Alphanumeric);
        self
    }
    /// Only ASCII digits `0-9`.
    pub fn digits(mut self) -> Self {
        self.rules.push(Rule::Digits);
        self
    }
    /// Only ASCII characters.
    pub fn ascii(mut self) -> Self {
        self.rules.push(Rule::Ascii);
        self
    }
    /// No uppercase characters.
    pub fn lowercase(mut self) -> Self {
        self.rules.push(Rule::Lowercase);
        self
    }
    /// No lowercase characters.
    pub fn uppercase(mut self) -> Self {
        self.rules.push(Rule::Uppercase);
        self
    }
    /// Must start with the given substring.
    pub fn starts_with(mut self, s: impl Into<String>) -> Self {
        self.rules.push(Rule::StartsWith(s.into()));
        self
    }
    /// Must end with the given substring.
    pub fn ends_with(mut self, s: impl Into<String>) -> Self {
        self.rules.push(Rule::EndsWith(s.into()));
        self
    }
    /// Must contain the given substring.
    pub fn contains(mut self, s: impl Into<String>) -> Self {
        self.rules.push(Rule::Contains(s.into()));
        self
    }
    /// Must equal one of the given options.
    pub fn one_of<S: AsRef<str>>(mut self, options: &[S]) -> Self {
        self.rules.push(Rule::OneOf(
            options.iter().map(|s| s.as_ref().to_string()).collect(),
        ));
        self
    }
    /// Must not equal any of the given options.
    pub fn not_one_of<S: AsRef<str>>(mut self, options: &[S]) -> Self {
        self.rules.push(Rule::NotOneOf(
            options.iter().map(|s| s.as_ref().to_string()).collect(),
        ));
        self
    }

    // --- numbers ---

    /// Parses as `i64`.
    pub fn integer(mut self) -> Self {
        self.rules.push(Rule::Integer);
        self
    }
    /// Parses as `i64` and is `> 0`.
    pub fn positive_integer(mut self) -> Self {
        self.rules.push(Rule::PositiveInteger);
        self
    }
    /// Parses as `i64` and is `>= 0`.
    pub fn non_negative_integer(mut self) -> Self {
        self.rules.push(Rule::NonNegativeInteger);
        self
    }
    /// Parses as `f64`.
    pub fn float(mut self) -> Self {
        self.rules.push(Rule::Float);
        self
    }
    /// Parses as `f64` and is `> 0.0`.
    pub fn positive_float(mut self) -> Self {
        self.rules.push(Rule::PositiveFloat);
        self
    }
    /// Parses as `f64` and is `>= 0.0`.
    pub fn non_negative_float(mut self) -> Self {
        self.rules.push(Rule::NonNegativeFloat);
        self
    }
    /// Parses as `i64` and is within `[min, max]` (inclusive).
    pub fn int_range(mut self, min: i64, max: i64) -> Self {
        self.rules.push(Rule::IntRange(min, max));
        self
    }
    /// Parses as `f64` and is within `[min, max]` (inclusive).
    pub fn float_range(mut self, min: f64, max: f64) -> Self {
        self.rules.push(Rule::FloatRange(min, max));
        self
    }
    /// TCP/UDP port: integer in `1..=65535`.
    pub fn port(mut self) -> Self {
        self.rules.push(Rule::Port);
        self
    }

    // --- boolean ---

    /// One of `true`/`false`/`1`/`0`/`yes`/`no` (case-insensitive).
    pub fn boolean(mut self) -> Self {
        self.rules.push(Rule::Boolean);
        self
    }

    // --- formats (std-only, best-effort) ---

    /// `local@domain`, no whitespace, both sides non-empty. Best-effort, not RFC 5322.
    pub fn email(mut self) -> Self {
        self.rules.push(Rule::Email);
        self
    }
    /// Must start with `http://` or `https://` and have a non-empty, whitespace-free remainder.
    pub fn url(mut self) -> Self {
        self.rules.push(Rule::Url);
        self
    }
    /// Canonical 8-4-4-4-12 hex form.
    pub fn uuid(mut self) -> Self {
        self.rules.push(Rule::Uuid);
        self
    }
    /// Parses via `std::net::Ipv4Addr`.
    pub fn ipv4(mut self) -> Self {
        self.rules.push(Rule::Ipv4);
        self
    }
    /// Parses via `std::net::IpAddr` (v4 or v6).
    pub fn ip(mut self) -> Self {
        self.rules.push(Rule::Ip);
        self
    }
    /// Hex string, optional `0x`/`0X` prefix; remainder must be non-empty hex digits.
    pub fn hex(mut self) -> Self {
        self.rules.push(Rule::Hex);
        self
    }

    // --- list ---

    /// Split the value on `delim` (trimming each part) and apply `item`'s rules to
    /// each element. Element failures are reported with key `KEY[i]`.
    ///
    /// When `item` is [`Field::optional`], empty parts skip the item rules; when
    /// [`Field::required`], empty parts fail the `required` rule.
    pub fn list(mut self, delim: char, item: Field) -> Self {
        self.rules.push(Rule::List {
            delim,
            item: Box::new(item),
            min_items: None,
            max_items: None,
        });
        self
    }
    /// Minimum number of items in the most recently added [`Field::list`].
    ///
    /// Has no effect if the most recent rule is not a list.
    pub fn min_items(mut self, n: usize) -> Self {
        if let Some(Rule::List { min_items, .. }) = self.rules.last_mut() {
            *min_items = Some(n);
        }
        self
    }
    /// Maximum number of items in the most recently added [`Field::list`].
    ///
    /// Has no effect if the most recent rule is not a list.
    pub fn max_items(mut self, n: usize) -> Self {
        if let Some(Rule::List { max_items, .. }) = self.rules.last_mut() {
            *max_items = Some(n);
        }
        self
    }
}

/// A collection of `(key, Field)` specs.
#[derive(Debug, Default)]
pub struct Schema {
    fields: Vec<(String, Field)>,
}

impl Schema {
    /// Create an empty schema.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a field spec for `name`.
    pub fn field(mut self, name: impl Into<String>, field: Field) -> Self {
        self.fields.push((name.into(), field));
        self
    }
}

/// Validate an arbitrary `EnvroVars` map against a schema.
///
/// This function is **completely decoupled from any I/O**: `vars` can come
/// from [`load_dotenv`], a CLI, a config service, test fixtures, or anywhere
/// else that produces a `HashMap<String, String>`.
///
/// Collects every failing rule across every field before returning.
/// Unknown keys in `vars` (not in the schema) are ignored.
///
/// See [`validate_env`] to validate the live process environment directly.
pub fn validate(vars: &EnvroVars, schema: &Schema) -> Result<(), EnvroError> {
    let mut issues = Vec::new();
    for (key, field) in &schema.fields {
        validate_field(key, field, vars.get(key).map(String::as_str), &mut issues);
    }
    if issues.is_empty() {
        Ok(())
    } else {
        Err(EnvroError::Validation { errors: issues })
    }
}

/// Load a `.env` file and validate it in one step.
pub fn load_dotenv_validated(path: &Path, schema: &Schema) -> Result<EnvroVars, EnvroError> {
    let vars = load_dotenv(path)?;
    validate(&vars, schema)?;
    Ok(vars)
}

/// Validate the **process environment** against a schema.
///
/// This is completely decoupled from `.env` file loading: for every key in the
/// schema, the value is read from `std::env::var(key)`. Missing keys or read
/// errors are treated as "not present" (same semantics as [`validate`] on a
/// `HashMap` where the key is absent).
///
/// Use this when your env vars come from anywhere other than a `.env` file
/// (shell exports, systemd unit, container runtime, `env::set_var`, tests, …).
pub fn validate_env(schema: &Schema) -> Result<(), EnvroError> {
    let mut issues = Vec::new();
    for (key, field) in &schema.fields {
        let value = std::env::var(key).ok();
        validate_field(key, field, value.as_deref(), &mut issues);
    }
    if issues.is_empty() {
        Ok(())
    } else {
        Err(EnvroError::Validation { errors: issues })
    }
}

fn validate_field(key: &str, field: &Field, value: Option<&str>, out: &mut Vec<ValidationIssue>) {
    let present = matches!(value, Some(v) if !v.is_empty());
    if !present {
        if matches!(field.presence, Presence::Required) {
            out.push(ValidationIssue {
                key: key.to_string(),
                rule: "required",
                reason: "value is missing or empty".to_string(),
            });
        }
        return;
    }
    let value = value.unwrap();
    for rule in &field.rules {
        check_rule(key, value, rule, out);
    }
}

fn check_rule(key: &str, value: &str, rule: &Rule, out: &mut Vec<ValidationIssue>) {
    let issue = |name: &'static str, reason: String| ValidationIssue {
        key: key.to_string(),
        rule: name,
        reason,
    };
    match rule {
        Rule::MinLen(n) => {
            let l = value.chars().count();
            if l < *n {
                out.push(issue("min_len", format!("length {} < {}", l, n)));
            }
        }
        Rule::MaxLen(n) => {
            let l = value.chars().count();
            if l > *n {
                out.push(issue("max_len", format!("length {} > {}", l, n)));
            }
        }
        Rule::ExactLen(n) => {
            let l = value.chars().count();
            if l != *n {
                out.push(issue("exact_len", format!("length {} != {}", l, n)));
            }
        }
        Rule::Alpha => {
            if !value.chars().all(|c| c.is_alphabetic()) {
                out.push(issue("alpha", "non-alphabetic character".to_string()));
            }
        }
        Rule::Alphanumeric => {
            if !value.chars().all(|c| c.is_alphanumeric()) {
                out.push(issue(
                    "alphanumeric",
                    "non-alphanumeric character".to_string(),
                ));
            }
        }
        Rule::Digits => {
            if !value.chars().all(|c| c.is_ascii_digit()) {
                out.push(issue("digits", "non-digit character".to_string()));
            }
        }
        Rule::Ascii => {
            if !value.is_ascii() {
                out.push(issue("ascii", "non-ASCII character".to_string()));
            }
        }
        Rule::Lowercase => {
            if value.chars().any(|c| c.is_uppercase()) {
                out.push(issue("lowercase", "contains uppercase".to_string()));
            }
        }
        Rule::Uppercase => {
            if value.chars().any(|c| c.is_lowercase()) {
                out.push(issue("uppercase", "contains lowercase".to_string()));
            }
        }
        Rule::StartsWith(s) => {
            if !value.starts_with(s.as_str()) {
                out.push(issue("starts_with", format!("does not start with {s:?}")));
            }
        }
        Rule::EndsWith(s) => {
            if !value.ends_with(s.as_str()) {
                out.push(issue("ends_with", format!("does not end with {s:?}")));
            }
        }
        Rule::Contains(s) => {
            if !value.contains(s.as_str()) {
                out.push(issue("contains", format!("does not contain {s:?}")));
            }
        }
        Rule::OneOf(opts) => {
            if !opts.iter().any(|o| o == value) {
                out.push(issue("one_of", format!("not one of {opts:?}")));
            }
        }
        Rule::NotOneOf(opts) => {
            if opts.iter().any(|o| o == value) {
                out.push(issue("not_one_of", format!("value is in {opts:?}")));
            }
        }
        Rule::Integer => {
            if crate::coerce::parse_i64(value).is_err() {
                out.push(issue("integer", "not an integer".to_string()));
            }
        }
        Rule::PositiveInteger => match crate::coerce::parse_i64(value) {
            Ok(n) if n > 0 => {}
            Ok(_) => out.push(issue("positive_integer", "must be > 0".to_string())),
            Err(_) => out.push(issue("positive_integer", "not an integer".to_string())),
        },
        Rule::NonNegativeInteger => match crate::coerce::parse_i64(value) {
            Ok(n) if n >= 0 => {}
            Ok(_) => out.push(issue("non_negative_integer", "must be >= 0".to_string())),
            Err(_) => out.push(issue("non_negative_integer", "not an integer".to_string())),
        },
        Rule::Float => {
            if crate::coerce::parse_f64(value).is_err() {
                out.push(issue("float", "not a float".to_string()));
            }
        }
        Rule::PositiveFloat => match crate::coerce::parse_f64(value) {
            Ok(n) if n > 0.0 => {}
            Ok(_) => out.push(issue("positive_float", "must be > 0".to_string())),
            Err(_) => out.push(issue("positive_float", "not a float".to_string())),
        },
        Rule::NonNegativeFloat => match crate::coerce::parse_f64(value) {
            Ok(n) if n >= 0.0 => {}
            Ok(_) => out.push(issue("non_negative_float", "must be >= 0".to_string())),
            Err(_) => out.push(issue("non_negative_float", "not a float".to_string())),
        },
        Rule::IntRange(min, max) => match crate::coerce::parse_i64(value) {
            Ok(n) if n >= *min && n <= *max => {}
            Ok(n) => out.push(issue(
                "int_range",
                format!("{} not in [{},{}]", n, min, max),
            )),
            Err(_) => out.push(issue("int_range", "not an integer".to_string())),
        },
        Rule::FloatRange(min, max) => match crate::coerce::parse_f64(value) {
            Ok(n) if n >= *min && n <= *max => {}
            Ok(n) => out.push(issue(
                "float_range",
                format!("{} not in [{},{}]", n, min, max),
            )),
            Err(_) => out.push(issue("float_range", "not a float".to_string())),
        },
        Rule::Port => {
            if let Err(e) = crate::coerce::parse_port_u16(value) {
                out.push(issue("port", e));
            }
        }
        Rule::Boolean => {
            if crate::coerce::parse_bool(value).is_err() {
                out.push(issue("boolean", "not a boolean".to_string()));
            }
        }
        Rule::Email => {
            if let Err(e) = check_email(value) {
                out.push(issue("email", e));
            }
        }
        Rule::Url => {
            if let Err(e) = check_url(value) {
                out.push(issue("url", e));
            }
        }
        Rule::Uuid => {
            if let Err(e) = check_uuid(value) {
                out.push(issue("uuid", e));
            }
        }
        Rule::Ipv4 => {
            if Ipv4Addr::from_str(value).is_err() {
                out.push(issue("ipv4", "not an IPv4 address".to_string()));
            }
        }
        Rule::Ip => {
            if IpAddr::from_str(value).is_err() {
                out.push(issue("ip", "not an IP address".to_string()));
            }
        }
        Rule::Hex => {
            if let Err(e) = check_hex(value) {
                out.push(issue("hex", e));
            }
        }
        Rule::List {
            delim,
            item,
            min_items,
            max_items,
        } => {
            let parts: Vec<&str> = value.split(*delim).map(str::trim).collect();
            if let Some(n) = min_items {
                if parts.len() < *n {
                    out.push(issue(
                        "min_items",
                        format!("length {} < {}", parts.len(), n),
                    ));
                }
            }
            if let Some(n) = max_items {
                if parts.len() > *n {
                    out.push(issue(
                        "max_items",
                        format!("length {} > {}", parts.len(), n),
                    ));
                }
            }
            for (i, part) in parts.iter().enumerate() {
                let sub_key = format!("{}[{}]", key, i);
                validate_field(&sub_key, item, Some(*part), out);
            }
        }
    }
}

fn check_email(v: &str) -> Result<(), String> {
    if v.chars().any(char::is_whitespace) {
        return Err("contains whitespace".to_string());
    }
    let mut parts = v.split('@');
    let local = parts.next().unwrap_or("");
    let domain = parts.next().unwrap_or("");
    if parts.next().is_some() {
        return Err("must contain exactly one '@'".to_string());
    }
    if local.is_empty() {
        return Err("empty local part".to_string());
    }
    if domain.is_empty() {
        return Err("empty domain".to_string());
    }
    Ok(())
}

fn check_url(v: &str) -> Result<(), String> {
    let rest = if let Some(r) = v.strip_prefix("http://") {
        r
    } else if let Some(r) = v.strip_prefix("https://") {
        r
    } else {
        return Err("must start with http:// or https://".to_string());
    };
    if rest.is_empty() {
        return Err("empty host".to_string());
    }
    if rest.chars().any(char::is_whitespace) {
        return Err("contains whitespace".to_string());
    }
    Ok(())
}

fn check_uuid(v: &str) -> Result<(), String> {
    if v.len() != 36 {
        return Err(format!("length {} != 36", v.len()));
    }
    for (i, b) in v.as_bytes().iter().enumerate() {
        let is_dash = matches!(i, 8 | 13 | 18 | 23);
        if is_dash {
            if *b != b'-' {
                return Err(format!("expected '-' at position {}", i));
            }
        } else if !(*b as char).is_ascii_hexdigit() {
            return Err(format!("non-hex digit at position {}", i));
        }
    }
    Ok(())
}

fn check_hex(v: &str) -> Result<(), String> {
    let rest = v
        .strip_prefix("0x")
        .or_else(|| v.strip_prefix("0X"))
        .unwrap_or(v);
    if rest.is_empty() {
        return Err("empty".to_string());
    }
    if !rest.chars().all(|c| c.is_ascii_hexdigit()) {
        return Err("non-hex digit".to_string());
    }
    Ok(())
}

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

    fn vars(pairs: &[(&str, &str)]) -> EnvroVars {
        pairs
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect()
    }

    fn issues(res: Result<(), EnvroError>) -> Vec<ValidationIssue> {
        match res {
            Ok(()) => Vec::new(),
            Err(EnvroError::Validation { errors }) => errors,
            Err(e) => panic!("unexpected error variant: {e}"),
        }
    }

    #[test]
    fn required_missing_and_empty_fails() {
        let schema = Schema::new()
            .field("A", Field::required())
            .field("B", Field::required());
        let v = vars(&[("B", "")]);
        let iss = issues(validate(&v, &schema));
        assert_eq!(iss.len(), 2);
        assert!(iss.iter().all(|i| i.rule == "required"));
    }

    #[test]
    fn optional_missing_or_empty_skips_rules() {
        let schema = Schema::new()
            .field("A", Field::optional().min_len(3))
            .field("B", Field::optional().min_len(3));
        let v = vars(&[("B", "")]);
        assert!(validate(&v, &schema).is_ok());
    }

    #[test]
    fn min_max_exact_len() {
        let schema = Schema::new()
            .field("A", Field::required().min_len(3))
            .field("B", Field::required().max_len(2))
            .field("C", Field::required().exact_len(4));
        let v = vars(&[("A", "ab"), ("B", "abc"), ("C", "abcd")]);
        let iss = issues(validate(&v, &schema));
        assert_eq!(iss.len(), 2);
        assert!(iss.iter().any(|i| i.key == "A" && i.rule == "min_len"));
        assert!(iss.iter().any(|i| i.key == "B" && i.rule == "max_len"));
    }

    #[test]
    fn string_shape_rules() {
        let schema = Schema::new()
            .field("A", Field::required().alpha())
            .field("B", Field::required().alphanumeric())
            .field("C", Field::required().digits())
            .field("D", Field::required().ascii())
            .field("E", Field::required().lowercase())
            .field("F", Field::required().uppercase());
        let v = vars(&[
            ("A", "abc"),
            ("B", "abc123"),
            ("C", "123"),
            ("D", "hello"),
            ("E", "abc"),
            ("F", "ABC"),
        ]);
        assert!(validate(&v, &schema).is_ok());

        let bad = vars(&[
            ("A", "ab1"),
            ("B", "ab!"),
            ("C", "12a"),
            ("D", "cafÉ"),
            ("E", "aB"),
            ("F", "Ab"),
        ]);
        let iss = issues(validate(&bad, &schema));
        assert_eq!(iss.len(), 6);
    }

    #[test]
    fn starts_ends_contains_one_of() {
        let schema = Schema::new()
            .field("A", Field::required().starts_with("http"))
            .field("B", Field::required().ends_with(".env"))
            .field("C", Field::required().contains("://"))
            .field(
                "D",
                Field::required().one_of(&["debug", "info", "warn", "error"]),
            )
            .field("E", Field::required().not_one_of(&["root", "admin"]));
        let ok = vars(&[
            ("A", "https://x"),
            ("B", "prod.env"),
            ("C", "pg://x"),
            ("D", "info"),
            ("E", "app"),
        ]);
        assert!(validate(&ok, &schema).is_ok());

        let bad = vars(&[
            ("A", "ftp://x"),
            ("B", "prod.yaml"),
            ("C", "no-scheme"),
            ("D", "trace"),
            ("E", "root"),
        ]);
        let iss = issues(validate(&bad, &schema));
        assert_eq!(iss.len(), 5);
    }

    #[test]
    fn number_rules() {
        let schema = Schema::new()
            .field("I", Field::required().integer())
            .field("P", Field::required().positive_integer())
            .field("N", Field::required().non_negative_integer())
            .field("F", Field::required().float())
            .field("R", Field::required().int_range(10, 20))
            .field("FR", Field::required().float_range(0.0, 1.0));
        let ok = vars(&[
            ("I", "-3"),
            ("P", "1"),
            ("N", "0"),
            ("F", "1.5"),
            ("R", "15"),
            ("FR", "0.5"),
        ]);
        assert!(validate(&ok, &schema).is_ok());

        let bad = vars(&[
            ("I", "x"),
            ("P", "0"),
            ("N", "-1"),
            ("F", "abc"),
            ("R", "99"),
            ("FR", "2.0"),
        ]);
        let iss = issues(validate(&bad, &schema));
        assert_eq!(iss.len(), 6);
    }

    #[test]
    fn port_rule() {
        let schema = Schema::new().field("P", Field::required().port());
        assert!(validate(&vars(&[("P", "8080")]), &schema).is_ok());
        assert!(validate(&vars(&[("P", "1")]), &schema).is_ok());
        assert!(validate(&vars(&[("P", "65535")]), &schema).is_ok());
        assert_eq!(issues(validate(&vars(&[("P", "0")]), &schema)).len(), 1);
        assert_eq!(issues(validate(&vars(&[("P", "70000")]), &schema)).len(), 1);
        assert_eq!(issues(validate(&vars(&[("P", "x")]), &schema)).len(), 1);
    }

    #[test]
    fn boolean_rule() {
        let schema = Schema::new().field("B", Field::required().boolean());
        for good in ["true", "TRUE", "false", "1", "0", "yes", "No"] {
            assert!(
                validate(&vars(&[("B", good)]), &schema).is_ok(),
                "expected {good} to pass"
            );
        }
        for bad in ["truthy", "on", "off", "2"] {
            let iss = issues(validate(&vars(&[("B", bad)]), &schema));
            assert_eq!(iss.len(), 1, "expected {bad} to fail");
            assert_eq!(iss[0].rule, "boolean");
        }
    }

    #[test]
    fn email_url_uuid_ip_hex() {
        let schema = Schema::new()
            .field("E", Field::required().email())
            .field("U", Field::required().url())
            .field("ID", Field::required().uuid())
            .field("V4", Field::required().ipv4())
            .field("IP", Field::required().ip())
            .field("H", Field::required().hex());
        let ok = vars(&[
            ("E", "a@b.co"),
            ("U", "https://example.com/path"),
            ("ID", "550e8400-e29b-41d4-a716-446655440000"),
            ("V4", "192.168.1.1"),
            ("IP", "::1"),
            ("H", "0xdeadBEEF"),
        ]);
        assert!(validate(&ok, &schema).is_ok());

        let bad = vars(&[
            ("E", "no-at"),
            ("U", "ftp://x"),
            ("ID", "not-a-uuid"),
            ("V4", "999.1.1.1"),
            ("IP", "not-ip"),
            ("H", "0xzz"),
        ]);
        let iss = issues(validate(&bad, &schema));
        assert_eq!(iss.len(), 6);
    }

    #[test]
    fn list_applies_item_rules() {
        let schema = Schema::new().field(
            "TAGS",
            Field::required().list(',', Field::required().min_len(2)),
        );
        assert!(validate(&vars(&[("TAGS", "aa, bb, ccc")]), &schema).is_ok());

        let iss = issues(validate(&vars(&[("TAGS", "aa,,x")]), &schema));
        // two failures: empty part (required) + "x" too short
        assert_eq!(iss.len(), 2);
        assert!(iss
            .iter()
            .any(|i| i.key == "TAGS[1]" && i.rule == "required"));
        assert!(iss
            .iter()
            .any(|i| i.key == "TAGS[2]" && i.rule == "min_len"));
    }

    #[test]
    fn list_optional_items_skip_empty() {
        let schema = Schema::new().field(
            "T",
            Field::required().list(',', Field::optional().min_len(2)),
        );
        // "a," -> ["a", ""] ; "a" fails min_len, "" is optional/skipped
        let iss = issues(validate(&vars(&[("T", "a,")]), &schema));
        assert_eq!(iss.len(), 1);
        assert_eq!(iss[0].key, "T[0]");
        assert_eq!(iss[0].rule, "min_len");
    }

    #[test]
    fn list_min_max_items() {
        let schema = Schema::new().field(
            "T",
            Field::required()
                .list(',', Field::required())
                .min_items(2)
                .max_items(3),
        );
        assert!(validate(&vars(&[("T", "a,b")]), &schema).is_ok());
        assert!(validate(&vars(&[("T", "a,b,c")]), &schema).is_ok());
        assert_eq!(issues(validate(&vars(&[("T", "a")]), &schema)).len(), 1);
        assert_eq!(
            issues(validate(&vars(&[("T", "a,b,c,d")]), &schema)).len(),
            1
        );
    }

    #[test]
    fn collects_all_failures() {
        let schema = Schema::new()
            .field("A", Field::required().min_len(5).max_len(3))
            .field("B", Field::required().integer());
        // "abcd" (len 4) fails both min_len(5) and max_len(3); "nope" fails integer
        let iss = issues(validate(&vars(&[("A", "abcd"), ("B", "nope")]), &schema));
        assert_eq!(iss.len(), 3);
        assert!(iss.iter().any(|i| i.key == "A" && i.rule == "min_len"));
        assert!(iss.iter().any(|i| i.key == "A" && i.rule == "max_len"));
        assert!(iss.iter().any(|i| i.key == "B" && i.rule == "integer"));
    }

    #[test]
    fn unknown_keys_are_ignored() {
        let schema = Schema::new().field("A", Field::required());
        let v = vars(&[("A", "x"), ("EXTRA", "y")]);
        assert!(validate(&v, &schema).is_ok());
    }

    #[test]
    fn display_includes_validation_prefix_and_issues() {
        let schema = Schema::new().field("A", Field::required().integer());
        let err = validate(&vars(&[("A", "nope")]), &schema).unwrap_err();
        let s = err.to_string();
        assert!(s.starts_with("VALIDATION_ERROR "), "got: {s}");
        assert!(s.contains("A[integer]"), "got: {s}");
    }
    #[test]
    fn exact_len_fail() {
        let schema = Schema::new().field("A", Field::required().exact_len(4));
        let iss = issues(validate(&vars(&[("A", "xyz")]), &schema));
        assert_eq!(iss.len(), 1);
        assert_eq!(iss[0].rule, "exact_len");
    }

    #[test]
    fn number_parse_error_branches() {
        // Non-numeric input hits the Err arm of positive_integer,
        // non_negative_integer, int_range, positive_float, non_negative_float,
        // float_range.
        let schema = Schema::new()
            .field("PI", Field::required().positive_integer())
            .field("NNI", Field::required().non_negative_integer())
            .field("IR", Field::required().int_range(0, 10))
            .field("PF", Field::required().positive_float())
            .field("NNF", Field::required().non_negative_float())
            .field("FR", Field::required().float_range(0.0, 1.0));
        let iss = issues(validate(
            &vars(&[
                ("PI", "x"),
                ("NNI", "x"),
                ("IR", "x"),
                ("PF", "x"),
                ("NNF", "x"),
                ("FR", "x"),
            ]),
            &schema,
        ));
        assert_eq!(iss.len(), 6);
        assert!(iss
            .iter()
            .any(|i| i.key == "PI" && i.rule == "positive_integer"));
        assert!(iss
            .iter()
            .any(|i| i.key == "NNI" && i.rule == "non_negative_integer"));
        assert!(iss.iter().any(|i| i.key == "IR" && i.rule == "int_range"));
        assert!(iss
            .iter()
            .any(|i| i.key == "PF" && i.rule == "positive_float"));
        assert!(iss
            .iter()
            .any(|i| i.key == "NNF" && i.rule == "non_negative_float"));
        assert!(iss.iter().any(|i| i.key == "FR" && i.rule == "float_range"));
    }

    #[test]
    fn positive_float_and_non_negative_float_value_branches() {
        // Covers positive_float(<=0), non_negative_float(<0), and their happy paths.
        let schema = Schema::new()
            .field("PF", Field::required().positive_float())
            .field("NNF", Field::required().non_negative_float());
        let iss = issues(validate(&vars(&[("PF", "0.0"), ("NNF", "-0.5")]), &schema));
        assert_eq!(iss.len(), 2);
        assert!(iss.iter().any(|i| i.rule == "positive_float"));
        assert!(iss.iter().any(|i| i.rule == "non_negative_float"));

        assert!(validate(&vars(&[("PF", "0.5"), ("NNF", "0.0")]), &schema).is_ok());
    }

    #[test]
    fn email_error_branches() {
        let schema = Schema::new().field("E", Field::required().email());
        let iss = issues(validate(&vars(&[("E", "a b@c.co")]), &schema));
        assert_eq!(iss.len(), 1);
        assert!(iss[0].reason.contains("whitespace"));
        let iss = issues(validate(&vars(&[("E", "a@b@c")]), &schema));
        assert_eq!(iss.len(), 1);
        assert!(iss[0].reason.contains("exactly one"));
        let iss = issues(validate(&vars(&[("E", "@b.co")]), &schema));
        assert_eq!(iss.len(), 1);
        assert!(iss[0].reason.contains("empty local"));
    }

    #[test]
    fn url_http_and_error_branches() {
        let schema = Schema::new().field("U", Field::required().url());
        assert!(validate(&vars(&[("U", "http://x")]), &schema).is_ok());
        let iss = issues(validate(&vars(&[("U", "https://")]), &schema));
        assert_eq!(iss.len(), 1);
        assert!(iss[0].reason.contains("empty host"));
        let iss = issues(validate(&vars(&[("U", "https://a b")]), &schema));
        assert_eq!(iss.len(), 1);
        assert!(iss[0].reason.contains("whitespace"));
    }

    #[test]
    fn uuid_position_and_hex_error_branches() {
        let schema = Schema::new().field("ID", Field::required().uuid());
        // len 36 but non-'-' at position 8
        let iss = issues(validate(
            &vars(&[("ID", "550e8400xe29b-41d4-a716-446655440000")]),
            &schema,
        ));
        assert_eq!(iss.len(), 1);
        assert!(iss[0].reason.contains("expected '-'"));

        // len 36, correct dashes, but non-hex at end
        let iss = issues(validate(
            &vars(&[("ID", "550e8400-e29b-41d4-a716-44665544000z")]),
            &schema,
        ));
        assert_eq!(iss.len(), 1);
        assert!(iss[0].reason.contains("non-hex digit"));
    }

    #[test]
    fn hex_empty_after_prefix_and_uppercase_prefix() {
        let schema = Schema::new().field("H", Field::optional().hex());
        // "0x" strips to "" -> empty branch
        let iss = issues(validate(&vars(&[("H", "0x")]), &schema));
        assert_eq!(iss.len(), 1);
        assert_eq!(iss[0].reason, "empty");
        // exercises the "0X" prefix branch too
        assert!(validate(&vars(&[("H", "0XFF")]), &schema).is_ok());
    }

    #[test]
    fn validate_env_reads_process_env() {
        use serial_test::serial;
        // guard: keep this scope thread-safe.
        #[serial]
        fn inner() {
            std::env::remove_var("VE_A");
            std::env::remove_var("VE_B");
            std::env::remove_var("VE_C");

            let schema = Schema::new()
                .field("VE_A", Field::required().min_len(3))
                .field("VE_B", Field::optional().integer())
                .field("VE_C", Field::required().port());

            // all missing: only required fields fail.
            let err = validate_env(&schema).unwrap_err();
            match err {
                EnvroError::Validation { errors } => {
                    assert_eq!(errors.len(), 2);
                    assert!(errors
                        .iter()
                        .any(|i| i.key == "VE_A" && i.rule == "required"));
                    assert!(errors
                        .iter()
                        .any(|i| i.key == "VE_C" && i.rule == "required"));
                }
                other => panic!("expected Validation, got {other}"),
            }

            // set valid values -> ok.
            std::env::set_var("VE_A", "envro");
            std::env::set_var("VE_C", "8080");
            validate_env(&schema).unwrap();

            // add a bad optional -> single failure.
            std::env::set_var("VE_B", "not-int");
            let err = validate_env(&schema).unwrap_err();
            match err {
                EnvroError::Validation { errors } => {
                    assert_eq!(errors.len(), 1);
                    assert_eq!(errors[0].key, "VE_B");
                    assert_eq!(errors[0].rule, "integer");
                }
                other => panic!("expected Validation, got {other}"),
            }

            std::env::remove_var("VE_A");
            std::env::remove_var("VE_B");
            std::env::remove_var("VE_C");
        }
        inner();
    }

    #[test]
    fn load_dotenv_validated_ok_and_err() {
        use std::io::Write;

        let dir = std::env::temp_dir();
        let ok_file = dir.join(".env-validate-ok");
        let mut f = std::fs::File::create(&ok_file).unwrap();
        f.write_all(b"NAME=envro\nPORT=8080").unwrap();
        let schema = Schema::new()
            .field("NAME", Field::required().min_len(3))
            .field("PORT", Field::required().port());
        let loaded = load_dotenv_validated(ok_file.as_path(), &schema).unwrap();
        assert_eq!(loaded.get("NAME").map(String::as_str), Some("envro"));
        assert_eq!(loaded.get("PORT").map(String::as_str), Some("8080"));

        let bad_file = dir.join(".env-validate-bad");
        let mut f = std::fs::File::create(&bad_file).unwrap();
        f.write_all(b"NAME=ab\nPORT=nope").unwrap();
        let err = load_dotenv_validated(bad_file.as_path(), &schema).unwrap_err();
        match err {
            EnvroError::Validation { errors } => assert_eq!(errors.len(), 2),
            other => panic!("expected Validation, got {other}"),
        }

        // Missing file -> EnvroError::File propagated.
        let err = load_dotenv_validated(Path::new("/nonexistent/.env-x"), &schema).unwrap_err();
        assert!(matches!(err, EnvroError::File { .. }));
    }
}