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
//! This module defines the Intermediate Validation Tree.
//!
//! It contains a simplified representation of a CDDL rule, flattened to only
//! include the parts that are necessary for validation.
//!
//! This module doesn't know anything about validating specific types (e.g.
//! CBOR or JSON), but it helps make writing those validators easier.

use std::collections::BTreeMap;
use std::fmt;

use strum_macros::{Display, IntoStaticStr};

use crate::ast;

/// The definition of a CDDL rule.
///
/// Each rule has a name, some (optional) generic parameters, and a
/// definition `Node`.
#[derive(Clone, Debug, PartialEq)]
pub struct RuleDef {
    /// Optional generic parameters.
    pub generic_parms: Vec<String>,
    /// The Node representing the rule definition.
    pub node: Node,
}

/// A set of CDDL rules.
///
/// Each rule has a name, some (optional) generic parameters, and a
/// definition `Node`.
pub type RulesByName = BTreeMap<String, RuleDef>;

/// A set of CDDL rules.
///
/// Each rule has a name, some (optional) generic parameters, and a
/// definition `Node`.
///
/// `RulesWithStrings` is exactly like `RulesByName`, except that it
/// preserves the original CDDL text for the rule, to assist in debugging.
pub type RulesWithStrings = BTreeMap<String, (RuleDef, String)>;

/// One of the types named in the CDDL prelude.
///
/// The following types are defined in [RFC8610 appendix D]:
/// `any`, `bool`, `int`, `uint`, `float`, `tstr`, `bstr`.
/// There are more that aren't supported by this crate yet.
///
/// [RFC8610 appendix D]: https://tools.ietf.org/html/rfc8610#appendix-D
#[derive(Debug, Copy, Clone, PartialEq, Eq, Display)]
#[allow(missing_docs)]
pub enum PreludeType {
    /// Any type or embedded data structure
    Any,
    /// Nil aka null: nothing.
    Nil,
    /// A boolean value: true or false
    Bool,
    /// A positive or negative integer
    Int,
    /// An integer >= 0
    Uint,
    /// An integer < 0
    Nint,
    /// A floating-point value
    Float,
    /// A text string
    Tstr,
    /// A byte string
    Bstr,
}

/// A literal value, e.g. `7`, `1.3`, or ``"foo"``.
#[derive(Debug, Clone, PartialEq)]
#[allow(missing_docs)]
pub enum Literal {
    Bool(bool),
    Int(i128),
    Float(f64),
    Text(String),
    Bytes(Vec<u8>),
    // TODO: nil?
}

impl fmt::Display for Literal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Literal::Bool(b) => write!(f, "{}", b),
            Literal::Int(i) => write!(f, "{}", i),
            // FIXME: it's annoying that floating point values can omit the
            // decimal, which can be confused for an integer.
            Literal::Float(fl) => write!(f, "{}", fl),
            Literal::Text(s) => write!(f, "\"{}\"", s),
            Literal::Bytes(_) => write!(f, "LiteralBytes"),
        }
    }
}

/// A shortcut for `Node::Literal(Literal::Bool(b))`
pub fn literal_bool(b: bool) -> Node {
    Node::Literal(Literal::Bool(b))
}

/// A shortcut for `Node::Literal(Literal::Int(i))`
///
/// This doesn't work for isize and usize, unfortunately.
pub fn literal_int<T: Into<i128>>(i: T) -> Node {
    Node::Literal(Literal::Int(i.into()))
}

/// A shortcut for `Node::Literal(Literal::Float(f))`
pub fn literal_float<T: Into<f64>>(f: T) -> Node {
    Node::Literal(Literal::Float(f.into()))
}

/// A shortcut for `Node::Literal(Literal::Text(t))`
pub fn literal_text<T: Into<String>>(s: T) -> Node {
    Node::Literal(Literal::Text(s.into()))
}

/// A shortcut for `Node::Literal(Literal::Bytes(b))`
pub fn literal_bytes<T: Into<Vec<u8>>>(b: T) -> Node {
    Node::Literal(Literal::Bytes(b.into()))
}

/// A rule reference, linked to a dispatch object for later resolution.
///
/// Resolving the rule reference is handled by the validation context.
#[derive(Debug, Clone, PartialEq)]
#[allow(missing_docs)]
pub struct Rule {
    pub name: String,
    pub generic_args: Vec<Node>,
}

