cddl-cat 0.7.1

Parse CDDL schemas and validate CBOR or JSON serialized data
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
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
//! This module contains code to validate serialized data.
//!
//! More precisely, it validates data that can be represented by [`Value`] trees.

use std::collections::{BTreeMap, HashMap, VecDeque};
use std::convert::TryInto;
use std::mem::discriminant;

use crate::context::LookupContext;
use crate::ivt::*;
use crate::normalize::normalize_size_range;
use crate::util::{mismatch, ValidateError, ValidateResult};
use crate::value::Value;

// A map from generic parameter name to the type being used here.
#[derive(Clone, Debug, Default)]
struct GenericMap<'a> {
    map: HashMap<String, &'a Node>,
    // Because we captured this map at a previous time, we may need to look up
    // generic types from that previous context.  We carry a copy of that
    // Context with us to do those lookups.
    past_ctx: Option<&'a Context<'a>>,
}

#[derive(Clone)]
struct Context<'a> {
    lookup: &'a dyn LookupContext,
    generic_map: GenericMap<'a>,
    depth: u32,
}

impl std::fmt::Debug for Context<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Context")
            .field("generic_map", &self.generic_map)
            .finish()
    }
}

struct NodeContext<'a> {
    node: &'a Node,
    ctx: Context<'a>,
}

impl<'a> Context<'a> {
    // Set a maximum depth, to avoid infinite recursion in the case of
    // circular rule references.
    const MAX_DEPTH: u32 = 50;

    fn inc_depth(&self) -> TempResult<u32> {
        if self.depth >= Self::MAX_DEPTH {
            Err(ValidateError::Structural("hit recursion limit".into()))
        } else {
            Ok(self.depth + 1)
        }
    }

    // `lookup_rule` will first do a search for a local generic parameter name
    // first, followed by a lookup for a rule by that name.

    // To give an example:
    //     IP = [u8, u8, u8, u8]
    //     PORT = u16
    //     socket = [IP, PORT]
    //     conn<IP> = [name, socket, IP] # This is the generic parm IP not the rule IP.
    //
    // While validating "conn" we have a Context with a generic mapping for "IP".
    // As soon as we traverse into "socket", we need for there to be a new context,
    // where no "IP" exists (so we'll do the rule lookup instead).
    //
    fn lookup_rule(&'a self, rule: &'a Rule) -> TempResult<NodeContext<'a>> {
        // First, check to see if the "rule name" is actually a generic parameter.
        // TODO: this would be a lot easier if this were pre-processed by the flattener
        // so that "rule lookup" and "generic type lookup" were two separate Node variants.
        let generic_node: Option<&'a Node> = self.generic_map.map.get(&rule.name).cloned();
        if let Some(node) = generic_node {
            // If we stored a past_ctx along with the generic args, then pass that
            // context along with the node that is substituting for this generic
            // parameter name.  Otherwise, return a new blank Context.
            let ctx = match self.generic_map.past_ctx {
                Some(ctx) => ctx.clone(),
                None => self.blank()?,
            };
            return Ok(NodeContext { node, ctx });
        }

        let rule_def: &RuleDef = self.lookup.lookup_rule(&rule.name)?;

        // Create a new context containing a new generic parameter map.
        let ctx = self.derive(rule_def, rule)?;

        Ok(NodeContext {
            node: &rule_def.node,
            ctx,
        })
    }

    // Create a new blank Context (with no parameter map)
    fn blank(&self) -> TempResult<Context<'a>> {
        Ok(Context {
            lookup: self.lookup,
            generic_map: GenericMap::default(),
            depth: self.inc_depth()?,
        })
    }

    // Create a new Context, with optional generic parameter map.
    fn derive(&'a self, rule_def: &'a RuleDef, rule: &'a Rule) -> TempResult<Context<'a>> {
        let parms_len = rule_def.generic_parms.len();
        let args_len = rule.generic_args.len();
        if parms_len != args_len {
            // Wrong number of generic arguments
            return Err(ValidateError::GenericError);
        }

        // Zip the two Vecs:
        // 1. rule_def.generic_parms
        // 2. rule.generic_args
        // and put them into a HashMap so we can do lookups from parm -> arg.
        let map: HashMap<String, &'a Node> = rule_def
            .generic_parms
            .iter()
            .cloned()
            .zip(rule.generic_args.iter())
            .collect();

        let generic_map = GenericMap {
            map,
            past_ctx: Some(self),
        };

        Ok(Context {
            lookup: self.lookup,
            generic_map,
            depth: self.inc_depth()?,
        })
    }
}

#[allow(dead_code, reason = "Prevent warnings if all features are disabled")]
pub(crate) fn do_validate(
    value: &Value,
    rule_def: &RuleDef,
    ctx: &dyn LookupContext,
) -> ValidateResult {
    // If the rule_def passed in requires generic parameters, we should
    // return an error, because we don't have any way to specify them.
    if !rule_def.generic_parms.is_empty() {
        return Err(ValidateError::GenericError);
    }

    let ctx = Context {
        lookup: ctx,
        generic_map: GenericMap::default(),
        depth: 0,
    };
    let node = &rule_def.node;
    validate(value, node, &ctx)
}

type ValueMap = BTreeMap<Value, Value>;

// A Result that returns some temporary value.
type TempResult<T> = Result<T, ValidateError>;

/// This struct allows us to maintain a map that is consumed during validation.
struct WorkingMap {
    map: ValueMap,
    // A stack of lists; each list contains maybe-discarded elements,
    // in (key, value) form.
    snaps: VecDeque<VecDeque<(Value, Value)>>,
}

impl WorkingMap {
    /// Makes a copy of an existing map's table.
    fn new(value_map: &ValueMap) -> WorkingMap {
        WorkingMap {
            map: value_map.clone(),
            snaps: VecDeque::new(),
        }
    }

    // When we start speculatively matching map elements (e.g. in a Choice
    // or Occur containing groups), we may fail the match partway through, and
    // need to rewind to the most recent snapshot.
    //
    // It's possible for nested snapshots to exist; for example if we have a
    // group-of-choices nested inside a group-of-choices.
    //
    // If one array is nested inside another, the inner array will get its
    // own WorkingArray so snapshots aren't necessary in that case.
    fn snapshot(&mut self) {
        self.snaps.push_back(VecDeque::new());
    }

    // Restore the map to the point when we last called snapshot()
    fn rewind(&mut self) {
        // If validate code is implemented correctly, then unwrap() should
        // never panic.
        let mut top_snap = self.snaps.pop_back().unwrap();
        // Drain the elements (order not important), and insert them back into
        // the working map.
        self.map.extend(top_snap.drain(..));
    }

    // We completed a match, so we can retire the most recent snapshot.
    fn commit(&mut self) {
        // If validate code is implemented correctly, then unwrap() should
        // never panic.
        // This throws away the list that was popped; those values were
        // successfully matched and are no longer needed.
        self.snaps.pop_back().unwrap();
    }

