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
/// CBOR validation implementation
pub mod cbor;

/// JSON validation implementation
pub mod json;

use crate::{ast::*, parser::ParserError, token::Numeric};
use std::{fmt, result};

/// Alias for `Result` with an error of type `validator::ValidationError`
pub type Result = result::Result<(), Error>;

/// Validation error types
#[derive(Debug)]
pub enum Error {
  /// CDDL syntax error, specific to the target data structure being validated
  Syntax(String),
  /// Error validating specific target data structure (i.e. JSON or CBOR)
  Target(Box<dyn std::error::Error>),
  /// Error compiling CDDL and/or target data structure
  Compilation(CompilationError),
  /// Occurrence error
  Occurrence(String),
  /// Aggregate errors
  MultiError(Vec<Error>),
}

impl fmt::Display for Error {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      Error::Syntax(se) => write!(f, "CDDL syntax error: {}", se),
      Error::Target(te) => write!(f, "{}", te),
      Error::Compilation(ce) => write!(f, "error on compilation: {}", ce),
      Error::Occurrence(oe) => write!(f, "occurrence error: {}", oe),
      Error::MultiError(me) => {
        let mut errors = String::new();

        for e in me.iter() {
          match e {
            // Temporary work around for nested MultiError's
            Error::MultiError(_) => errors.push_str(&format!("{}", e)),
            _ => errors.push_str(&format!("{}\n\n", e)),
          }
        }

        write!(f, "{}", errors)
      }
    }
  }
}

impl std::error::Error for Error {
  fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
    match self {
      Error::Compilation(ce) => Some(ce),
      Error::Target(te) => Some(te.as_ref()),
      _ => None,
    }
  }
}

/// Compilation errors
#[derive(Debug)]
pub enum CompilationError {
  /// Error compiling CDDL data definition
  CDDL(ParserError),
  /// Error compiling data target (i.e. JSON or CBOR)
  Target(Box<dyn std::error::Error>),
}

impl fmt::Display for CompilationError {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      CompilationError::CDDL(ce) => write!(f, "{}", ce),
      CompilationError::Target(te) => write!(f, "{}", te),
    }
  }
}

impl std::error::Error for CompilationError {
  fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
    match self {
      CompilationError::CDDL(ce) => Some(ce),
      CompilationError::Target(te) => Some(te.as_ref()),
    }
  }
}

/// Required behavior of a validator over different data types
pub trait Validator<T> {
  /// Initiate validation
  fn validate(&self, value: &T) -> Result;

  /// Validate data against the rule with the given identifier
  fn validate_rule_for_ident(
    &self,
    ident: &Identifier,
    is_enumeration: bool,
    expected_memberkey: Option<String>,
    actual_memberkey: Option<String>,
    occur: Option<&Occur>,
    value: &T,
  ) -> Result;

  /// Validate data against a given type rule
  fn validate_type_rule(
    &self,
    tr: &TypeRule,
    expected_memberkey: Option<String>,
    actual_memberkey: Option<String>,
    occur: Option<&Occur>,
    value: &T,
  ) -> Result;

  /// Validate data against a given group rule
  fn validate_group_rule(
    &self,
    gr: &GroupRule,
    is_enumeration: bool,
    occur: Option<&Occur>,
    value: &T,
  ) -> Result;

  /// Validate data against a given type, which may include additional type
  /// choices
  fn validate_type(
    &self,
    t: &Type,
    expected_memberkey: Option<String>,
    actual_memberkey: Option<String>,
    occur: Option<&Occur>,
    value: &T,
  ) -> Result;

  /// Validate data against a given type, which may include an optional range or
  /// control operator
  fn validate_type1(
    &self,
    t1: &Type1,
    expected_memberkey: Option<String>,
    actual_memberkey: Option<String>,
    occur: Option<&Occur>,
    value: &T,
  ) -> Result;

  /// Validate data against a given range operation between a lower and upper
  /// bound
  fn validate_range(&self, lower: &Type2, upper: &Type2, is_inclusive: bool, value: &T) -> Result;

  /// Validate data against a given control operator defined by a target type
  /// and a controller types
  fn validate_control_operator(
    &self,
    target: &Type2,
    operator: &'static str,
    controller: &Type2,
    value: &T,
  ) -> Result;

  /// Validate data against a given type
  fn validate_type2(
    &self,
    t2: &Type2,
    expected_memberkey: Option<String>,
    actual_memberkey: Option<String>,
    occur: Option<&Occur>,
    value: &T,
  ) -> Result;

  /// Validate data against a given group
  fn validate_group(&self, g: &Group, occur: Option<&Occur>, value: &T) -> Result;

  /// Validate data against a given group-to-choice enumeration
  fn validate_group_to_choice_enum(&self, g: &Group, occur: Option<&Occur>, value: &T) -> Result;

  /// Validate data against a given group choice
  fn validate_group_choice(&self, gc: &GroupChoice, occur: Option<&Occur>, value: &T) -> Result;

  /// Validate data against a given group entry
  fn validate_group_entry(
    &self,
    ge: &GroupEntry,
    is_enumeration: bool,
    wildcard_entry: Option<&Type>,
    occur: Option<&Occur>,
    value: &T,
  ) -> Result;

  /// Validate data against a given array and occurrence indicator
  fn validate_array_occurrence(&self, occur: &Occur, group: &str, values: &[T]) -> Result;

  /// Expect data to be a boolean
  fn expect_bool(&self, ident: &str, value: &T) -> Result;

  /// Validate data against a given numeric data type
  fn validate_numeric_data_type(
    &self,
    expected_memberkey: Option<String>,
    actual_memberkey: Option<String>,
    ident: &str,
    value: &T,
  ) -> Result;
}

