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
use super::token::{ByteSliceValue, ByteVecValue, RangeValue, SocketPlug, Value};
use std::fmt;

#[cfg(feature = "std")]
use std::borrow::Cow;

#[cfg(not(feature = "std"))]
use alloc::{
  borrow::Cow,
  boxed::Box,
  string::{String, ToString},
  vec::Vec,
};

/// Describes the literal formatting of an AST node
pub trait Node {
  /// Returns an optional formatted token literal string of the AST node
  fn token_literal(&self) -> Option<String>;
}

/// CDDL AST
///
/// ```abnf
/// cddl = S 1*(rule S)
/// ```
#[derive(Default, Debug)]
pub struct CDDL<'a> {
  /// Zero or more production rules
  pub rules: Vec<Rule<'a>>,
}

impl<'a> fmt::Display for CDDL<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut cddl_output = String::new();

    for r in self.rules.iter() {
      cddl_output.push_str(&format!("{}\n\n", r));
    }

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

impl<'a> Node for CDDL<'a> {
  fn token_literal(&self) -> Option<String> {
    if !self.rules.is_empty() {
      return self.rules[0].token_literal();
    }

    None
  }
}

/// Identifier for a type name, group name or bareword, with an optional socket
///
/// ```abnf
/// id = EALPHA *(*("-" / ".") (EALPHA / DIGIT))
/// ALPHA = %x41-5A / %x61-7A
/// EALPHA = ALPHA / "@" / "_" / "$"
/// DIGIT = %x30-39
/// ```
#[derive(Debug, PartialEq)]
pub struct Identifier<'a>(pub (&'a str, Option<&'a SocketPlug>));

impl<'a> Node for Identifier<'a> {
  fn token_literal(&self) -> Option<String> {
    Some(format!("{:?}", self))
  }
}

impl<'a> fmt::Display for Identifier<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    if let Some(sp) = (self.0).1 {
      return write!(f, "{}{}", sp, (self.0).0);
    }

    write!(f, "{}", (self.0).0)
  }
}

impl<'a> From<(&'a str, Option<&'a SocketPlug>)> for Identifier<'a> {
  fn from(ident: (&'a str, Option<&'a SocketPlug>)) -> Self {
    Identifier(ident)
  }
}

impl<'a> From<&'static str> for Identifier<'a> {
  fn from(ident: &'static str) -> Self {
    // TODO: support socketplug
    Identifier((ident, None))
  }
}

/// Type or group expression
///
/// ```abnf
/// rule = typename [genericparm] S assignt S type
///     / groupname [genericparm] S assigng S grpent
/// ```
#[derive(Debug)]
pub enum Rule<'a> {
  /// Type expression
  Type(TypeRule<'a>),
  /// Group expression
  Group(Box<GroupRule<'a>>),
}

impl<'a> fmt::Display for Rule<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      Rule::Type(tr) => write!(f, "{}", tr),
      Rule::Group(gr) => write!(f, "{}", gr),
    }
  }
}

impl<'a> Node for Rule<'a> {
  fn token_literal(&self) -> Option<String> {
    match self {
      Rule::Type(tr) => tr.token_literal(),
      Rule::Group(gr) => gr.token_literal(),
    }
  }
}

/// Type expression
///
/// ```abnf
/// typename [genericparm] S assignt S type
/// ```
#[derive(Debug)]
pub struct TypeRule<'a> {
  /// Type name identifier
  pub name: Identifier<'a>,
  /// Optional generic parameters
  pub generic_param: Option<GenericParm<'a>>,
  /// Extends an existing type choice
  pub is_type_choice_alternate: bool,
  /// Type value
  pub value: Type<'a>,
}

impl<'a> fmt::Display for TypeRule<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut tr_output = self.name.to_string();

    if let Some(gp) = &self.generic_param {
      tr_output.push_str(&gp.to_string());
    }

    if self.is_type_choice_alternate {
      tr_output.push_str(" /= ");
    } else {
      tr_output.push_str(" = ");
    }

    tr_output.push_str(&self.value.to_string());

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