    // Peek at the value correspending to a given key (if any)
    fn peek_at(&self, key: &Value) -> Option<&Value> {
        self.map.get(key)
    }

    // Remove a value from the working map.
    // If there is an active snapshot, stash the key/value pair there until
    // we're certain we've matched the entire group.
    fn remove(&mut self, key: &Value) {
        // If validate code is implemented correctly, then unwrap() should
        // never panic (we've already peeked at this value in order to match
        // it.)
        let value = self.map.remove(key).unwrap();
        // If there is a current snapshot, preserve this element
        // for later rewind.
        if let Some(snap) = self.snaps.back_mut() {
            snap.push_back((key.clone(), value));
        }
    }
}

/// This struct allows us to maintain a copy of an array that is consumed
/// during validation.
#[derive(Debug)]
struct WorkingArray {
    // The elements in the Value Array
    array: VecDeque<Value>,
    // A stack of lists; each list contains maybe-discarded elements.
    snaps: VecDeque<VecDeque<Value>>,
}

impl WorkingArray {
    /// Makes a copy of an existing map's table.
    fn new(array: &[Value]) -> WorkingArray {
        let deque: VecDeque<Value> = array.iter().cloned().collect();
        WorkingArray {
            array: deque,
            snaps: VecDeque::new(),
        }
    }

    // When we start speculatively matching array elements (e.g. in a Choice
    // or Occur containing groups), we may fail the match partway through, and
    // need to rewind to the most recent snapshot.
    //
    // It's possible for nested snapshots to exist; for example if we have a
    // group-of-choices nested inside a group-of-choices.
    //
    // If one array is nested inside another, the inner array will get its
    // own WorkingArray so snapshots aren't necessary in that case.
    fn snapshot(&mut self) {
        let new_snap: VecDeque<Value> = VecDeque::new();
        self.snaps.push_back(new_snap);
    }

    // Restore the array to the point when we last called snapshot()
    fn rewind(&mut self) {
        // If validate code is implemented correctly, then unwrap() should
        // never panic.
        let mut top_snap = self.snaps.pop_back().unwrap();
        // drain the elements in LIFO order, and push them back into
        // the working array.
        for element in top_snap.drain(..).rev() {
            self.array.push_front(element);
        }
    }

    // We completed a match, so we can retire the most recent snapshot.
    fn commit(&mut self) {
        // If validate code is implemented correctly, then unwrap() should
        // never panic.
        // This throws away the list that was popped; those values were
        // successfully matched and are no longer needed.
        self.snaps.pop_back().unwrap();
    }

    // Peek at the front of the working array.
    fn peek_front(&self) -> Option<&Value> {
        self.array.front()
    }

    // Remove an element from the working array.
    // If there is an active snapshot, stash the element there until we're
    // certain we've matched the entire group.
    fn pop_front(&mut self) {
        // If validate code is implemented correctly, then unwrap() should
        // never panic (we've already peeked at this value in order to match
        // it.)
        let element = self.array.pop_front().unwrap();
        // If there is a current snapshot, preserve this element
        // for later rewind.
        if let Some(snap) = self.snaps.back_mut() {
            snap.push_back(element);
        }
    }
}

// This is the main validation dispatch function.
// It tries to match a Node and a Value, recursing as needed.
#[allow(
    dead_code,
    reason = "Prevent warnings if both ciborium and serde_json are disabled"
)]
fn validate(value: &Value, node: &Node, ctx: &Context) -> ValidateResult {
    match node {
        Node::Literal(l) => validate_literal(l, value),
        Node::PreludeType(p) => validate_prelude_type(*p, value),
        Node::Choice(c) => validate_choice(c, value, ctx),
        Node::Map(m) => validate_map(m, value, ctx),
        Node::Array(a) => validate_array(a, value, ctx),
        Node::Rule(r) => validate_rule(r, value, ctx),
        Node::Group(g) => validate_standalone_group(g, value, ctx),
        Node::KeyValue(_) => Err(ValidateError::Structural("unexpected KeyValue".into())),
        Node::Occur(_) => Err(ValidateError::Structural("unexpected Occur".into())),
        Node::Unwrap(_) => Err(ValidateError::Structural("unexpected Unwrap".into())),
        Node::Range(r) => validate_range(r, value, ctx),
        Node::Control(ctl) => validate_control(ctl, value, ctx),
        Node::Choiceify(r) => validate_choiceify(r, value, ctx),
        Node::ChoiceifyInline(a) => validate_choiceify_inline(a, value, ctx),
    }
}

// Perform map key search.
// Some keys (literal values) can be found with a fast search, while
// others may require a linear search.
fn validate_map_key<'a>(
    value_map: &'a mut WorkingMap,
    node: &Node,
    ctx: &Context,
) -> TempResult<(Value, &'a Value)> {
    match node {
        Node::Literal(l) => map_search_literal(l, value_map),
        _ => map_search(node, value_map, ctx),
    }
}

/// Validate a `Choice` containing an arbitrary number of "option" nodes.
///
/// If any of the options matches, this validation is successful.
fn validate_choice(choice: &Choice, value: &Value, ctx: &Context) -> ValidateResult {
    for node in &choice.options {
        match validate(value, node, ctx) {
            Ok(()) => {
                return Ok(());
            }
            Err(e) => {
                // Only fail if the error is considered fatal.
                // Otherwise, we'll keep trying other options.
                if e.is_fatal() {
                    return Err(e);
                }
            }
        }
    }
    let expected = format!("choice of {}", choice.options.len());
    Err(mismatch(expected))
}

/// Validate a `Rule` reference
///
/// Seek out the right `Node` and `Context`, and recurse.
fn validate_rule(rule: &Rule, value: &Value, ctx: &Context) -> ValidateResult {
    let answer = ctx.lookup_rule(rule)?;
    validate(value, answer.node, &answer.ctx)
}

/// Create a `Value` from a `Literal`.
impl From<&Literal> for Value {
    fn from(l: &Literal) -> Value {
        match l {
            Literal::Bool(b) => Value::Bool(*b),
            Literal::Int(i) => Value::Integer(*i),
            Literal::Float(f) => Value::from_float(*f),
            Literal::Text(t) => Value::Text(t.clone()),
            Literal::Bytes(b) => Value::Bytes(b.clone()),
        }
    }
}

fn validate_literal(literal: &Literal, value: &Value) -> ValidateResult {
    if *value == Value::from(literal) {
        return Ok(());
    }
    Err(mismatch(format!("{}", literal)))
}