impl Rule {
    // Create a new rule reference by name
    #[doc(hidden)] // Only pub for integration tests
    pub fn new(name: &str, generic_args: Vec<Node>) -> Rule {
        Rule {
            name: name.to_string(),
            generic_args,
        }
    }

    #[doc(hidden)] // Only pub for integration tests
    pub fn new_name(name: &str) -> Rule {
        Rule {
            name: name.to_string(),
            generic_args: Vec::new(),
        }
    }
}

/// A Choice validates if any one of a set of options validates.
#[derive(Debug, Clone, PartialEq)]
#[allow(missing_docs)]
pub struct Choice {
    pub options: Vec<Node>,
}

/// A key-value pair; key and value can be anything (types, arrays, maps, etc.)
///
/// "Cut" means a match on this key will prevent any later keys from matching.
///
/// For example, a map containing `"optional-key": "hello"`
/// would be permitted to match the following:
///
/// ```text
/// extensible-map-example = {
///     ? "optional-key" => int,
///     * tstr => any
/// }
/// ```
/// If we add the cut symbol `^`, the same map would not match:
///
/// ```text
/// extensible-map-example = {
///     ? "optional-key" ^ => int,
///     * tstr => any
/// }
/// ```
///
/// Note: CDDL map members that use `:` always use cut semantics.
///
/// See RFC8610 3.5.4 for more discussion.
///
#[derive(Clone, PartialEq)]
#[allow(missing_docs)]
pub struct KeyValue {
    pub key: Box<Node>,
    pub value: Box<Node>,
    pub cut: IsCut,
}

pub(crate) type IsCut = bool;

impl KeyValue {
    #[doc(hidden)] // Only pub for integration tests
    pub fn new(key: Node, value: Node, cut: IsCut) -> KeyValue {
        KeyValue {
            key: Box::new(key),
            value: Box::new(value),
            cut,
        }
    }
}

impl fmt::Display for KeyValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", self.key, self.value)
    }
}

// Implement Debug by hand so we can format it like a map.
impl fmt::Debug for KeyValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut formatter = f.debug_tuple("KeyValue");
        formatter.field(&self.key).field(&self.value).finish()
    }
}

/// Specify a CDDL occurrence's limits.
///
/// An "occurrence" in CDDL specifies how many times a value should repeat
/// (in an array) or flag optional map keys.  [RFC8610] specifies the following
/// occurrence symbols:
/// ```text
/// "?" Optional
/// "*" Zero or more
/// "+" One or more
/// n*m Between n and m, inclusive (n and m are both optional)
/// ```
///
/// [RFC8610]: https://tools.ietf.org/html/rfc8610
///
// Re-use the Occur limit type that the parser uses
pub type OccurLimit = ast::Occur;

/// Occurences specify how many times a value can appear.
///
/// This implementation wraps the Node that the occurrence applies to.

#[derive(Debug, Clone, PartialEq)]
#[allow(missing_docs)]
pub struct Occur {
    pub limit: OccurLimit,
    pub node: Box<Node>,
}

impl Occur {
    /// Creates a new Occur from one of the CDDL occurrence chars ?*+
    pub fn new(limit: OccurLimit, node: Node) -> Occur {
        Occur {
            limit,
            node: Box::new(node),
        }
    }

    /// Get the CDDL symbol for this occurrence.
    ///
    /// Returns `?`, `*`, `+`, or `n*m`
    pub fn symbol(&self) -> String {
        match self.limit {
            OccurLimit::Optional => "?".into(),
            OccurLimit::ZeroOrMore => "*".into(),
            OccurLimit::OneOrMore => "+".into(),
            OccurLimit::Numbered(n, m) => match (n, m) {
                (0, std::usize::MAX) => format!("{}*", n),
                (_, _) => format!("{}*{}", n, m),
            },
        }
    }

    /// Return the lower and upper limits on this occurrence
    ///
    /// Occurrences can always be represented by an inclusive [lower, upper]
    /// count limit.
    ///
    /// required         => [1, 1]
    /// optional "?"     => [0, 1]
    /// zero-or-more "*" => [0, MAX]
    /// one-or-more "+"  => [1, MAX]
    pub fn limits(&self) -> (usize, usize) {
        match self.limit {
            OccurLimit::Optional => (0, 1),
            OccurLimit::ZeroOrMore => (0, usize::MAX),
            OccurLimit::OneOrMore => (1, usize::MAX),
            OccurLimit::Numbered(n, m) => (n, m),
        }
    }
}