impl<'a> Node for TypeRule<'a> {
  fn token_literal(&self) -> Option<String> {
    Some(format!("{:?}", self))
  }
}

/// Group expression
///
/// ```abnf
/// groupname [genericparm] S assigng S grpent
/// ```
#[derive(Debug)]
pub struct GroupRule<'a> {
  /// Group name identifier
  pub name: Identifier<'a>,
  /// Optional generic parameters
  pub generic_param: Option<GenericParm<'a>>,
  /// Extends an existing group choice
  pub is_group_choice_alternate: bool,
  /// Group entry
  pub entry: GroupEntry<'a>,
}

impl<'a> fmt::Display for GroupRule<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut gr_output = self.name.to_string();

    if let Some(gp) = &self.generic_param {
      gr_output.push_str(&gp.to_string());
    }

    if self.is_group_choice_alternate {
      gr_output.push_str(" //= ");
    } else {
      gr_output.push_str(" = ");
    }

    gr_output.push_str(&self.entry.to_string());

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

impl<'a> Node for GroupRule<'a> {
  fn token_literal(&self) -> Option<String> {
    Some("".into())
  }
}

/// Generic parameters
///
/// ```abnf
/// genericparm =  "<" S id S *("," S id S ) ">"
/// ```
#[derive(Default, Debug)]
pub struct GenericParm<'a>(pub Vec<Identifier<'a>>);

impl<'a> fmt::Display for GenericParm<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut gp = String::from("<");
    for (idx, parm) in self.0.iter().enumerate() {
      if idx != 0 {
        gp.push_str(", ");
      }

      gp.push_str(&parm.to_string());
    }

    gp.push('>');

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

/// Generic arguments
///
/// ```abnf
/// genericarg = "<" S type1 S *("," S type1 S )  ">"
/// ```
#[derive(Debug)]
pub struct GenericArg<'a>(pub Vec<Type1<'a>>);

impl<'a> fmt::Display for GenericArg<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut ga = String::from("<");
    for (idx, arg) in self.0.iter().enumerate() {
      if idx != 0 {
        ga.push_str(", ");
      }

      ga.push_str(&arg.to_string());
    }

    ga.push('>');

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

/// Type choices
///
/// ```abnf
/// type = type1 *(S "/" S  type1)
/// ```
#[derive(Debug)]
pub struct Type<'a>(pub Vec<Type1<'a>>);

impl<'a> fmt::Display for Type<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut types = String::new();

    for (idx, t) in self.0.iter().enumerate() {
      if idx == 0 {
        types.push_str(&t.to_string());
        continue;
      }

      types.push_str(&format!(" / {}", t.to_string()));
    }

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

/// Type with optional range or control operator
///
/// ```abnf
/// type1 = type2 [S (rangeop / ctlop) S type2]
/// ```
#[derive(Debug)]
pub struct Type1<'a> {
  /// Type
  pub type2: Type2<'a>,
  /// Range or control operator over a second type
  pub operator: Option<(RangeCtlOp, Type2<'a>)>,
}

impl<'a> fmt::Display for Type1<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut t1 = String::new();

    t1.push_str(&self.type2.to_string());

    if let Some((rco, t2)) = &self.operator {
      t1.push_str(&rco.to_string());
      t1.push_str(&t2.to_string());
    }

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

/// Range or control operator
///
/// ```abnf
/// rangeop = "..." / ".."
/// ctlop = "." id
/// ```
#[derive(Debug, PartialEq)]
pub enum RangeCtlOp {
  /// Range operator where value is `true` if inclusive
  RangeOp(bool),
  /// Control operator where value is the identifier of the operator
  CtlOp(&'static str),
}

impl<'a> fmt::Display for RangeCtlOp {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      RangeCtlOp::RangeOp(false) => write!(f, "..."),
      RangeCtlOp::RangeOp(true) => write!(f, ".."),
      RangeCtlOp::CtlOp(ctrl) => write!(f, ".{}", ctrl),
    }
  }
}