// Find a specific key in the map and return that key plus a reference to its value.
fn map_search_literal<'a>(
    literal: &Literal,
    working_map: &'a mut WorkingMap,
) -> TempResult<(Value, &'a Value)> {
    let search_key = Value::from(literal);
    match working_map.peek_at(&search_key) {
        Some(val) => Ok((search_key, val)),
        None => {
            // We didn't find the key; return an error
            Err(mismatch(format!("map{{{}}}", literal)))
        }
    }
}

// Iterate over each key in the working map, looking for a match.
// If we find a match, return a copy of the key, and a reference to the value.
// This is less efficient than map_search_literal.
fn map_search<'a>(
    node: &Node,
    working_map: &'a mut WorkingMap,
    ctx: &Context,
) -> TempResult<(Value, &'a Value)> {
    for (key, value) in &working_map.map {
        let attempt = validate(key, node, ctx);
        if attempt.is_ok() {
            return Ok((key.clone(), value));
        }
    }
    // We searched all the keys without finding a match.  Validation fails.
    Err(mismatch(format!("map{{{}}}", node)))
}

// Note `ty` is passed by value because clippy says it's only 1 byte.
fn validate_prelude_type(ty: PreludeType, value: &Value) -> ValidateResult {
    match (ty, value) {
        (PreludeType::Any, _) => Ok(()),
        (PreludeType::Nil, Value::Null) => Ok(()),
        (PreludeType::Nil, _) => Err(mismatch("nil")),
        (PreludeType::Bool, Value::Bool(_)) => Ok(()),
        (PreludeType::Bool, _) => Err(mismatch("bool")),
        (PreludeType::Int, Value::Integer(_)) => Ok(()),
        (PreludeType::Int, _) => Err(mismatch("int")),
        (PreludeType::Uint, Value::Integer(x)) if *x >= 0 => Ok(()),
        (PreludeType::Uint, _) => Err(mismatch("uint")),
        (PreludeType::Nint, Value::Integer(x)) if *x < 0 => Ok(()),
        (PreludeType::Nint, _) => Err(mismatch("nint")),
        (PreludeType::Float, Value::Float(_)) => Ok(()),
        (PreludeType::Float, _) => Err(mismatch("float")),
        (PreludeType::Tstr, Value::Text(_)) => Ok(()),
        (PreludeType::Tstr, _) => Err(mismatch("tstr")),
        (PreludeType::Bstr, Value::Bytes(_)) => Ok(()),
        (PreludeType::Bstr, _) => Err(mismatch("bstr")),
    }
}

// FIXME: should this be combined with Map handling?
fn validate_array(ar: &Array, value: &Value, ctx: &Context) -> ValidateResult {
    match value {
        Value::Array(a) => validate_array_part2(ar, a, ctx),
        _ => Err(mismatch("array")),
    }
}

fn validate_array_part2(ar: &Array, value_array: &[Value], ctx: &Context) -> ValidateResult {
    // Strategy for validating an array:
    // 1. We assume that the code that constructed the IVT Array placed the
    //    members in matching order (literals first, more general types at the
    //    end) so that we consume IVT Array members in order without worrying
    //    about non-deterministic results.
    // 2. Make a mutable working copy of the Value::Array contents
    // 3. Iterate over the IVT Array, searching the working copy for a
    //    matching key.
    // 4. If a match is found, remove the value from our working copy.
    // 6. If the IVT member can consume multiple values, repeat the search for
    //    this key.
    // 7. If a match is not found and the member is optional (or we've already
    //    consumed an acceptable number of keys), continue to the next IVT
    //    member.
    // 8. If the member is not found and we haven't consumed the expected
    //    number of values, return an error.

    let mut working_array = WorkingArray::new(value_array);

    for member in &ar.members {
        validate_array_member(member, &mut working_array, ctx)?;
    }
    if working_array.array.is_empty() {
        Ok(())
    } else {
        // If the working map isn't empty, that means we had some extra values
        // that didn't match anything.
        // FIXME: Should this be a unique error type?
        Err(mismatch("shorter array"))
    }
}

fn validate_array_member(
    member: &Node,
    working_array: &mut WorkingArray,
    ctx: &Context,
) -> ValidateResult {
    match member {
        // FIXME: does it make sense for this to destructure & dispatch
        // each Node type here?  Is there any way to make this generic?
        Node::Occur(o) => validate_array_occur(o, working_array, ctx),
        Node::KeyValue(kv) => {
            // The key is ignored.  Validate the value only.
            // FIXME: should we try to use the key to provide a more
            // useful error message?
            validate_array_value(&kv.value, working_array, ctx)
        }
        Node::Rule(r) => {
            // FIXME: This seems like a gross hack.  We need to dereference
            // the rule here, because if we drop to the bottom and call
            // validate_array_value() then we lose our ability to "see
            // through" KeyValue and Group nodes while remembering that we are
            // in an array context (with a particular working copy).
            // BUG: Choice nodes will have the same problem.

            let answer = ctx.lookup_rule(r)?;
            validate_array_member(answer.node, working_array, &answer.ctx)
        }
        Node::Unwrap(r) => {
            // Like Rule, we are dereferencing the Rule by hand here so that
            // we can "see through" to the underlying data without forgetting
            // we were in an array context.
            let answer = ctx.lookup_rule(r)?;
            validate_array_unwrap(answer.node, working_array, &answer.ctx)
        }
        Node::Choice(c) => {
            // We need to explore each of the possible choices.
            // We can't use validate_array_value() because we'll lose our
            // array context.
            for option in &c.options {
                match validate_array_member(option, working_array, ctx) {
                    Ok(()) => {
                        return Ok(());
                    }
                    Err(e) => {
                        // Only fail if the error is considered fatal.
                        // Otherwise, we'll keep trying other options.
                        if e.is_fatal() {
                            return Err(e);
                        }
                    }
                }
            }
            // None of the choices worked.
            let expected = format!("choice of {}", c.options.len());
            Err(mismatch(expected))
        }
        Node::Group(g) => {
            // As we call validate_array_member, we don't know how many items
            // it might speculatively pop from the list.  So we'll take a snapshot
            // now and commit our changes if we match successfully (and roll them
            // back if it fails).
            working_array.snapshot();

            // Recurse into each member of the group.
            for group_member in &g.members {
                match validate_array_member(group_member, working_array, ctx) {
                    Ok(_) => {
                        // So far so good...
                    }
                    Err(e) => {
                        // Since we failed to validate the entire group, rewind to our most
                        // recent snapshot.  This may put values back into the array,
                        // so they can be matched by whatever we try next (or trigger
                        // an error if they aren't consumed by anything).
                        working_array.rewind();
                        return Err(e);
                    }
                }
            }
            // All group members validated Ok.
            working_array.commit();
            Ok(())
        }
        m => validate_array_value(m, working_array, ctx),
    }
}