impl fmt::Display for Occur {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} {}", self.symbol(), self.node)
    }
}

/// A map containing key-value pairs.
#[derive(Debug, Clone, PartialEq)]
#[allow(missing_docs)]
pub struct Map {
    pub members: Vec<Node>,
}

/// A context-free group of key-value pairs.
#[derive(Debug, Clone, PartialEq)]
#[allow(missing_docs)]
pub struct Group {
    pub members: Vec<Node>,
}

/// An array is a list of types in a specific order.
///
/// Arrays are expected to take the form of "records" or "vectors".
///
/// A "vector" array is expected to have an arbitrary-length list of a single
/// type, e.g. zero-or-more integers:
/// ```text
/// [ * int ]
/// ```
/// The type in a vector could be something complex, like a group, choice, or
/// another array or map.
///
/// A "record" array is a sequence of different values, each with a specific
/// type.  It has similar semantics to a rust tuple, though it could also
/// theoretically be used to serialize a struct.
///
/// CDDL syntax allows certain nonsensical or ambiguous arrays, for example:
/// ```text
/// thing = [ * mygroup ]
/// mygroup = ( a = tstr, b = int)
/// ```
/// or
/// ```text
/// thing = [ * "a" = int, * "b" = int ]
/// ```
///
/// CDDL arrays may be composed of key-value pairs, but the keys are solely
/// for information/debugging; they are ignored for validation purposes.
///
#[derive(Debug, Clone, PartialEq)]
#[allow(missing_docs)]
pub struct Array {
    pub members: Vec<Node>,
}

/// A range of numbers.
///
/// Ranges can be defined as inclusive (`..`) or exclusive (`...`).
///
/// CDDL only defines ranges between two integers or between two floating
/// point values.  A lower bound that exceeds the upper bound is valid CDDL,
/// but behaves as an empty set.
#[derive(Debug, Clone, PartialEq)]
#[allow(missing_docs)]
pub struct Range {
    pub start: Box<Node>,
    pub end: Box<Node>,
    pub inclusive: bool,
}

impl fmt::Display for Range {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let op = if self.inclusive { ".." } else { "..." };
        write!(f, "{}{}{}", self.start, op, self.end)
    }
}

/// Control Operators
///
/// A control operator constrains a type by adding an additional condition
/// that must be met. For example, "tstr .size 10" permits only strings of
/// 10 bytes or less.  See RFC 8610 section 3.8 for details.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum Control {
    /// Limit the size in bytes.
    Size(CtlOpSize),
    /// Limit the numeric value by the upper bound (exclusive).
    Lt(CtlOpLt),
    /// Limit the numeric value by the upper bound (inclusive).
    Le(CtlOpLe),
    /// Limit the numeric value by the lower bound (exclusive).
    Gt(CtlOpGt),
    /// Limit the numeric value by the lower bound (inclusive).
    Ge(CtlOpGe),
    /// Apply a regular expression to a text string.
    Regexp(CtlOpRegexp),
    /// Validate a nested CBOR bytestring
    Cbor(CtlOpCbor),
}

/// Control Operator `.size`
///
/// `.size` is defined in RFC 8610 3.8.1.
/// It sets an upper limit, measured in bytes.
///
/// For example, "tstr .size 10" permits only strings of
/// 10 bytes or less.  See RFC 8610 section 3.8 for details.
#[derive(Debug, Clone, PartialEq)]
pub struct CtlOpSize {
    /// The type that is size-constrained.
    ///
    /// Only certain types are permitted.  RFC 8610 defines `.size` for
    /// `tstr`, `bstr`, and unsigned integers.
    pub target: Box<Node>,
    /// The size limit, in bytes.
    pub size: Box<Node>,
}

/// Control Operator `.lt`
///
/// `.lt` is defined in RFC 8610 section 3.8.6.
/// It sets an exclusive upper bound on the numeric value.
///
/// For example, `uint .lt 10` permits only integers from 0 to 9.
///
/// Note: while RFC 8610 defines `.lt` for both integers and floating-point
/// numbers, this implementation currently supports integer comparisons only.
#[derive(Debug, Clone, PartialEq)]
pub struct CtlOpLt {
    /// The type that is limit-constrained.
    ///
    /// Only numeric types are permitted. In this implementation,
    /// `.lt` applies to integer values.
    pub target: Box<Node>,