/// Type
///
/// ```abnf
/// type2 = value
///     / typename [genericarg]
///     / "(" S type S ")"
///     / "{" S group S "}"
///     / "[" S group S "]"
///     / "~" S typename [genericarg]
///     / "&" S "(" S group S ")"
///     / "&" S groupname [genericarg]
///     / "#" "6" ["." uint] "(" S type S ")"
///     / "#" DIGIT ["." uint]                ; major/ai
///     / "#"                                 ; any
/// ```
#[derive(Debug)]
pub enum Type2<'a> {
  /// Integer value
  IntValue(isize),
  /// Unsigned integer value
  UintValue(usize),
  /// Float value
  FloatValue(f64),
  /// Text string value (enclosed by '"')
  TextValue(&'a str),
  /// Base 16 encoded prefixed byte string
  B16ByteString(Cow<'a, [u8]>),
  /// Base 64 encoded (URL safe) prefixed byte string
  B64ByteString(Cow<'a, [u8]>),
  /// Type name identifier with optional generic arguments
  Typename((Identifier<'a>, Option<GenericArg<'a>>)),
  /// Parenthesized type expression (for operator precedence)
  ParenthesizedType(Type<'a>),
  /// Map expression
  Map(Group<'a>),
  /// Array expression
  Array(Group<'a>),
  /// Unwrapped group
  Unwrap((Identifier<'a>, Option<GenericArg<'a>>)),
  /// Enumeration expression over an inline group
  ChoiceFromInlineGroup(Group<'a>),
  /// Enumeration expression over previously defined group
  ChoiceFromGroup((Identifier<'a>, Option<GenericArg<'a>>)),
  /// Tagged data item
  TaggedData((Option<usize>, &'a str)),
  /// Data item of a major type with optional data constraint
  TaggedDataMajorType((u8, Option<usize>)),
  /// Any data item
  Any,
}

impl<'a> fmt::Display for Type2<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      Type2::IntValue(value) => write!(f, "{}", value),
      Type2::UintValue(value) => write!(f, "{}", value),
      Type2::FloatValue(value) => write!(f, "{}", value),
      Type2::TextValue(value) => write!(f, "\"{}\"", value),
      Type2::B16ByteString(value) => {
        write!(f, "{}", std::str::from_utf8(value).map_err(|_| fmt::Error)?)
      }
      Type2::B64ByteString(value) => {
        write!(f, "{}", std::str::from_utf8(value).map_err(|_| fmt::Error)?)
      }
      Type2::Typename((tn, ga)) => {
        if let Some(args) = ga {
          return write!(f, "{}{}", tn, args);
        }

        write!(f, "{}", tn)
      }
      Type2::ParenthesizedType(_t) => unimplemented!(),
      Type2::Map(g) => write!(f, "{{{}}}", g),
      Type2::Array(g) => write!(f, "[{}]", g),
      Type2::Unwrap((ident, ga)) => {
        if let Some(args) = ga {
          return write!(f, "{}{}", ident, args);
        }

        write!(f, "{}", ident)
      }
      Type2::ChoiceFromInlineGroup(g) => write!(f, "&({})", g),
      Type2::ChoiceFromGroup((ident, generic_arg)) => {
        if let Some(ga) = generic_arg {
          return write!(f, "&{}{}", ident, ga);
        }

        write!(f, "&{}", ident)
      }
      Type2::TaggedData((tag_uint, tagged_value)) => {
        if let Some(t) = tag_uint {
          return write!(f, "#6.{}({})", t, tagged_value);
        }

        write!(f, "#6({})", tagged_value)
      }
      Type2::TaggedDataMajorType((major_type, tag_uint)) => {
        if let Some(t) = tag_uint {
          return write!(f, "{}.{}", major_type, t);
        }

        write!(f, "{}", major_type)
      }
      Type2::Any => write!(f, "#"),
    }
  }
}

impl<'a> From<Value<'a>> for Type2<'a> {
  fn from(value: Value<'a>) -> Self {
    match value {
      Value::TEXT(t) => Type2::TextValue(t),
      Value::INT(i) => Type2::IntValue(i),
      Value::UINT(ui) => Type2::UintValue(ui),
      Value::FLOAT(f) => Type2::FloatValue(f),
    }
  }
}

impl<'a> From<RangeValue<'a>> for Type2<'a> {
  fn from(rv: RangeValue<'a>) -> Self {
    match rv {
      RangeValue::IDENT(ident) => Type2::Typename((ident.into(), None)),
      RangeValue::INT(i) => Type2::IntValue(i),
      RangeValue::UINT(ui) => Type2::UintValue(ui),
      RangeValue::FLOAT(f) => Type2::FloatValue(f),
    }
  }
}

impl<'a> From<&ByteSliceValue<'a>> for Type2<'a> {
  fn from(value: &ByteSliceValue<'a>) -> Self {
    match value {
      ByteSliceValue::B16(b16) => Type2::B16ByteString(Cow::from(*b16)),
      ByteSliceValue::B64(b64) => Type2::B64ByteString(Cow::from(*b64)),
    }
  }
}

impl<'a> From<ByteVecValue> for Type2<'a> {
  fn from(value: ByteVecValue) -> Self {
    match value {
      ByteVecValue::B16(b16) => Type2::B16ByteString(Cow::from(b16)),
      ByteVecValue::B64(b64) => Type2::B64ByteString(Cow::from(b64)),
    }
  }
}

/// Group choices
///
/// ```abnf
/// group = grpchoice * (S "//" S grpchoice)
/// ```
#[derive(Debug)]
pub struct Group<'a>(pub Vec<GroupChoice<'a>>);

impl<'a> fmt::Display for Group<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut group_choices = String::new();

    for (idx, gc) in self.0.iter().enumerate() {
      if idx == 0 {
        group_choices.push_str(&gc.to_string());
        continue;
      }

      group_choices.push_str(&format!(" / {}", gc));
    }

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

/// Group entries
///
/// ```abnf
/// grpchoice = *(grpent optcom)
/// ```
#[derive(Debug)]
pub struct GroupChoice<'a>(pub Vec<GroupEntry<'a>>);