fn validate_array_unwrap(
    node: &Node,
    working_array: &mut WorkingArray,
    ctx: &Context,
) -> ValidateResult {
    // After traversing an unwrap from inside an array, the next node must be an
    // Array node too.
    match node {
        Node::Array(a) => {
            // Recurse into each member of the unwrapped array.
            for member in &a.members {
                validate_array_member(member, working_array, ctx)?;
            }
            // All array members validated Ok.
            Ok(())
        }
        _ => Err(mismatch("unwrap array")),
    }
}

/// Validate an occurrence against a mutable working array.
// FIXME: this is pretty similar to validate_map_occur; maybe they can be combined?
fn validate_array_occur(
    occur: &Occur,
    working_array: &mut WorkingArray,
    ctx: &Context,
) -> ValidateResult {
    let (lower_limit, upper_limit) = occur.limits();
    let mut count: usize = 0;

    loop {
        match validate_array_member(&occur.node, working_array, ctx) {
            Ok(_) => (),
            Err(e) => {
                if e.is_mismatch() {
                    // Stop trying to match this occurrence.
                    break;
                }
                // The error is something serious (e.g. MissingRule or
                // Unsupported).  We should fail now and propagate that
                // error upward.
                return Err(e);
            }
        }
        count += 1;
        if count >= upper_limit {
            // Stop matching; we've consumed the maximum number of this key.
            break;
        }
    }
    if count < lower_limit {
        return Err(mismatch(format!("more array element [{}]", occur)));
    }
    Ok(())
}

/// Validate some node against a mutable working array.
fn validate_array_value(
    node: &Node,
    working_array: &mut WorkingArray,
    ctx: &Context,
) -> ValidateResult {
    match working_array.peek_front() {
        Some(val) => {
            validate(val, node, ctx)?;
            // We had a successful match; remove the matched value.
            working_array.pop_front();
            Ok(())
        }
        None => Err(mismatch(format!("array element {}", node))),
    }
}

fn validate_map(m: &Map, value: &Value, ctx: &Context) -> ValidateResult {
    match value {
        Value::Map(vm) => validate_map_part2(m, vm, ctx),
        _ => Err(mismatch("map")),
    }
}

fn validate_map_part2(m: &Map, value_map: &ValueMap, ctx: &Context) -> ValidateResult {
    // Strategy for validating a map:
    // 1. We assume that the code that constructed the IVT Map placed the keys
    //    in matching order (literals first, more general types at the end) so
    //    that we consume IVT Map keys in order without worrying about non-
    //    deterministic results.
    // 2. Make a mutable working copy of the Value::Map contents
    // 3. Iterate over the IVT Map, searching the Value::Map for a matching key.
    // 4. If a match is found, remove the key-value pair from our working copy.
    // 5. Validate the key's corresponding value.
    // 6. If the key can consume multiple values, repeat the search for this key.
    // 7. If the key is not found and the key is optional (or we've already consumed
    //    an acceptable number of keys), continue to the next key.
    // 8. If the key is not found and we haven't consumed the expected number of
    //    keys, return an error.

    let mut working_map = WorkingMap::new(value_map);

    for member in &m.members {
        validate_map_member(member, &mut working_map, ctx).map_err(|e| {
            // If a MapCut error pops out here, change it to a Mismatch, so that
            // it can't cause trouble in nested maps.
            e.erase_mapcut()
        })?;
    }
    if working_map.map.is_empty() {
        Ok(())
    } else {
        // If the working map isn't empty, that means we had some extra values
        // that didn't match anything.
        Err(mismatch("shorter map"))
    }
}

fn validate_map_member(
    member: &Node,
    working_map: &mut WorkingMap,
    ctx: &Context,
) -> ValidateResult {
    match member {
        // FIXME: does it make sense for this to destructure & dispatch
        // each Node type here?  Is there any way to make this generic?
        Node::Occur(o) => validate_map_occur(o, working_map, ctx),
        Node::KeyValue(kv) => validate_map_keyvalue(kv, working_map, ctx),
        Node::Rule(r) => {
            // We can't use the generic validate() here; we would forget that
            // we were in a map context.  We need to punch down a level into
            // the rule and match again.
            let answer = ctx.lookup_rule(r)?;
            validate_map_member(answer.node, working_map, &answer.ctx)
        }
        Node::Unwrap(r) => {
            // Like Rule, we are dereferencing the Rule by hand here so that
            // we can "see through" to the underlying data without forgetting
            // we were in a map context.
            let answer = ctx.lookup_rule(r)?;
            validate_map_unwrap(answer.node, working_map, &answer.ctx)
        }
        Node::Group(g) => {
            // As we call validate_array_member, we don't know how many items
            // it might speculatively pop from the list.  So we'll take a
            // snapshot now and commit our changes if we match successfully
            // (and roll them back if it fails).
            working_map.snapshot();

            // Recurse into each member of the group.
            for group_member in &g.members {
                match validate_map_member(group_member, working_map, ctx) {
                    Ok(_) => {
                        // So far so good...
                    }
                    Err(e) => {
                        // Since we failed to validate the entire group,
                        // rewind to our most recent snapshot.  This may put
                        // values back into the map, so they can be matched by
                        // whatever we try next (or trigger an error if they
                        // aren't consumed by anything).
                        working_map.rewind();

                        // Also forget any MapCut errors, so that a sibling
                        // group may succeed where we failed.
                        return Err(e.erase_mapcut());
                    }
                }
            }
            // All group members validated Ok.
            working_map.commit();
            Ok(())
        }
        Node::Choice(c) => validate_map_choice(&c.options, working_map, ctx),
        Node::Choiceify(r) => validate_map_choiceify(r, working_map, ctx),
        Node::ChoiceifyInline(a) => validate_map_choice(&a.members, working_map, ctx),

        // I don't think any of these are possible using CDDL grammar.
        Node::Literal(_) => Err(ValidateError::Structural("literal map member".into())),
        Node::PreludeType(_) => Err(ValidateError::Structural("prelude type map member".into())),
        Node::Map(_) => Err(ValidateError::Structural("map as map member".into())),
        Node::Array(_) => Err(ValidateError::Structural("array as map member".into())),
        Node::Range(_) => Err(ValidateError::Structural("range as map member".into())),
        Node::Control(_) => Err(ValidateError::Structural("control op as map member".into())),
    }
}

fn validate_map_choice(
    options: &[Node],
    working_map: &mut WorkingMap,
    ctx: &Context,
) -> ValidateResult {
    // We need to explore each of the possible choices.
    for option in options {
        match validate_map_member(option, working_map, ctx) {
            Ok(()) => {
                return Ok(());
            }
            Err(e) => {
                // We can keep trying other options as long as the
                // error is a Mismatch, not a MapCut or something
                // fatal.
                if !e.is_mismatch() {
                    return Err(e);
                }
            }
        }
    }
    // None of the choices worked.
    let expected = format!("choice of {}", options.len());
    Err(mismatch(expected))
}