    /// The numeric limit.
    ///
    /// This must resolve (via rules) to an integer literal.
    pub lt: Box<Node>,
}

/// Control Operator `.le`
///
/// `.le` is defined in RFC 8610 section 3.8.6.
/// It sets an inclusive upper bound on the numeric value.
///
/// For example, `uint .le 10` permits only integers from 0 to 10.
///
/// Note: while RFC 8610 defines `.le` for both integers and floating-point
/// numbers, this implementation currently supports integer comparisons only.
#[derive(Debug, Clone, PartialEq)]
pub struct CtlOpLe {
    /// The type that is limit-constrained.
    ///
    /// Only numeric types are permitted. In this implementation,
    /// `.le` applies to integer values.
    pub target: Box<Node>,

    /// The numeric limit.
    ///
    /// This must resolve (via rules) to an integer literal.
    pub le: Box<Node>,
}

/// Control Operator `.gt`
///
/// `.gt` is defined in RFC 8610 section 3.8.6.
/// It sets an exclusive lower bound on the numeric value.
///
/// For example, `uint .gt 10` permits only integers bigger than 10 (exclusive).
///
/// Note: while RFC 8610 defines `.gt` for both integers and floating-point
/// numbers, this implementation currently supports integer comparisons only.
#[derive(Debug, Clone, PartialEq)]
pub struct CtlOpGt {
    /// The type that is limit-constrained.
    ///
    /// Only numeric types are permitted. In this implementation,
    /// `.gt` applies to integer values.
    pub target: Box<Node>,

    /// The numeric limit.
    ///
    /// This must resolve (via rules) to an integer literal.
    pub gt: Box<Node>,
}

/// Control Operator `.ge`
///
/// `.ge` is defined in RFC 8610 section 3.8.6.
/// It sets an exclusive lower bound on the numeric value.
///
/// For example, `uint .ge 10` permits only integers bigger than 10 (inclusive).
///
/// Note: while RFC 8610 defines `.ge` for both integers and floating-point
/// numbers, this implementation currently supports integer comparisons only.
#[derive(Debug, Clone, PartialEq)]
pub struct CtlOpGe {
    /// The type that is limit-constrained.
    ///
    /// Only numeric types are permitted. In this implementation,
    /// `.ge` applies to integer values.
    pub target: Box<Node>,

    /// The numeric limit.
    ///
    /// This must resolve (via rules) to an integer literal.
    pub ge: Box<Node>,
}

/// Control Operator `.regexp`
///
/// `.regexp` is defined in RFC 8610 3.8.3.
///
#[derive(Debug, Clone)]
pub struct CtlOpRegexp {
    /// The regular expression, in compiled form.
    pub(crate) re: regex::Regex,
}

impl PartialEq for CtlOpRegexp {
    fn eq(&self, other: &Self) -> bool {
        // We only need to compare the string form,
        // not the compiled form.
        self.re.as_str() == other.re.as_str()
    }
}

/// Control Operator `.cbor`
///
/// `.cbor` is defined in RFC 8610 3.8.4
///
/// A ".cbor" control on a byte string indicates that the byte string
/// carries a CBOR-encoded data item.  Decoded, the data item matches the
/// type given as the right-hand-side argument.
#[derive(Debug, Clone, PartialEq)]
pub struct CtlOpCbor {
    /// The nested node to satisfy
    pub(crate) node: Box<Node>,
}

/// Any node in the Intermediate Validation Tree.
#[derive(Debug, Clone, PartialEq, IntoStaticStr)]
#[allow(missing_docs)]
pub enum Node {
    Literal(Literal),
    PreludeType(PreludeType),
    Rule(Rule),
    Choice(Choice),
    Map(Map),
    Array(Array),
    Group(Group),
    KeyValue(KeyValue),
    Occur(Occur),
    Unwrap(Rule),
    Range(Range),
    Control(Control),
    Choiceify(Rule),
    ChoiceifyInline(Array),
}

impl fmt::Display for Node {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Node::Literal(l) => write!(f, "{}", l),
            Node::PreludeType(p) => write!(f, "{}", p),
            Node::KeyValue(kv) => write!(f, "{}", kv),
            _ => {
                let variant: &str = self.into();
                write!(f, "{}", variant)
            }
        }
    }
}