impl<'a> fmt::Display for GroupChoice<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    if self.0.len() == 1 {
      return write!(f, "{}", self.0[0]);
    }

    let mut group_entries = String::new();

    for ge in self.0.iter() {
      group_entries.push_str(&format!("\t{},\n", ge));
    }

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

/// Group entry
///
/// ```abnf
/// grpent = [occur S] [memberkey S] type
///       / [occur S] groupname [genericarg]  ; preempted by above
///       / [occur S] "(" S group S ")"
/// ```
#[derive(Debug)]
pub enum GroupEntry<'a> {
  /// Value group entry type
  ValueMemberKey(Box<ValueMemberKeyEntry<'a>>),
  /// Group entry from a named group or type
  TypeGroupname(TypeGroupnameEntry<'a>),
  /// Parenthesized group with optional occurrence indicator
  InlineGroup((Option<Occur>, Group<'a>)),
}

impl<'a> fmt::Display for GroupEntry<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      GroupEntry::ValueMemberKey(vmke) => write!(f, "{}", vmke),
      GroupEntry::TypeGroupname(gne) => write!(f, "{}", gne),
      GroupEntry::InlineGroup((occur, group)) => {
        if let Some(o) = occur {
          return write!(f, "{} ({})", o, group);
        }

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

/// Value group entry type with optional occurrence indicator and optional
/// member key
///
/// ```abnf
/// [occur S] [memberkey S] type
/// ```
#[derive(Debug)]
pub struct ValueMemberKeyEntry<'a> {
  /// Optional occurrence indicator
  pub occur: Option<Occur>,
  /// Optional member key
  pub member_key: Option<MemberKey<'a>>,
  /// Entry type
  pub entry_type: Type<'a>,
}

impl<'a> fmt::Display for ValueMemberKeyEntry<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    if let Some(o) = &self.occur {
      if let Some(mk) = &self.member_key {
        return write!(f, "{} {} {}", o, mk, self.entry_type);
      }

      return write!(f, "{} {}", o, self.entry_type);
    }

    if let Some(mk) = &self.member_key {
      return write!(f, "{} {}", mk, self.entry_type);
    }

    write!(f, "{}", self.entry_type)
  }
}