// TODO: this duplicates a lot of code from validate_choiceify_members. Merge them?
fn validate_map_choiceify_members(
    choices: &[Node],
    working_map: &mut WorkingMap,
    ctx: &Context,
) -> ValidateResult {
    // Iterate over the input nodes. We expect a list of KeyValue variants.
    // For each KeyValue, extract its .value member and try to validate that.
    // Because we are in a group context, referring to other groups by name is
    // also allowed; we will transparently unwrap those (recursively).

    for item in choices {
        let validate_result = match item {
            Node::KeyValue(kv) => {
                // Even though we're in a map, the choicify operation
                // throws away the keys on its immediate operand.
                // validate against the value (which is presumably
                // a group containing KeyValues)
                validate_map_member(&kv.value, working_map, ctx)
            }
            Node::Rule(rule) => {
                // A group may include another group by name.
                validate_map_choiceify(rule, working_map, ctx)
            }
            _ => {
                // The flatten code will simplify a key-less KeyValue
                // to just a plain Node; handle that here.
                validate_map_member(item, working_map, ctx)
            }
        };

        // This part is the same as validate_map_choice().
        // If we matched, return Ok immediately.
        // If we didn't, keep searching (unless the error is not a mismatch).
        match validate_result {
            Ok(()) => {
                return Ok(());
            }
            Err(e) => {
                // We can keep trying other options as long as the
                // error is a Mismatch, not a MapCut or something
                // fatal.
                if !e.is_mismatch() {
                    return Err(e);
                }
            }
        }
    }
    let expected = format!("choiceified group of {}", choices.len());
    Err(mismatch(expected))
}

/// Validate a "choice-ified group" (the CDDL "&" operator)
///
/// Each value in the named group will be used as a possible choice.
fn validate_map_choiceify(
    rule: &Rule,
    working_map: &mut WorkingMap,
    ctx: &Context,
) -> ValidateResult {
    let NodeContext { node, ctx } = ctx.lookup_rule(rule)?;
    match node {
        Node::Group(g) => validate_map_choiceify_members(&g.members, working_map, &ctx),
        Node::Rule(r) => validate_map_choiceify(r, working_map, &ctx),
        _ => Err(ValidateError::Structural(
            "improper map choiceify target".into(),
        )),
    }
}

fn validate_map_unwrap(node: &Node, working_map: &mut WorkingMap, ctx: &Context) -> ValidateResult {
    // After traversing an unwrap from inside a map, the next node must be a
    // Map node too.
    match node {
        Node::Map(m) => {
            // Recurse into each member of the unwrapped array.
            for member in &m.members {
                validate_map_member(member, working_map, ctx)?;
            }
            // All array members validated Ok.
            Ok(())
        }
        _ => Err(mismatch("unwrap map")),
    }
}

/// Validate an occurrence against a mutable working map.
fn validate_map_occur(
    occur: &Occur,
    working_map: &mut WorkingMap,
    ctx: &Context,
) -> ValidateResult {
    let (lower_limit, upper_limit) = occur.limits();
    let mut count: usize = 0;

    loop {
        match validate_map_member(&occur.node, working_map, ctx) {
            Ok(_) => (),
            Err(e) => {
                if e.is_mismatch() {
                    // Stop trying to match this occurrence.
                    break;
                }
                // Either we got a MapCut error, or it's something even more
                // serious (e.g. MissingRule or Unsupported).  We should fail
                // now and propagate that error upward.
                return Err(e);
            }
        }
        count += 1;
        if count >= upper_limit {
            // Stop matching; we've consumed the maximum number of this key.
            break;
        }
    }
    if count < lower_limit {
        // Read this format string as "{{" then "{}" then "}}"
        // The first and last print a single brace; the value is in the
        // middle, e.g "{foo}".
        return Err(mismatch(format!("map{{{}}}]", occur)));
    }
    Ok(())
}

/// Validate a key-value pair against a mutable working map.
fn validate_map_keyvalue(
    kv: &KeyValue,
    working_map: &mut WorkingMap,
    ctx: &Context,
) -> ValidateResult {
    // CDDL syntax reminder:
    //   a => b   ; non-cut
    //   a ^ => b ; cut
    //   a: b     ; cut
    //
    // If we're using "cut" semantics, a partial match (key matches + value
    // mismatch) should force validation failure for the entire map.  We
    // signal this to our caller with a MapCut error.
    // If we're using "non-cut" semantics, a partial match will leave the
    // key-value pair in place, in the hope it may match something else.

    let key_node = &kv.key;
    let val_node = &kv.value;
    let cut = kv.cut;

    // If we fail to validate a key, exit now with an error.
    let (working_key, working_val) = validate_map_key(working_map, key_node, ctx)?;

    // Match the value that was returned.
    match validate(working_val, val_node, ctx) {
        Ok(()) => {
            working_map.remove(&working_key);
            Ok(())
        }
        Err(e) => {
            match (cut, e) {
                (true, ValidateError::Mismatch(m)) => {
                    // If "cut" semantics are in force, then rewrite Mismatch errors.
                    // This allows special handling when nested inside Occur nodes.
                    Err(ValidateError::MapCut(m))
                }
                (_, x) => Err(x),
            }
        }
    }
}

fn validate_standalone_group(g: &Group, value: &Value, ctx: &Context) -> ValidateResult {
    // Since we're not in an array or map context, it's not clear how we should
    // validate a group containing multiple elements.  If we see one, return an
    // error.
    match g.members.len() {
        1 => {
            // Since our group has length 1, validate against that single element.
            validate(value, &g.members[0], ctx)
        }
        _ => Err(ValidateError::Unsupported("standalone group".into())),
    }
}

fn deref_range_rule(node: &Node, ctx: &Context) -> TempResult<Literal> {
    match node {
        Node::Literal(l) => Ok(l.clone()),
        Node::Rule(r) => {
            let answer = ctx.lookup_rule(r)?;
            deref_range_rule(answer.node, &answer.ctx)
        }
        _ => Err(ValidateError::Structural(
            "confusing type on range operator".into(),
        )),
    }
}

// Returns true if value is within range
fn check_range<T: PartialOrd>(start: T, end: T, value: T, inclusive: bool) -> bool {
    if value < start {
        return false;
    }
    if inclusive {
        value <= end
    } else {
        value < end
    }
}

fn validate_range(range: &Range, value: &Value, ctx: &Context) -> ValidateResult {
    // first dereference rules on start and end, if necessary.
    let start = deref_range_rule(&range.start, ctx)?;
    let end = deref_range_rule(&range.end, ctx)?;

    match (&start, &end, &value) {
        (Literal::Int(i1), Literal::Int(i2), Value::Integer(v)) => {
            if check_range(i1, i2, v, range.inclusive) {
                Ok(())
            } else {
                Err(mismatch(format!("{}", range)))
            }
        }
        (Literal::Float(f1), Literal::Float(f2), Value::Float(v)) => {
            if check_range(f1, f2, &v.0, range.inclusive) {
                Ok(())
            } else {
                Err(mismatch(format!("{}", range)))
            }
        }
        _ => {
            if discriminant(&start) == discriminant(&end) {
                // The range types were the same, so this is just a mismatch.
                Err(mismatch(format!("{}", range)))
            } else {
                // The range types didn't agree; return an error that points the
                // finger at the CDDL instead.
                Err(ValidateError::Structural(
                    "mismatched types on range operator".into(),
                ))
            }
        }
    }
}