impl CDDL {
  fn numerical_value_type_from_ident(&self, ident: &Identifier) -> Option<Vec<&Type2>> {
    let mut type_choices = Vec::new();

    for rule in self.rules.iter() {
      match rule {
        Rule::Type(tr) if tr.name == *ident => {
          for tc in tr.value.0.iter() {
            match &tc.type2 {
              Type2::IntValue(_) | Type2::UintValue(_) | Type2::FloatValue(_) => {
                type_choices.push(&tc.type2);
              }
              Type2::Typename((ident, _)) => return self.numerical_value_type_from_ident(ident),
              _ => continue,
            }
          }
        }
        _ => continue,
      }
    }

    if !type_choices.is_empty() {
      return Some(type_choices);
    }

    None
  }

  // Checks whether or not a given type is a type name identifier and that it
  // resolves to a text string data type (text | tstr)
  fn is_type_string_data_type(&self, t2: &Type2) -> bool {
    match t2 {
      Type2::Typename((Identifier((ident, _)), _)) if *ident == "text" || *ident == "tstr" => true,
      Type2::Typename((ident, _)) => self.rules.iter().any(|r| match r {
        Rule::Type(tr) if tr.name == *ident => tr
          .value
          .0
          .iter()
          .any(|tc| self.is_type_string_data_type(&tc.type2)),
        _ => false,
      }),
      _ => false,
    }
  }

  fn is_type_numeric_data_type(&self, t2: &Type2) -> bool {
    match t2 {
      Type2::Typename((Identifier((ident, _)), _)) if is_numeric_data_type(ident) => true,
      Type2::Typename((ident, _)) => self.rules.iter().any(|r| match r {
        Rule::Type(tr) if tr.name == *ident => tr
          .value
          .0
          .iter()
          .any(|tc| self.is_type_numeric_data_type(&tc.type2)),
        _ => false,
      }),
      _ => false,
    }
  }

  // Returns the text value(s) from a given type
  fn text_values_from_type(&self, ident: &Type2) -> result::Result<Vec<String>, Error> {
    match ident {
      Type2::TextValue(t) => Ok(vec![t.into()]),
      Type2::Typename((ident, _)) => {
        let mut text_values = Vec::new();

        for r in self.rules.iter() {
          match r {
            Rule::Type(tr) if tr.name == *ident => {
              for tc in tr.value.0.iter() {
                text_values.append(&mut self.text_values_from_type(&tc.type2)?);
              }
            }
            _ => continue,
          }
        }

        Ok(text_values)
      }
      _ => Err(Error::Syntax(
        "Value can only be referenced via another type name identifier".into(),
      )),
    }
  }

  // Returns the numeric value(s) from a given type, provided their types match
  // that of the given target data type
  fn numeric_values_from_type(
    &self,
    target: &Type2,
    ident: &Type2,
  ) -> result::Result<Vec<Numeric>, Error> {
    let target_idents = self.numerical_ident_from_type(target)?;

    match ident {
      Type2::IntValue(i) => {
        if target_idents.into_iter().any(|ti| ti == "int" || ti == "number") {
          return Ok(vec![Numeric::INT(*i)])
        }

        Err(Error::Syntax("Target data type must be an 'int' or 'number' in order to validate against an integer value".into()))
      }
      Type2::UintValue(ui) => {
        if target_idents.into_iter().any(|ti| ti == "uint" || ti == "number") {
          return Ok(vec![Numeric::UINT(*ui)]);
        }

        Err(Error::Syntax("Target data type must be a 'uint' or 'number' in order to validate against an unsigned integer value".into()))
      }
      Type2::FloatValue(f) => {
        if target_idents.into_iter().any(|ti| match ti.as_ref() {
          "float" | "float16" | "float32" | "float64" | "float16-32" | "float32-64" | "number" => true,
          _ => false,
        }) {
          return Ok(vec![Numeric::FLOAT(*f)]);
        }

        Err(Error::Syntax("Target data type must be a 'float', 'float16', 'float32', 'float64', 'float16-32', 'float32-64' or 'number' in order to validate against an unsigned integer value".into()))
      }
      Type2::Typename((ident, _)) => {
        let mut numeric_values = Vec::new();

        for r in self.rules.iter() {
          match r {
            Rule::Type(tr) if tr.name == *ident => {
              for tc in tr.value.0.iter() {
                numeric_values.append(&mut self.numeric_values_from_type(target, &tc.type2)?);
              }
            }
            _ => continue,
          }
        }

        Ok(numeric_values)
      }
      _ => Err(Error::Syntax(
        "Value can only be referenced via another type name identifier whose target type is the type of the value".into(),
      )),
    }
  }

  fn numerical_ident_from_type(&self, t2: &Type2) -> result::Result<Vec<String>, Error> {
    let mut numeric_type_idents = Vec::new();

    match t2 {
      Type2::Typename((Identifier((ident, _)), _)) if is_numeric_data_type(ident) => {
        numeric_type_idents.push(ident.clone());
        Ok(numeric_type_idents)
      }
      Type2::Typename((ident, _)) => {
        for r in self.rules.iter() {
          match r {
            Rule::Type(tr) if tr.name == *ident => {
              for tc in tr.value.0.iter() {
                numeric_type_idents.append(&mut self.numerical_ident_from_type(&tc.type2)?);
              }
            }
            _ => continue,
          }
        }

        Ok(numeric_type_idents)
      }
      _ => Err(Error::Syntax(
        "Numeric identifier can only be referenced via another type name identifier".into(),
      )),
    }
  }
}

fn is_numeric_data_type(t: &str) -> bool {
  match t {
    "uint" | "nint" | "int" | "number" | "float" | "float16" | "float32" | "float64"
    | "float16-32" | "float32-64" => true,
    _ => false,
  }
}