/// Group entry from a named type or group
#[derive(Debug)]
pub struct TypeGroupnameEntry<'a> {
  /// Optional occurrence indicator
  pub occur: Option<Occur>,
  /// Type or group name identifier
  pub name: Identifier<'a>,
  /// Optional generic arguments
  pub generic_arg: Option<GenericArg<'a>>,
}

impl<'a> fmt::Display for TypeGroupnameEntry<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    if let Some(o) = &self.occur {
      if let Some(ga) = &self.generic_arg {
        return write!(f, "{} {} {}", o, self.name, ga);
      }

      return write!(f, "{} {}", o, self.name);
    }

    if let Some(ga) = &self.generic_arg {
      return write!(f, "{} {}", self.name, ga);
    }

    write!(f, "{}", self.name)
  }
}

/// Member key
/// ```abnf
/// memberkey = type1 S ["^" S] "=>"
///           / bareword S ":"
///           / value S ":"
/// ```
#[derive(Debug)]
pub enum MemberKey<'a> {
  /// Type expression. If second value in tuple is `true`, a cut is present
  Type1(Box<(Type1<'a>, bool)>),
  /// Bareword string type
  Bareword(Identifier<'a>),
  /// Value type
  Value(Value<'a>),
}

impl<'a> fmt::Display for MemberKey<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      MemberKey::Type1(t1) => {
        if t1.1 {
          return write!(f, "{} ^ =>", t1.0);
        }

        write!(f, "{} =>", t1.0)
      }
      MemberKey::Bareword(ident) => write!(f, "{}:", ident),
      MemberKey::Value(value) => write!(f, "{}:", value),
    }
  }
}

/// Occurrence indicator
/// ```abnf
/// occur = [uint] "*" [uint]
///       / "+"
///       / "?"
/// ```
#[derive(Debug)]
pub enum Occur {
  /// Occurrence indicator in the form n*m, where n is an optional lower limit
  /// and m is an optional upper limit
  Exact((Option<usize>, Option<usize>)),
  /// Occurrence indicator in the form *, allowing zero or more occurrences
  ZeroOrMore,
  /// Occurrence indicator in the form +, allowing one or more occurrences
  OneOrMore,
  /// Occurrence indicator in the form ?, allowing an optional occurrence
  Optional,
}

impl fmt::Display for Occur {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      Occur::ZeroOrMore => write!(f, "*"),
      Occur::Exact((l, u)) => {
        if let Some(li) = l {
          if let Some(ui) = u {
            return write!(f, "{}*{}", li, ui);
          }

          return write!(f, "{}*", li);
        }

        if let Some(ui) = u {
          return write!(f, "*{}", ui);
        }

        write!(f, "*")
      }
      Occur::OneOrMore => write!(f, "+"),
      Occur::Optional => write!(f, "?"),
    }
  }
}

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

  #[test]
  fn verify_groupentry_output() {
    assert_eq!(
      GroupEntry::TypeGroupname(TypeGroupnameEntry {
        occur: None,
        name: Identifier::from("entry1"),
        generic_arg: None,
      })
      .to_string(),
      "entry1".to_string()
    )
  }

  #[test]
  fn verify_group_output() {
    assert_eq!(
      Group(vec![GroupChoice(vec![
        GroupEntry::ValueMemberKey(Box::from(ValueMemberKeyEntry {
          occur: None,
          member_key: Some(MemberKey::Bareword("key1".into())),
          entry_type: Type(vec![Type1 {
            type2: Type2::TextValue("value1"),
            operator: None,
          }]),
        })),
        GroupEntry::ValueMemberKey(Box::from(ValueMemberKeyEntry {
          occur: None,
          member_key: Some(MemberKey::Bareword("key2".into())),
          entry_type: Type(vec![Type1 {
            type2: Type2::TextValue("value2"),
            operator: None,
          }]),
        })),
      ])])
      .to_string(),
      "\tkey1: \"value1\",\n\tkey2: \"value2\",\n".to_string()
    )
  }
}