// Follow a chain of Rule references until we reach a non-Rule node.
fn chase_rules<'a, F, R>(node: &'a Node, ctx: &'a Context<'a>, f: F) -> TempResult<R>
where
    F: Fn(&Node) -> TempResult<R>,
    R: 'static,
{
    if let Node::Rule(rule) = node {
        let answer = ctx.lookup_rule(rule)?;
        chase_rules(answer.node, &answer.ctx, f)
    } else {
        f(node)
    }
}

fn validate_control(ctl: &Control, value: &Value, ctx: &Context) -> ValidateResult {
    match ctl {
        Control::Size(ctl_size) => validate_control_size(ctl_size, value, ctx),
        Control::Lt(ctl_lt) => validate_control_lt(ctl_lt, value, ctx),
        Control::Le(ctl_le) => validate_control_le(ctl_le, value, ctx),
        Control::Gt(ctl_gt) => validate_control_gt(ctl_gt, value, ctx),
        Control::Ge(ctl_ge) => validate_control_ge(ctl_ge, value, ctx),
        Control::Regexp(re) => validate_control_regexp(re, value),
        Control::Cbor(ctl_cbor) => validate_control_cbor(ctl_cbor, value, ctx),
    }
}

#[cfg(not(feature = "ciborium"))]
fn validate_control_cbor(_ctl_cbor: &CtlOpCbor, _value: &Value, _ctx: &Context) -> ValidateResult {
    Err(ValidateError::Unsupported(
        "'.cbor' control operator; enable ciborium feature to support.".into(),
    ))
}

#[cfg(feature = "ciborium")]
fn validate_control_cbor(ctl_cbor: &CtlOpCbor, value: &Value, ctx: &Context) -> ValidateResult {
    use ciborium::Value as CBOR_Value;
    use std::convert::TryFrom;

    match value {
        Value::Bytes(bytes) => {
            let cbor_value: CBOR_Value = ciborium::from_reader(bytes.as_slice())
                .map_err(|e| ValidateError::ValueError(format!("{}", e)))?;

            let nested_value = Value::try_from(cbor_value)?;

            validate(&nested_value, ctl_cbor.node.as_ref(), ctx)
        }
        _ => Err::<(), ValidateError>(mismatch("Bytes")),
    }
}

fn validate_control_size(ctl: &CtlOpSize, value: &Value, ctx: &Context) -> ValidateResult {
    let size: Range = chase_rules(&ctl.size, ctx, normalize_size_range)?;

    chase_rules(&ctl.target, ctx, |target_node| {
        // Ensure that the target node evaluates to some type that is
        // compatible with the .size operator, and then validate the size limit.
        match target_node {
            Node::PreludeType(PreludeType::Uint) => validate_size_uint(&size, value),
            Node::PreludeType(PreludeType::Tstr) => validate_size_tstr(&size, value),
            Node::PreludeType(PreludeType::Bstr) => validate_size_bstr(&size, value),
            _ => {
                let msg = format!("bad .size target type ({})", target_node);

                Err(ValidateError::Structural(msg))
            }
        }
    })
}

fn validate_control_lt(ctl: &CtlOpLt, value: &Value, ctx: &Context) -> ValidateResult {
    // Resolve the limit to an integer literal (rules are allowed).
    let lt: i128 = chase_rules(&ctl.lt, ctx, |lt_node| match lt_node {
        Node::Literal(Literal::Int(i)) => Ok(*i),
        _ => {
            let msg = format!("bad .lt argument type ({})", lt_node);
            Err(ValidateError::Structural(msg))
        }
    })?;

    chase_rules(&ctl.target, ctx, |target_node| {
        // Ensure the target type is compatible with `.lt`, then validate.
        match target_node {
            Node::PreludeType(PreludeType::Uint) => validate_lt_uint(lt, value),
            Node::PreludeType(PreludeType::Nint) => validate_lt_nint(lt, value),
            Node::PreludeType(PreludeType::Int) => validate_lt_int(lt, value),
            Node::PreludeType(PreludeType::Float) => Err(ValidateError::Structural(
                ".lt for float is not supported yet".into(),
            )),
            _ => {
                let msg = format!("bad .lt target type ({})", target_node);
                Err(ValidateError::Structural(msg))
            }
        }
    })
}

fn validate_control_le(ctl: &CtlOpLe, value: &Value, ctx: &Context) -> ValidateResult {
    // Resolve the limit to an integer literal (rules are allowed).
    let le: i128 = chase_rules(&ctl.le, ctx, |le_node| match le_node {
        Node::Literal(Literal::Int(i)) => Ok(*i),
        _ => {
            let msg = format!("bad .le argument type ({})", le_node);
            Err(ValidateError::Structural(msg))
        }
    })?;

    chase_rules(&ctl.target, ctx, |target_node| {
        // Ensure the target type is compatible with `.le`, then validate.
        match target_node {
            Node::PreludeType(PreludeType::Uint) => validate_le_uint(le, value),
            Node::PreludeType(PreludeType::Nint) => validate_le_nint(le, value),
            Node::PreludeType(PreludeType::Int) => validate_le_int(le, value),
            Node::PreludeType(PreludeType::Float) => Err(ValidateError::Structural(
                ".le for float is not supported yet".into(),
            )),
            _ => {
                let msg = format!("bad .le target type ({})", target_node);
                Err(ValidateError::Structural(msg))
            }
        }
    })
}

fn validate_control_gt(ctl: &CtlOpGt, value: &Value, ctx: &Context) -> ValidateResult {
    // Resolve the limit to an integer literal (rules are allowed).
    let gt: i128 = chase_rules(&ctl.gt, ctx, |gt_node| match gt_node {
        Node::Literal(Literal::Int(i)) => Ok(*i),
        _ => {
            let msg = format!("bad .gt argument type ({})", gt_node);
            Err(ValidateError::Structural(msg))
        }
    })?;

    chase_rules(&ctl.target, ctx, |target_node| {
        // Ensure the target type is compatible with `.gt`, then validate.
        match target_node {
            Node::PreludeType(PreludeType::Uint) => validate_gt_uint(gt, value),
            Node::PreludeType(PreludeType::Nint) => validate_gt_nint(gt, value),
            Node::PreludeType(PreludeType::Int) => validate_gt_int(gt, value),
            Node::PreludeType(PreludeType::Float) => Err(ValidateError::Structural(
                ".gt for float is not supported yet".into(),
            )),
            _ => {
                let msg = format!("bad .gt target type ({})", target_node);
                Err(ValidateError::Structural(msg))
            }
        }
    })
}

fn validate_control_ge(ctl: &CtlOpGe, value: &Value, ctx: &Context) -> ValidateResult {
    // Resolve the limit to an integer literal (rules are allowed).
    let ge: i128 = chase_rules(&ctl.ge, ctx, |ge_node| match ge_node {
        Node::Literal(Literal::Int(i)) => Ok(*i),
        _ => {
            let msg = format!("bad .ge argument type ({})", ge_node);
            Err(ValidateError::Structural(msg))
        }
    })?;

    chase_rules(&ctl.target, ctx, |target_node| {
        // Ensure the target type is compatible with `.ge`, then validate.
        match target_node {
            Node::PreludeType(PreludeType::Uint) => validate_ge_uint(ge, value),
            Node::PreludeType(PreludeType::Nint) => validate_ge_nint(ge, value),
            Node::PreludeType(PreludeType::Int) => validate_ge_int(ge, value),
            Node::PreludeType(PreludeType::Float) => Err(ValidateError::Structural(
                ".ge for float is not supported yet".into(),
            )),
            _ => {
                let msg = format!("bad .ge target type ({})", target_node);
                Err(ValidateError::Structural(msg))
            }
        }
    })
}

/// Validate the control operator "regexp"
///
/// `regexp` applies a regular expression to a text string.
///
fn validate_control_regexp(re: &CtlOpRegexp, value: &Value) -> ValidateResult {
    match value {
        Value::Text(text) => {
            if re.re.is_match(text) {
                Ok(())
            } else {
                Err(mismatch("regex mismatch"))
            }
        }
        _ => Err(mismatch("tstr")),
    }
}

fn validate_size_uint(size: &Range, value: &Value) -> ValidateResult {
    let (start_i, end_i) = match (size.start.as_ref(), size.end.as_ref()) {
        (Node::Literal(Literal::Int(a)), Node::Literal(Literal::Int(b))) => (*a, *b),
        _ => {
            return Err(ValidateError::Structural(
                "bad .size range endpoints for uint".into(),
            ))
        }
    };

    let start_u: u64 = start_i
        .try_into()
        .map_err(|_| ValidateError::Structural(format!("bad .size limit {}", start_i)))?;
    let end_u: u64 = end_i
        .try_into()
        .map_err(|_| ValidateError::Structural(format!("bad .size limit {}", end_i)))?;

    if start_u > end_u || (start_u == end_u && !size.inclusive) {
        return Err(mismatch("uint over .size limit"));
    }

    match value {
        Value::Integer(x) => {
            if *x < 0 {
                return Err(mismatch(".size on negative integer"));
            }

            // Degenerate case (normalized literal `.size N` -> [N..N]):
            // For uint, spec defines `.size N` as value-range 0...(256**N), NOT "needs exactly N bytes".
            if start_u == end_u && size.inclusive {
                let n = end_u;

                // If N is 16 or larger, any non-negative i128 is < 2^127, hence < 2^(8N) for N>=16.
                if n >= 16 {
                    return Ok(());
                }

                // Require x < 256**N  (exclusive upper bound)
                let limit = 1i128 << (n * 8);
                return if *x < limit {
                    Ok(())
                } else {
                    Err(mismatch("uint over .size limit"))
                };
            }

            // Proper range semantics: constrain the *minimal required* number of bytes.
            // required bytes:
            // 0 -> 0
            // 1..255 -> 1
            // 256..65535 -> 2, etc.
            let v: u128 = *x as u128;
            let needed: u64 = if v == 0 {
                0
            } else {
                let bits = 128u32 - v.leading_zeros(); // 1..128
                (bits as u64).div_ceil(8)
            };

            // Enforce lower bound (inclusive)
            if needed < start_u {
                return Err(mismatch("uint under .size limit"));
            }

            // Enforce upper bound (inclusive/exclusive)
            let ok_max = if size.inclusive {
                needed <= end_u
            } else {
                needed < end_u
            };

            if ok_max {
                Ok(())
            } else {
                Err(mismatch("uint over .size limit"))
            }
        }
        _ => Err(mismatch("uint")),
    }
}

// Check the size of a text string.
fn validate_size_tstr(size: &Range, value: &Value) -> ValidateResult {
    let (start_i, end_i) = match (size.start.as_ref(), size.end.as_ref()) {
        (Node::Literal(Literal::Int(a)), Node::Literal(Literal::Int(b))) => (*a, *b),
        _ => {
            return Err(ValidateError::Structural(
                "bad .size range endpoints for tstr".into(),
            ))
        }
    };

    let min_u: u64 = start_i
        .try_into()
        .map_err(|_| ValidateError::Structural(format!("bad .size limit {}", start_i)))?;
    let max_u: u64 = end_i
        .try_into()
        .map_err(|_| ValidateError::Structural(format!("bad .size limit {}", end_i)))?;

    if min_u > max_u || (min_u == max_u && !size.inclusive) {
        return Err(mismatch("tstr over .size limit"));
    }

    match value {
        Value::Text(s) => {
            let len_u: u64 = s.len() as u64;

            if len_u < min_u {
                return Err(mismatch("tstr under .size limit"));
            }

            let ok_max = if size.inclusive {
                len_u <= max_u
            } else {
                len_u < max_u
            };
            if !ok_max {
                return Err(mismatch("tstr over .size limit"));
            }

            Ok(())
        }
        _ => Err(mismatch("tstr")),
    }
}

// Check the size of a byte string.
fn validate_size_bstr(size: &Range, value: &Value) -> ValidateResult {
    let (start_i, end_i) = match (size.start.as_ref(), size.end.as_ref()) {
        (Node::Literal(Literal::Int(a)), Node::Literal(Literal::Int(b))) => (*a, *b),
        _ => {
            return Err(ValidateError::Structural(
                "bad .size range endpoints for bstr".into(),
            ))
        }
    };

    let min_u: u64 = start_i
        .try_into()
        .map_err(|_| ValidateError::Structural(format!("bad .size limit {}", start_i)))?;
    let max_u: u64 = end_i
        .try_into()
        .map_err(|_| ValidateError::Structural(format!("bad .size limit {}", end_i)))?;

    if min_u > max_u || (min_u == max_u && !size.inclusive) {
        return Err(mismatch("bstr over .size limit"));
    }

    match value {
        Value::Bytes(b) => {
            let len_u: u64 = b.len().try_into().unwrap_or(u64::MAX);

            if len_u < min_u {
                return Err(mismatch("bstr under .size limit"));
            }

            let ok_max = if size.inclusive {
                len_u <= max_u
            } else {
                len_u < max_u
            };
            if !ok_max {
                return Err(mismatch("bstr over .size limit"));
            }

            Ok(())
        }
        _ => Err(mismatch("bstr")),
    }
}

fn validate_lt_uint(lt: i128, value: &Value) -> ValidateResult {
    match value {
        Value::Integer(x) => {
            // uint domain restriction
            if *x < 0 {
                Err(mismatch("uint"))
            } else if *x < lt {
                Ok(())
            } else {
                Err(mismatch("uint over .lt limit"))
            }
        }
        _ => Err(mismatch("uint")),
    }
}

fn validate_lt_nint(lt: i128, value: &Value) -> ValidateResult {
    match value {
        Value::Integer(x) => {
            // nint domain restriction: must be negative
            if *x >= 0 {
                Err(mismatch("nint"))
            } else if *x < lt {
                Ok(())
            } else {
                Err(mismatch("nint over .lt limit"))
            }
        }
        _ => Err(mismatch("nint")),
    }
}

fn validate_lt_int(lt: i128, value: &Value) -> ValidateResult {
    match value {
        Value::Integer(x) => {
            if *x < lt {
                Ok(())
            } else {
                Err(mismatch("int over .lt limit"))
            }
        }
        _ => Err(mismatch("int")),
    }
}

fn validate_le_uint(lt: i128, value: &Value) -> ValidateResult {
    match value {
        Value::Integer(x) => {
            // uint domain restriction
            if *x < 0 {
                Err(mismatch("uint"))
            } else if *x <= lt {
                Ok(())
            } else {
                Err(mismatch("uint over .le limit"))
            }
        }
        _ => Err(mismatch("uint")),
    }
}

fn validate_le_nint(lt: i128, value: &Value) -> ValidateResult {
    match value {
        Value::Integer(x) => {
            // nint domain restriction: must be negative
            if *x >= 0 {
                Err(mismatch("nint"))
            } else if *x <= lt {
                Ok(())
            } else {
                Err(mismatch("nint over .lt limit"))
            }
        }
        _ => Err(mismatch("nint")),
    }
}

fn validate_le_int(lt: i128, value: &Value) -> ValidateResult {
    match value {
        Value::Integer(x) => {
            if *x <= lt {
                Ok(())
            } else {
                Err(mismatch("int over .le limit"))
            }
        }
        _ => Err(mismatch("int")),
    }
}

fn validate_gt_uint(lt: i128, value: &Value) -> ValidateResult {
    match value {
        Value::Integer(x) => {
            // uint domain restriction
            if *x < 0 {
                Err(mismatch("uint"))
            } else if *x > lt {
                Ok(())
            } else {
                Err(mismatch("uint under .gt limit"))
            }
        }
        _ => Err(mismatch("uint")),
    }
}

fn validate_gt_nint(lt: i128, value: &Value) -> ValidateResult {
    match value {
        Value::Integer(x) => {
            // nint domain restriction: must be negative
            if *x >= 0 {
                Err(mismatch("nint"))
            } else if *x > lt {
                Ok(())
            } else {
                Err(mismatch("nint over .gt limit"))
            }
        }
        _ => Err(mismatch("nint")),
    }
}

fn validate_gt_int(lt: i128, value: &Value) -> ValidateResult {
    match value {
        Value::Integer(x) => {
            if *x > lt {
                Ok(())
            } else {
                Err(mismatch("int over .ge limit"))
            }
        }
        _ => Err(mismatch("int")),
    }
}

fn validate_ge_uint(lt: i128, value: &Value) -> ValidateResult {
    match value {
        Value::Integer(x) => {
            // uint domain restriction
            if *x < 0 {
                Err(mismatch("uint"))
            } else if *x >= lt {
                Ok(())
            } else {
                Err(mismatch("uint under .ge limit"))
            }
        }
        _ => Err(mismatch("uint")),
    }
}

fn validate_ge_nint(lt: i128, value: &Value) -> ValidateResult {
    match value {
        Value::Integer(x) => {
            // nint domain restriction: must be negative
            if *x >= 0 {
                Err(mismatch("nint"))
            } else if *x >= lt {
                Ok(())
            } else {
                Err(mismatch("nint over .ge limit"))
            }
        }
        _ => Err(mismatch("nint")),
    }
}

fn validate_ge_int(lt: i128, value: &Value) -> ValidateResult {
    match value {
        Value::Integer(x) => {
            if *x >= lt {
                Ok(())
            } else {
                Err(mismatch("int over .ge limit"))
            }
        }
        _ => Err(mismatch("int")),
    }
}

fn validate_choiceify_members(choices: &[Node], value: &Value, ctx: &Context) -> ValidateResult {
    // Iterate over the input nodes. We expect a list of KeyValue variants.
    // For each KeyValue, extract its .value member and try to validate that.
    // Because we are in a group context, referring to other groups by name is
    // also allowed; we will transparently unwrap those (recursively).
    for item in choices {
        let validate_result = match item {
            Node::KeyValue(kv) => validate(value, &kv.value, ctx),
            Node::Rule(rule) => {
                // A group may include another group by name. Handling this:
                // Dereference the rule. If it leads to a Group, then
                // recursively walk the nodes in that group. Otherwise,
                // return an error.
                validate_choiceify(rule, value, ctx)
            }
            _ => {
                // Reading the CDDL spec, you might expect this case to be
                // un-necessary. However, the flatten code always simplifies
                // a keyless KeyValue node into just a value. Which means
                // that any type might appear here.
                validate(value, item, ctx)
            }
        };

        // This part is the same as validate_choice().
        // If we matched, return Ok immediately.
        // If we didn't, keep searching (unless the error is fatal).
        match validate_result {
            Ok(()) => {
                return Ok(());
            }
            Err(e) => {
                // Only fail if the error is considered fatal.
                // Otherwise, we'll keep trying other options.
                if e.is_fatal() {
                    return Err(e);
                }
            }
        }
    }
    let expected = format!("choiceified group of {}", choices.len());
    Err(mismatch(expected))
}

/// Validate a "choice-ified group" (the CDDL "&" operator)
///
/// Each value in the named group will be used as a possible choice.
fn validate_choiceify(rule: &Rule, value: &Value, ctx: &Context) -> ValidateResult {
    let NodeContext { node, ctx } = ctx.lookup_rule(rule)?;
    match node {
        Node::Group(g) => validate_choiceify_members(&g.members, value, &ctx),
        Node::Rule(r) => validate_choiceify(r, value, &ctx),
        _ => Err(ValidateError::Structural(
            "improper choiceify target".into(),
        )),
    }
}

/// Validate an inline "choice-ified group" (the CDDL "&" operator)
///
/// Each value in the inline group will be used as a possible choice.
fn validate_choiceify_inline(array: &Array, value: &Value, ctx: &Context) -> ValidateResult {
    validate_choiceify_members(&array.members, value, ctx)
}