From 9f8025e3153f365d65100922682631a7e1841427 Mon Sep 17 00:00:00 2001 From: anweiss <2326106+anweiss@users.noreply.github.com> Date: Thu, 7 Oct 2021 11:14:32 -0400 Subject: [PATCH] ast refactor --- src/ast.rs | 1208 ++++++++++++++++++++------------------ src/faker.rs | 86 +-- src/parser.rs | 298 +++++----- src/parser_tests.rs | 508 +++++++--------- src/util.rs | 76 +-- src/validator/cbor.rs | 108 ++-- src/validator/control.rs | 174 +++--- src/validator/json.rs | 92 +-- src/visitor.rs | 50 +- tests/parser.rs | 207 +++---- 10 files changed, 1391 insertions(+), 1416 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index a90c3b1f..bbd9fd74 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -140,7 +140,7 @@ impl<'a> fmt::Display for CDDL<'a> { /// DIGIT = %x30-39 /// ``` #[cfg_attr(target_arch = "wasm32", derive(Serialize))] -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub struct Identifier<'a> { /// Identifier pub ident: &'a str, @@ -180,8 +180,7 @@ impl<'a> From<&'static str> for Identifier<'a> { return Identifier { ident, socket: Some(SocketPlug::GROUP), - #[cfg(feature = "ast-span")] - span: Span::default(), + ..Default::default() }; } } @@ -189,8 +188,7 @@ impl<'a> From<&'static str> for Identifier<'a> { return Identifier { ident, socket: Some(SocketPlug::TYPE), - #[cfg(feature = "ast-span")] - span: Span::default(), + ..Default::default() }; } } @@ -198,8 +196,7 @@ impl<'a> From<&'static str> for Identifier<'a> { Identifier { ident, socket: None, - #[cfg(feature = "ast-span")] - span: Span::default(), + ..Default::default() } } } @@ -385,7 +382,7 @@ impl<'a> Rule<'a> { /// typename [genericparm] S assignt S type /// ``` #[cfg_attr(target_arch = "wasm32", derive(Serialize))] -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Default)] pub struct TypeRule<'a> { /// Type name identifier #[cfg_attr(target_arch = "wasm32", serde(borrow))] @@ -501,7 +498,7 @@ impl<'a> fmt::Display for GroupRule<'a> { /// genericparm = "<" S id S *("," S id S ) ">" /// ``` #[cfg_attr(target_arch = "wasm32", derive(Serialize))] -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Default)] pub struct GenericParams<'a> { /// List of generic parameters pub params: Vec>, @@ -510,19 +507,9 @@ pub struct GenericParams<'a> { pub span: Span, } -impl<'a> Default for GenericParams<'a> { - fn default() -> Self { - GenericParams { - params: Vec::new(), - #[cfg(feature = "ast-span")] - span: Span::default(), - } - } -} - /// Generic parameter #[cfg_attr(target_arch = "wasm32", derive(Serialize))] -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Default)] pub struct GenericParam<'a> { /// Generic parameter pub param: Identifier<'a>, @@ -570,7 +557,7 @@ impl<'a> fmt::Display for GenericParams<'a> { /// genericarg = "<" S type1 S *("," S type1 S ) ">" /// ``` #[cfg_attr(target_arch = "wasm32", derive(Serialize))] -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct GenericArgs<'a> { /// Generic arguments pub args: Vec>, @@ -579,20 +566,9 @@ pub struct GenericArgs<'a> { pub span: Span, } -impl<'a> GenericArgs<'a> { - /// Default `GenericArg` - pub fn default() -> Self { - GenericArgs { - args: Vec::new(), - #[cfg(feature = "ast-span")] - span: Span::default(), - } - } -} - /// Generic argument #[cfg_attr(target_arch = "wasm32", derive(Serialize))] -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct GenericArg<'a> { /// Generic argument pub arg: Box>, @@ -635,7 +611,7 @@ impl<'a> fmt::Display for GenericArgs<'a> { } /// A generic rule that has undergone monomorphization -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub struct GenericRule<'a> { /// Name pub name: &'a str, @@ -651,7 +627,7 @@ pub struct GenericRule<'a> { /// type = type1 *(S "/" S type1) /// ``` #[cfg_attr(target_arch = "wasm32", derive(Serialize))] -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct Type<'a> { /// Type choices pub type_choices: Vec>, @@ -688,12 +664,12 @@ impl<'a> From<&Identifier<'a>> for Type<'a> { Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: ident.clone(), generic_args: None, #[cfg(feature = "ast-span")] span: ident.span, - }, + }), operator: None, #[cfg(feature = "ast-span")] span: ident.span, @@ -713,7 +689,7 @@ impl<'a> From<&Identifier<'a>> for Type<'a> { /// Type choice #[cfg_attr(target_arch = "wasm32", derive(Serialize))] -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct TypeChoice<'a> { /// Type choice @@ -774,11 +750,11 @@ impl<'a> Type<'a> { if self.type_choices.len() == 1 { if let Some(tc) = self.type_choices.first() { if tc.type1.operator.is_none() { - if let Type2::Typename { + if let Type2::Typename(Typename { ident, generic_args, span, - } = &tc.type1.type2 + }) = &tc.type1.type2 { return Some((ident.clone(), generic_args.clone(), *span)); } @@ -797,10 +773,10 @@ impl<'a> Type<'a> { if self.type_choices.len() == 1 { if let Some(tc) = self.type_choices.first() { if tc.type1.operator.is_none() { - if let Type2::Typename { + if let Type2::Typename(Typename { ident, generic_args, - } = &tc.type1.type2 + }) = &tc.type1.type2 { return Some((ident.clone(), generic_args.clone())); } @@ -818,7 +794,7 @@ impl<'a> Type<'a> { /// type1 = type2 [S (rangeop / ctlop) S type2] /// ``` #[cfg_attr(target_arch = "wasm32", derive(Serialize))] -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct Type1<'a> { /// Type pub type2: Type2<'a>, @@ -839,41 +815,41 @@ impl<'a> From> for Type1<'a> { #[cfg(feature = "ast-span")] let span = Span::default(); let type2 = match value { - Value::TEXT(value) => Type2::TextValue { + Value::TEXT(value) => Type2::TextValue(TextValue { value, #[cfg(feature = "ast-span")] span, - }, - Value::INT(value) => Type2::IntValue { + }), + Value::INT(value) => Type2::IntValue(IntValue { value, #[cfg(feature = "ast-span")] span, - }, - Value::FLOAT(value) => Type2::FloatValue { + }), + Value::FLOAT(value) => Type2::FloatValue(FloatValue { value, #[cfg(feature = "ast-span")] span, - }, - Value::UINT(value) => Type2::UintValue { + }), + Value::UINT(value) => Type2::UintValue(UintValue { value, #[cfg(feature = "ast-span")] span, - }, - Value::BYTE(ByteValue::B16(value)) => Type2::B16ByteString { + }), + Value::BYTE(ByteValue::B16(value)) => Type2::B16ByteString(B16ByteString { value, #[cfg(feature = "ast-span")] span, - }, - Value::BYTE(ByteValue::B64(value)) => Type2::B64ByteString { + }), + Value::BYTE(ByteValue::B64(value)) => Type2::B64ByteString(B64ByteString { value, #[cfg(feature = "ast-span")] span, - }, - Value::BYTE(ByteValue::UTF8(value)) => Type2::UTF8ByteString { + }), + Value::BYTE(ByteValue::UTF8(value)) => Type2::UTF8ByteString(Utf8ByteString { value, #[cfg(feature = "ast-span")] span, - }, + }), }; Type1 { @@ -1017,218 +993,53 @@ impl<'a> fmt::Display for RangeCtlOp<'a> { #[derive(Debug, Clone, PartialEq)] pub enum Type2<'a> { /// Integer value - IntValue { - /// Value - value: isize, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - }, + IntValue(IntValue), /// Unsigned integer value - UintValue { - /// Value - value: usize, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - }, + UintValue(UintValue), /// Float value - FloatValue { - /// Value - value: f64, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - }, + FloatValue(FloatValue), /// Text string value (enclosed by '"') - TextValue { - /// Value - value: Cow<'a, str>, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - }, + TextValue(TextValue<'a>), /// UTF-8 encoded byte string (enclosed by '') - UTF8ByteString { - /// Value - value: Cow<'a, [u8]>, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - }, + UTF8ByteString(Utf8ByteString<'a>), /// Base 16 encoded prefixed byte string - B16ByteString { - /// Value - value: Cow<'a, [u8]>, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - }, + B16ByteString(B16ByteString<'a>), /// Base 64 encoded (URL safe) prefixed byte string - B64ByteString { - /// Value - value: Cow<'a, [u8]>, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - }, + B64ByteString(B64ByteString<'a>), /// Type name identifier with optional generic arguments - Typename { - /// Identifier - ident: Identifier<'a>, - /// Generic arguments - generic_args: Option>, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - }, + Typename(Typename<'a>), /// Parenthesized type expression (for operator precedence) - ParenthesizedType { - /// Type - pt: Type<'a>, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments_before_type: Option>, - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments_after_type: Option>, - }, + ParenthesizedType(ParenthesizedType<'a>), /// Map expression - Map { - /// Group - group: Group<'a>, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments_before_group: Option>, - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments_after_group: Option>, - }, + Map(Map<'a>), /// Array expression - Array { - /// Span - group: Group<'a>, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments_before_group: Option>, - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments_after_group: Option>, - }, + Array(Array<'a>), /// Unwrapped group - Unwrap { - /// Identifier - ident: Identifier<'a>, - /// Generic arguments - generic_args: Option>, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments: Option>, - }, + Unwrap(Unwrap<'a>), /// Enumeration expression over an inline group - ChoiceFromInlineGroup { - /// Group - group: Group<'a>, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments: Option>, - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments_before_group: Option>, - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments_after_group: Option>, - }, + ChoiceFromInlineGroup(ChoiceFromInlineGroup<'a>), /// Enumeration expression over previously defined group - ChoiceFromGroup { - /// Identifier - ident: Identifier<'a>, - /// Generic arguments - generic_args: Option>, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments: Option>, - }, + ChoiceFromGroup(ChoiceFromGroup<'a>), /// Tagged data item where the first element is an optional tag and the second /// is the type of the tagged value - TaggedData { - /// Tag - tag: Option, - /// Type - t: Type<'a>, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments_before_type: Option>, - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments_after_type: Option>, - }, + TaggedData(TaggedData<'a>), /// Data item of a major type with optional data constraint - DataMajorType { - /// Major type - mt: u8, - /// Constraint - constraint: Option, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - }, + DataMajorType(DataMajorType), /// Any data item #[cfg(feature = "ast-span")] @@ -1239,44 +1050,311 @@ pub enum Type2<'a> { Any, } +#[cfg(feature = "ast-span")] +impl<'a> Default for Type2<'a> { + fn default() -> Self { + Type2::Any(Span::default()) + } +} + +#[cfg(not(feature = "ast-span"))] +impl<'a> Default for Type2<'a> { + fn default() -> Self { + Type2::Any + } +} + +/// Int value +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct IntValue { + /// Value + pub value: isize, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, +} + +/// Uint value +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct UintValue { + /// Value + pub value: usize, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, +} + +/// Float value +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct FloatValue { + /// Value + pub value: f64, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, +} + +/// Text value +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct TextValue<'a> { + /// Value + pub value: Cow<'a, str>, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, +} + +/// Utf8 byte string +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct Utf8ByteString<'a> { + /// Value + pub value: Cow<'a, [u8]>, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, +} + +/// Base16 byte string +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct B16ByteString<'a> { + /// Value + pub value: Cow<'a, [u8]>, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, +} + +/// Base64 byte string +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct B64ByteString<'a> { + /// Value + pub value: Cow<'a, [u8]>, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, +} + +/// Type name +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct Typename<'a> { + /// Identifier + pub ident: Identifier<'a>, + /// Generic arguments + pub generic_args: Option>, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, +} + +#[doc(hidden)] +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Clone, PartialEq)] +pub enum ParenthesizedTypeOrGroup<'a> { + Group(Group<'a>), + Type(ParenthesizedType<'a>), +} + +/// Parenthesized type +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct ParenthesizedType<'a> { + /// Type + pub pt: Type<'a>, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, + + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_before_type: Option>, + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_after_type: Option>, +} + +/// Map +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct Map<'a> { + /// Group + pub group: Group<'a>, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, + + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_before_group: Option>, + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_after_group: Option>, +} + +/// Array +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct Array<'a> { + /// Group + pub group: Group<'a>, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, + + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_before_group: Option>, + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_after_group: Option>, +} + +/// Unwrap +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct Unwrap<'a> { + /// Identifier + pub ident: Identifier<'a>, + /// Generic arguments + pub generic_args: Option>, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, + + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments: Option>, +} + +/// Choice from inline group +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct ChoiceFromInlineGroup<'a> { + /// Group + pub group: Group<'a>, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, + + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments: Option>, + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_before_group: Option>, + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_after_group: Option>, +} + +/// Choice from group +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct ChoiceFromGroup<'a> { + /// Identifier + pub ident: Identifier<'a>, + /// Generic arguments + pub generic_args: Option>, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, + + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments: Option>, +} + +/// Tagged data +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct TaggedData<'a> { + /// Tag + pub tag: Option, + /// Type + pub t: Type<'a>, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, + + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_before_type: Option>, + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_after_type: Option>, +} + +/// Major data type +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct DataMajorType { + /// Major type + pub mt: u8, + /// Constraint + pub constraint: Option, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, +} + #[allow(clippy::cognitive_complexity)] 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::UTF8ByteString { value, .. } => write!( + Type2::IntValue(IntValue { value, .. }) => write!(f, "{}", value), + Type2::UintValue(UintValue { value, .. }) => write!(f, "{}", value), + Type2::FloatValue(FloatValue { value, .. }) => write!(f, "{}", value), + Type2::TextValue(TextValue { value, .. }) => write!(f, "\"{}\"", value), + Type2::UTF8ByteString(Utf8ByteString { value, .. }) => write!( f, "'{}'", std::str::from_utf8(value).map_err(|_| fmt::Error)? ), - Type2::B16ByteString { value, .. } => { + Type2::B16ByteString(B16ByteString { value, .. }) => { write!(f, "{}", std::str::from_utf8(value).map_err(|_| fmt::Error)?) } - Type2::B64ByteString { value, .. } => { + Type2::B64ByteString(B64ByteString { value, .. }) => { write!(f, "{}", std::str::from_utf8(value).map_err(|_| fmt::Error)?) } - Type2::Typename { + Type2::Typename(Typename { ident, generic_args, .. - } => { + }) => { if let Some(args) = generic_args { return write!(f, "{}{}", ident, args); } write!(f, "{}", ident) } - Type2::ParenthesizedType { + Type2::ParenthesizedType(ParenthesizedType { #[cfg(feature = "ast-comments")] comments_before_type, pt, #[cfg(feature = "ast-comments")] comments_after_type, .. - } => { + }) => { let mut pt_str = String::from("("); #[cfg(feature = "ast-comments")] @@ -1305,14 +1383,14 @@ impl<'a> fmt::Display for Type2<'a> { write!(f, "{}", pt_str) } - Type2::Map { + Type2::Map(Map { #[cfg(feature = "ast-comments")] comments_before_group, group, #[cfg(feature = "ast-comments")] comments_after_group, .. - } => { + }) => { let mut t2_str = String::from("{"); #[cfg(feature = "ast-comments")] @@ -1360,14 +1438,14 @@ impl<'a> fmt::Display for Type2<'a> { write!(f, "{}", t2_str) } - Type2::Array { + Type2::Array(Array { #[cfg(feature = "ast-comments")] comments_before_group, group, #[cfg(feature = "ast-comments")] comments_after_group, .. - } => { + }) => { let mut t2_str = String::from("["); #[cfg(feature = "ast-comments")] @@ -1424,13 +1502,13 @@ impl<'a> fmt::Display for Type2<'a> { write!(f, "{}", t2_str) } - Type2::Unwrap { + Type2::Unwrap(Unwrap { #[cfg(feature = "ast-comments")] comments, ident, generic_args, .. - } => { + }) => { let mut t2_str = String::new(); #[cfg(feature = "ast-comments")] @@ -1446,7 +1524,7 @@ impl<'a> fmt::Display for Type2<'a> { write!(f, "{}", t2_str) } - Type2::ChoiceFromInlineGroup { + Type2::ChoiceFromInlineGroup(ChoiceFromInlineGroup { #[cfg(feature = "ast-comments")] comments, #[cfg(feature = "ast-comments")] @@ -1455,7 +1533,7 @@ impl<'a> fmt::Display for Type2<'a> { #[cfg(feature = "ast-comments")] comments_after_group, .. - } => { + }) => { let mut t2_str = String::from("&"); #[cfg(feature = "ast-comments")] @@ -1485,13 +1563,13 @@ impl<'a> fmt::Display for Type2<'a> { write!(f, "{}", t2_str) } - Type2::ChoiceFromGroup { + Type2::ChoiceFromGroup(ChoiceFromGroup { #[cfg(feature = "ast-comments")] comments, ident, generic_args, .. - } => { + }) => { let mut t2_str = String::from("&"); #[cfg(feature = "ast-comments")] @@ -1507,7 +1585,7 @@ impl<'a> fmt::Display for Type2<'a> { write!(f, "{}", t2_str) } - Type2::TaggedData { + Type2::TaggedData(TaggedData { tag, #[cfg(feature = "ast-comments")] comments_before_type, @@ -1515,7 +1593,7 @@ impl<'a> fmt::Display for Type2<'a> { #[cfg(feature = "ast-comments")] comments_after_type, .. - } => { + }) => { let mut t2_str = String::from("#6"); if let Some(tag_uint) = tag { @@ -1544,7 +1622,7 @@ impl<'a> fmt::Display for Type2<'a> { write!(f, "{}", t2_str) } - Type2::DataMajorType { mt, constraint, .. } => { + Type2::DataMajorType(DataMajorType { mt, constraint, .. }) => { if let Some(c) = constraint { return write!(f, "{}.{}", mt, c); } @@ -1565,7 +1643,7 @@ impl<'a> From> for Type2<'a> { let span = Span::default(); match rv { - RangeValue::IDENT(ident) => Type2::Typename { + RangeValue::IDENT(ident) => Type2::Typename(Typename { ident: Identifier { ident: ident.0, socket: ident.1, @@ -1575,268 +1653,271 @@ impl<'a> From> for Type2<'a> { generic_args: None, #[cfg(feature = "ast-span")] span, - }, - RangeValue::INT(value) => Type2::IntValue { + }), + RangeValue::INT(value) => Type2::IntValue(IntValue { value, #[cfg(feature = "ast-span")] span, - }, - RangeValue::UINT(value) => Type2::UintValue { + }), + RangeValue::UINT(value) => Type2::UintValue(UintValue { value, #[cfg(feature = "ast-span")] span, - }, - RangeValue::FLOAT(value) => Type2::FloatValue { + }), + RangeValue::FLOAT(value) => Type2::FloatValue(FloatValue { value, #[cfg(feature = "ast-span")] span, - }, + }), } } } impl<'a> From> for Type2<'a> { fn from(type1: Type1<'a>) -> Self { - Type2::ParenthesizedType { + Type2::ParenthesizedType(ParenthesizedType { pt: Type { type_choices: vec![TypeChoice { type1, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-comments")] - comments_before_type: None, + ..Default::default() }], #[cfg(feature = "ast-span")] span: Span::default(), }, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - } + ..Default::default() + }) } } impl<'a> From for Type2<'a> { fn from(value: usize) -> Self { - Type2::UintValue { + Type2::UintValue(UintValue { value, - #[cfg(feature = "ast-span")] - span: Span::default(), - } + ..Default::default() + }) } } impl<'a> From for Type2<'a> { fn from(value: isize) -> Self { - Type2::IntValue { + Type2::IntValue(IntValue { value, - #[cfg(feature = "ast-span")] - span: Span::default(), - } + ..Default::default() + }) } } impl<'a> From for Type2<'a> { fn from(value: f64) -> Self { - Type2::FloatValue { + Type2::FloatValue(FloatValue { value, - #[cfg(feature = "ast-span")] - span: Span::default(), - } + ..Default::default() + }) } } impl<'a> From for Type2<'a> { fn from(value: String) -> Self { - Type2::TextValue { + Type2::TextValue(TextValue { value: value.into(), - #[cfg(feature = "ast-span")] - span: Span::default(), - } + ..Default::default() + }) } } // Convenience method for testing impl<'a> From<&'a str> for Type2<'a> { fn from(value: &'a str) -> Self { - Type2::UTF8ByteString { + Type2::UTF8ByteString(Utf8ByteString { value: value.as_bytes().into(), - #[cfg(feature = "ast-span")] - span: Span::default(), - } + ..Default::default() + }) } } impl<'a> From> for Type2<'a> { fn from(value: ByteValue<'a>) -> Self { match value { - ByteValue::UTF8(value) => Type2::UTF8ByteString { + ByteValue::UTF8(value) => Type2::UTF8ByteString(Utf8ByteString { value, - #[cfg(feature = "ast-span")] - span: Span::default(), - }, - ByteValue::B16(value) => Type2::B16ByteString { + ..Default::default() + }), + ByteValue::B16(value) => Type2::B16ByteString(B16ByteString { value, - #[cfg(feature = "ast-span")] - span: Span::default(), - }, - ByteValue::B64(value) => Type2::B64ByteString { + ..Default::default() + }), + ByteValue::B64(value) => Type2::B64ByteString(B64ByteString { value, - #[cfg(feature = "ast-span")] - span: Span::default(), - }, + ..Default::default() + }), } } } +fn decfrac_type<'a>() -> Type<'a> { + Type { + type_choices: vec![TypeChoice { + type1: Type1 { + type2: Type2::Array(Array { + group: Group { + group_choices: vec![GroupChoice { + group_entries: vec![ + ( + GroupEntry::ValueMemberKey { + ge: Box::from(ValueMemberKeyEntry { + member_key: Some(MemberKey::Bareword(BarewordMemberKey { + ident: "e10".into(), + ..Default::default() + })), + entry_type: Type { + type_choices: vec![TypeChoice { + type1: Type1 { + type2: Type2::Typename(Typename { + ident: Token::INT.into(), + ..Default::default() + }), + ..Default::default() + }, + ..Default::default() + }], + ..Default::default() + }, + occur: None, + }), + #[cfg(feature = "ast-comments")] + leading_comments: None, + #[cfg(feature = "ast-span")] + span: Span::default(), + #[cfg(feature = "ast-comments")] + trailing_comments: None, + }, + OptionalComma::default(), + ), + ( + GroupEntry::ValueMemberKey { + ge: Box::from(ValueMemberKeyEntry { + member_key: Some(MemberKey::Bareword(BarewordMemberKey { + ident: "m".into(), + ..Default::default() + })), + entry_type: Type { + type_choices: vec![TypeChoice { + type1: Type1 { + type2: Type2::Typename(Typename { + ident: Token::INTEGER.into(), + ..Default::default() + }), + ..Default::default() + }, + ..Default::default() + }], + ..Default::default() + }, + ..Default::default() + }), + #[cfg(feature = "ast-comments")] + leading_comments: None, + #[cfg(feature = "ast-span")] + span: Span::default(), + #[cfg(feature = "ast-comments")] + trailing_comments: None, + }, + OptionalComma::default(), + ), + ], + ..Default::default() + }], + ..Default::default() + }, + ..Default::default() + }), + ..Default::default() + }, + ..Default::default() + }], + ..Default::default() + } +} + /// Retrieve `Type2` from token if it is a tag type in the standard prelude pub fn tag_from_token<'a>(token: &Token) -> Option> { match token { - Token::TDATE => Some(Type2::TaggedData { + Token::TDATE => Some(Type2::TaggedData(TaggedData { tag: Some(0), t: type_from_token(Token::TSTR), - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), - Token::TIME => Some(Type2::TaggedData { + ..Default::default() + })), + Token::TIME => Some(Type2::TaggedData(TaggedData { tag: Some(1), t: type_from_token(Token::NUMBER), - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), - Token::BIGUINT => Some(Type2::TaggedData { + ..Default::default() + })), + Token::BIGUINT => Some(Type2::TaggedData(TaggedData { tag: Some(2), t: type_from_token(Token::BSTR), - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), - Token::BIGNINT => Some(Type2::TaggedData { + ..Default::default() + })), + Token::BIGNINT => Some(Type2::TaggedData(TaggedData { tag: Some(3), t: type_from_token(Token::BSTR), - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), - Token::DECFRAC => unimplemented!(), + ..Default::default() + })), + Token::DECFRAC => Some(Type2::TaggedData(TaggedData { + tag: Some(4), + t: decfrac_type(), + ..Default::default() + })), Token::BIGFLOAT => unimplemented!(), - Token::EB64URL => Some(Type2::TaggedData { + Token::EB64URL => Some(Type2::TaggedData(TaggedData { tag: Some(21), t: type_from_token(Token::ANY), - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), - Token::EB64LEGACY => Some(Type2::TaggedData { + ..Default::default() + })), + Token::EB64LEGACY => Some(Type2::TaggedData(TaggedData { tag: Some(22), t: type_from_token(Token::ANY), - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), - Token::EB16 => Some(Type2::TaggedData { + ..Default::default() + })), + Token::EB16 => Some(Type2::TaggedData(TaggedData { tag: Some(23), t: type_from_token(Token::ANY), - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), - Token::ENCODEDCBOR => Some(Type2::TaggedData { + ..Default::default() + })), + Token::ENCODEDCBOR => Some(Type2::TaggedData(TaggedData { tag: Some(24), t: type_from_token(Token::BSTR), - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), - Token::URI => Some(Type2::TaggedData { + ..Default::default() + })), + Token::URI => Some(Type2::TaggedData(TaggedData { tag: Some(32), t: type_from_token(Token::TSTR), - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), - Token::B64URL => Some(Type2::TaggedData { + ..Default::default() + })), + Token::B64URL => Some(Type2::TaggedData(TaggedData { tag: Some(33), t: type_from_token(Token::TSTR), - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), - Token::B64LEGACY => Some(Type2::TaggedData { + ..Default::default() + })), + Token::B64LEGACY => Some(Type2::TaggedData(TaggedData { tag: Some(34), t: type_from_token(Token::TSTR), - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), - Token::REGEXP => Some(Type2::TaggedData { + ..Default::default() + })), + Token::REGEXP => Some(Type2::TaggedData(TaggedData { tag: Some(35), t: type_from_token(Token::TSTR), - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), - Token::MIMEMESSAGE => Some(Type2::TaggedData { + ..Default::default() + })), + Token::MIMEMESSAGE => Some(Type2::TaggedData(TaggedData { tag: Some(36), t: type_from_token(Token::TSTR), - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), - Token::CBORANY => Some(Type2::TaggedData { + ..Default::default() + })), + Token::CBORANY => Some(Type2::TaggedData(TaggedData { tag: Some(55799), t: type_from_token(Token::ANY), - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), + ..Default::default() + })), _ => None, } } @@ -1846,25 +1927,15 @@ pub fn type_from_token(token: Token) -> Type { Type { type_choices: vec![TypeChoice { type1: Type1 { - #[cfg(feature = "ast-comments")] - comments_after_type: None, - operator: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier::from(token), - generic_args: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }, + ..Default::default() + }), + ..Default::default() }, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - #[cfg(feature = "ast-comments")] - comments_before_type: None, + ..Default::default() }], - #[cfg(feature = "ast-span")] - span: Span::default(), + ..Default::default() } } @@ -1874,7 +1945,7 @@ pub fn type_from_token(token: Token) -> Type { /// group = grpchoice * (S "//" S grpchoice) /// ``` #[cfg_attr(target_arch = "wasm32", derive(Serialize))] -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct Group<'a> { /// Group choices #[cfg_attr(target_arch = "wasm32", serde(borrow))] @@ -1968,7 +2039,7 @@ impl<'a> fmt::Display for Group<'a> { /// /// If tuple is true, then entry is marked by a trailing comma #[cfg_attr(target_arch = "wasm32", derive(Serialize))] -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct GroupChoice<'a> { /// Group entries where the second item in the tuple indicates where or not a /// trailing comma is present @@ -2518,7 +2589,7 @@ impl<'a> fmt::Display for Occurrence<'a> { /// [occur S] [memberkey S] type /// ``` #[cfg_attr(target_arch = "wasm32", derive(Serialize))] -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct ValueMemberKeyEntry<'a> { /// Optional occurrence indicator pub occur: Option>, @@ -2588,92 +2659,110 @@ impl<'a> fmt::Display for TypeGroupnameEntry<'a> { #[derive(Debug, Clone, PartialEq)] pub enum MemberKey<'a> { /// Type expression - Type1 { - /// Type1 - #[cfg_attr(target_arch = "wasm32", serde(borrow))] - t1: Box>, - /// Is cut indicator present - is_cut: bool, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments_before_cut: Option>, - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments_after_cut: Option>, - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments_after_arrowmap: Option>, - }, + Type1(Type1MemberKey<'a>), /// Bareword string type - Bareword { - /// Identifier - #[cfg_attr(target_arch = "wasm32", serde(borrow))] - ident: Identifier<'a>, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments: Option>, - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments_after_colon: Option>, - }, + Bareword(BarewordMemberKey<'a>), /// Value type - Value { - /// Value - #[cfg_attr(target_arch = "wasm32", serde(borrow))] - value: Value<'a>, - /// Span - #[cfg(feature = "ast-span")] - span: Span, - - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments: Option>, - #[cfg(feature = "ast-comments")] - #[cfg_attr(target_arch = "wasm32", serde(skip))] - #[doc(hidden)] - comments_after_colon: Option>, - }, + Value(ValueMemberKey<'a>), // Used while parsing a parenthesized type that is not followed by a cut nor // an arrow map #[cfg_attr(target_arch = "wasm32", serde(skip))] #[doc(hidden)] - ParenthesizedType { - non_member_key: ParenthesizedType<'a>, - #[cfg(feature = "ast-comments")] - comments_before_type_or_group: Option>, - #[cfg(feature = "ast-comments")] - comments_after_type_or_group: Option>, - }, + Parenthesized(ParenthesizedMemberKey<'a>), +} + +/// Type1 member key +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Clone, PartialEq, Default)] +pub struct Type1MemberKey<'a> { + /// Type1 + #[cfg_attr(target_arch = "wasm32", serde(borrow))] + pub t1: Box>, + /// Is cut indicator present + pub is_cut: bool, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, + + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_before_cut: Option>, + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_after_cut: Option>, + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_after_arrowmap: Option>, +} + +/// Bareword member key +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Clone, PartialEq, Default)] +pub struct BarewordMemberKey<'a> { + /// Identifier + #[cfg_attr(target_arch = "wasm32", serde(borrow))] + pub ident: Identifier<'a>, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, + + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments: Option>, + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_after_colon: Option>, +} + +/// Value member key +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] +#[derive(Debug, Clone, PartialEq)] +pub struct ValueMemberKey<'a> { + /// Value + #[cfg_attr(target_arch = "wasm32", serde(borrow))] + pub value: Value<'a>, + /// Span + #[cfg(feature = "ast-span")] + pub span: Span, + + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments: Option>, + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_after_colon: Option>, } +/// Parenthesized member key +#[cfg_attr(target_arch = "wasm32", derive(Serialize))] #[derive(Debug, Clone, PartialEq)] #[doc(hidden)] -pub enum ParenthesizedType<'a> { - Group(Group<'a>), - Type(Type<'a>), +pub struct ParenthesizedMemberKey<'a> { + pub non_member_key: ParenthesizedTypeOrGroup<'a>, + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_before_type_or_group: Option>, + #[cfg(feature = "ast-comments")] + #[cfg_attr(target_arch = "wasm32", serde(skip))] + #[doc(hidden)] + pub comments_after_type_or_group: Option>, } impl<'a> fmt::Display for MemberKey<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - MemberKey::Type1 { + MemberKey::Type1(Type1MemberKey { t1, #[cfg(feature = "ast-comments")] comments_before_cut, @@ -2683,7 +2772,7 @@ impl<'a> fmt::Display for MemberKey<'a> { #[cfg(feature = "ast-comments")] comments_after_arrowmap, .. - } => { + }) => { let mut mk_str = format!("{} ", t1); #[cfg(feature = "ast-comments")] @@ -2715,14 +2804,14 @@ impl<'a> fmt::Display for MemberKey<'a> { write!(f, "{}", mk_str) } - MemberKey::Bareword { + MemberKey::Bareword(BarewordMemberKey { ident, #[cfg(feature = "ast-comments")] comments, #[cfg(feature = "ast-comments")] comments_after_colon, .. - } => { + }) => { let mut mk_str = format!("{}", ident); #[cfg(feature = "ast-comments")] @@ -2743,14 +2832,14 @@ impl<'a> fmt::Display for MemberKey<'a> { write!(f, "{}", mk_str) } - MemberKey::Value { + MemberKey::Value(ValueMemberKey { value, #[cfg(feature = "ast-comments")] comments, #[cfg(feature = "ast-comments")] comments_after_colon, .. - } => { + }) => { let mut mk_str = format!("{}", value); #[cfg(feature = "ast-comments")] @@ -2771,13 +2860,13 @@ impl<'a> fmt::Display for MemberKey<'a> { write!(f, "{}", mk_str) } - MemberKey::ParenthesizedType { - non_member_key: ParenthesizedType::Group(g), + MemberKey::Parenthesized(ParenthesizedMemberKey { + non_member_key: ParenthesizedTypeOrGroup::Group(g), #[cfg(feature = "ast-comments")] comments_before_type_or_group, #[cfg(feature = "ast-comments")] comments_after_type_or_group, - } => { + }) => { let mut nmk_str = String::new(); #[cfg(feature = "ast-comments")] @@ -2794,13 +2883,13 @@ impl<'a> fmt::Display for MemberKey<'a> { write!(f, "{}", nmk_str) } - MemberKey::ParenthesizedType { - non_member_key: ParenthesizedType::Type(t), + MemberKey::Parenthesized(ParenthesizedMemberKey { + non_member_key: ParenthesizedTypeOrGroup::Type(ParenthesizedType { pt, .. }), #[cfg(feature = "ast-comments")] comments_before_type_or_group, #[cfg(feature = "ast-comments")] comments_after_type_or_group, - } => { + }) => { let mut nmk_str = String::new(); #[cfg(feature = "ast-comments")] @@ -2808,7 +2897,7 @@ impl<'a> fmt::Display for MemberKey<'a> { nmk_str.push_str(&comments.to_string()); } - nmk_str.push_str(&t.to_string()); + nmk_str.push_str(&pt.to_string()); #[cfg(feature = "ast-comments")] if let Some(comments) = comments_after_type_or_group { @@ -2937,31 +3026,22 @@ mod tests { GroupEntry::ValueMemberKey { ge: Box::from(ValueMemberKeyEntry { occur: None, - member_key: Some(MemberKey::Bareword { + member_key: Some(MemberKey::Bareword(BarewordMemberKey { ident: "key1".into(), - comments: None, - comments_after_colon: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), + ..Default::default() + })), entry_type: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::TextValue { + type2: Type2::TextValue(TextValue { value: "value1".into(), - #[cfg(feature = "ast-span")] - span: Span::default(), - }, - operator: None, - comments_after_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), + ..Default::default() + }), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], - #[cfg(feature = "ast-span")] - span: Span::default(), + ..Default::default() }, }), leading_comments: None, @@ -2971,39 +3051,29 @@ mod tests { }, OptionalComma { optional_comma: true, - trailing_comments: None, - _a: PhantomData::default(), + ..Default::default() } ), ( GroupEntry::ValueMemberKey { ge: Box::from(ValueMemberKeyEntry { occur: None, - member_key: Some(MemberKey::Bareword { + member_key: Some(MemberKey::Bareword(BarewordMemberKey { ident: "key2".into(), - comments: None, - comments_after_colon: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }), + ..Default::default() + })), entry_type: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::TextValue { + type2: Type2::TextValue(TextValue { value: "value2".into(), - #[cfg(feature = "ast-span")] - span: Span::default(), - }, - operator: None, - comments_after_type: None, - #[cfg(feature = "ast-span")] - span: Span::default(), + ..Default::default() + }), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], - #[cfg(feature = "ast-span")] - span: Span::default(), + ..Default::default() }, }), leading_comments: None, @@ -3013,17 +3083,13 @@ mod tests { }, OptionalComma { optional_comma: true, - trailing_comments: None, - _a: PhantomData::default(), + ..Default::default() } ), ], - comments_before_grpchoice: None, - #[cfg(feature = "ast-span")] - span: Span::default(), + ..Default::default() }], - #[cfg(feature = "ast-span")] - span: Span::default(), + ..Default::default() } .to_string(), " key1: \"value1\", key2: \"value2\", ".to_string() diff --git a/src/faker.rs b/src/faker.rs index 3068a6cd..95b706d8 100644 --- a/src/faker.rs +++ b/src/faker.rs @@ -1,5 +1,5 @@ use crate::{ - ast::*, + ast::{self, *}, token::{lookup_ident, Token}, util::*, visitor::{self, *}, @@ -203,15 +203,15 @@ impl<'a> Visitor<'a, Error> for Faker<'a> { ctrl: &Token<'a>, controller: &Type2<'a>, ) -> visitor::Result { - if let Type2::Typename { + if let Type2::Typename(Typename { ident: target_ident, .. - } = target + }) = target { - if let Type2::Typename { + if let Type2::Typename(Typename { ident: controller_ident, .. - } = controller + }) = controller { if let Some(name) = self.eval_generic_rule { if let Some(gr) = self @@ -258,7 +258,7 @@ impl<'a> Visitor<'a, Error> for Faker<'a> { match ctrl { Token::SIZE => match target { - Type2::Typename { ident, .. } + Type2::Typename(Typename { ident, .. }) if is_ident_string_data_type(self.cddl, ident) || is_ident_uint_data_type(self.cddl, ident) => { @@ -281,26 +281,26 @@ impl<'a> Visitor<'a, Error> for Faker<'a> { is_inclusive: bool, ) -> visitor::Result { match lower { - Type2::IntValue { value: lower_v, .. } => match upper { - Type2::IntValue { value: upper, .. } => { + Type2::IntValue(IntValue { value: lower_v, .. }) => match upper { + Type2::IntValue(IntValue { value: upper, .. }) => { if is_inclusive { self.faked_json = Some((*lower_v..=*upper).fake::().into()); } else { self.faked_json = Some((*lower_v..*upper).fake::().into()); } } - Type2::UintValue { value: upper, .. } => { + Type2::UintValue(UintValue { value: upper, .. }) => { if is_inclusive { self.faked_json = Some((*lower_v..=*upper as isize).fake::().into()); } else { self.faked_json = Some((*lower_v..*upper as isize).fake::().into()); } } - Type2::Typename { + Type2::Typename(Typename { ident, generic_args, .. - } => { + }) => { if let Some(ga) = generic_args { return self.process_generic_args(ident, ga); } @@ -311,26 +311,26 @@ impl<'a> Visitor<'a, Error> for Faker<'a> { } _ => return Ok(()), }, - Type2::UintValue { value: lower_v, .. } => match upper { - Type2::IntValue { value: upper, .. } => { + Type2::UintValue(UintValue { value: lower_v, .. }) => match upper { + Type2::IntValue(IntValue { value: upper, .. }) => { if is_inclusive { self.faked_json = Some((*lower_v..=*upper as usize).fake::().into()); } else { self.faked_json = Some((*lower_v..*upper as usize).fake::().into()); } } - Type2::UintValue { value: upper, .. } => { + Type2::UintValue(UintValue { value: upper, .. }) => { if is_inclusive { self.faked_json = Some((*lower_v..=*upper).fake::().into()); } else { self.faked_json = Some((*lower_v..*upper).fake::().into()); } } - Type2::Typename { + Type2::Typename(Typename { ident, generic_args, .. - } => { + }) => { if let Some(ga) = generic_args { return self.process_generic_args(ident, ga); } @@ -341,19 +341,19 @@ impl<'a> Visitor<'a, Error> for Faker<'a> { } _ => return Ok(()), }, - Type2::FloatValue { value: lower_v, .. } => match upper { - Type2::FloatValue { value: upper, .. } => { + Type2::FloatValue(FloatValue { value: lower_v, .. }) => match upper { + Type2::FloatValue(FloatValue { value: upper, .. }) => { if is_inclusive { self.faked_json = Some((*lower_v..=*upper).fake::().into()); } else { self.faked_json = Some((*lower_v..*upper).fake::().into()); } } - Type2::Typename { + Type2::Typename(Typename { ident, generic_args, .. - } => { + }) => { if let Some(ga) = generic_args { return self.process_generic_args(ident, ga); } @@ -364,11 +364,11 @@ impl<'a> Visitor<'a, Error> for Faker<'a> { } _ => return Ok(()), }, - Type2::Typename { + Type2::Typename(Typename { ident, generic_args, .. - } => { + }) => { if let Some(ga) = generic_args { return self.process_generic_args(ident, ga); } @@ -392,10 +392,10 @@ impl<'a> Visitor<'a, Error> for Faker<'a> { if let Some(mk) = &entry.member_key { match mk { - MemberKey::Bareword { ident, .. } => { + MemberKey::Bareword(BarewordMemberKey { ident, .. }) => { entries.insert(ident.ident.to_string(), (occur, entry.entry_type.clone())); } - MemberKey::Value { value, .. } => { + MemberKey::Value(ValueMemberKey { value, .. }) => { entries.insert(value.to_string(), (occur, entry.entry_type.clone())); } _ => return Ok(()), @@ -413,14 +413,14 @@ impl<'a> Visitor<'a, Error> for Faker<'a> { fn visit_type2(&mut self, t2: &Type2<'a>) -> visitor::Result { match t2 { - Type2::TextValue { value, .. } => { + Type2::TextValue(TextValue { value, .. }) => { if let Some(Value::Array(array)) = self.faked_json.as_mut() { array.push(value.as_ref().into()); } else { self.faked_json = Some(value.as_ref().into()); } } - Type2::UTF8ByteString { value, .. } => { + Type2::UTF8ByteString(Utf8ByteString { value, .. }) => { let value = std::str::from_utf8(value.as_ref()).map_err(Error::Utf8Error)?; if let Some(Value::Array(array)) = self.faked_json.as_mut() { @@ -429,7 +429,7 @@ impl<'a> Visitor<'a, Error> for Faker<'a> { self.faked_json = Some(value.into()); } } - Type2::UintValue { value, .. } => match &self.ctrl { + Type2::UintValue(UintValue { value, .. }) => match &self.ctrl { Some((target, Token::SIZE)) => { if is_ident_string_data_type(self.cddl, target) { self.faked_json = Some(value.fake::().into()) @@ -439,9 +439,9 @@ impl<'a> Visitor<'a, Error> for Faker<'a> { } _ => self.faked_json = Some((*value).into()), }, - Type2::IntValue { value, .. } => self.faked_json = Some((*value).into()), - Type2::FloatValue { value, .. } => self.faked_json = Some((*value).into()), - Type2::Array { group, .. } => { + Type2::IntValue(IntValue { value, .. }) => self.faked_json = Some((*value).into()), + Type2::FloatValue(FloatValue { value, .. }) => self.faked_json = Some((*value).into()), + Type2::Array(Array { group, .. }) => { self.faked_json = Some(Value::Array(Vec::new())); self.entries = Entries::Array(Vec::new()); self.visit_group(group)?; @@ -620,7 +620,7 @@ impl<'a> Visitor<'a, Error> for Faker<'a> { self.entries = Entries::None; } - Type2::Map { group, .. } => { + Type2::Map(ast::Map { group, .. }) => { self.faked_json = Some(Value::Object(Map::default())); self.entries = Entries::Map(HashMap::new()); self.visit_group(group)?; @@ -658,22 +658,22 @@ impl<'a> Visitor<'a, Error> for Faker<'a> { self.entries = Entries::None; } - Type2::Typename { + Type2::Typename(Typename { ident, generic_args, .. - } => { + }) => { if let Some(ga) = generic_args { return self.process_generic_args(ident, ga); } return self.visit_identifier(ident); } - Type2::ChoiceFromGroup { + Type2::ChoiceFromGroup(ChoiceFromGroup { ident, generic_args, .. - } => { + }) => { if let Some(ga) = generic_args { self.is_group_to_choice_enum = true; @@ -712,20 +712,22 @@ impl<'a> Visitor<'a, Error> for Faker<'a> { self.faked_json = Some(Name(EN).fake::().into()); } else if let Token::NUMBER = lookup_ident(ident.ident) { if FFaker.fake::() { - self.faked_json = Some(FFaker.fake::().into()) + self.faked_json = Some(FFaker.fake::().into()); } else { - self.faked_json = Some(FFaker.fake::().into()) + self.faked_json = Some(FFaker.fake::().into()); } } else if is_ident_float_data_type(self.cddl, ident) { - self.faked_json = Some(FFaker.fake::().into()) + self.faked_json = Some(FFaker.fake::().into()); } else if is_ident_uint_data_type(self.cddl, ident) { - self.faked_json = Some(FFaker.fake::().into()) + self.faked_json = Some(FFaker.fake::().into()); } else if is_ident_nint_data_type(self.cddl, ident) { - self.faked_json = Some((..0).fake::().into()) + self.faked_json = Some((..0).fake::().into()); } else if is_ident_integer_data_type(self.cddl, ident) { - self.faked_json = Some(FFaker.fake::().into()) + self.faked_json = Some(FFaker.fake::().into()); } else if is_ident_bool_data_type(self.cddl, ident) { - self.faked_json = Some(FFaker.fake::().into()) + self.faked_json = Some(FFaker.fake::().into()); + } else if is_ident_null_data_type(self.cddl, ident) { + self.faked_json = Some(Value::Null); } else if let Some(rule) = rule_from_ident(self.cddl, ident) { self.visit_rule(rule)?; } else { diff --git a/src/parser.rs b/src/parser.rs index 327210ad..0189bf8b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -679,19 +679,20 @@ where // TODO: Replace with box pattern destructuring once supported in stable if let GroupEntry::ValueMemberKey { ge, .. } = &group_entry.0 { if ge.occur.is_none() && ge.member_key.is_none() { - let value = self.parse_type(Some(Type2::ParenthesizedType { - #[cfg(feature = "ast-comments")] - comments_before_type: comments_before_group.clone(), - pt: ge.entry_type.clone(), - #[cfg(feature = "ast-comments")] - comments_after_type: comments_after_group.clone(), - #[cfg(feature = "ast-span")] - span: Span( - begin_pt_range, - self.parser_position.range.1, - begin_rule_line, - ), - }))?; + let value = + self.parse_type(Some(Type2::ParenthesizedType(ParenthesizedType { + #[cfg(feature = "ast-comments")] + comments_before_type: comments_before_group.clone(), + pt: ge.entry_type.clone(), + #[cfg(feature = "ast-comments")] + comments_after_type: comments_after_group.clone(), + #[cfg(feature = "ast-span")] + span: Span( + begin_pt_range, + self.parser_position.range.1, + begin_rule_line, + ), + })))?; #[cfg(feature = "ast-span")] { @@ -992,13 +993,14 @@ where } #[cfg(feature = "ast-span")] - let begin_type_range = if let Some(Type2::ParenthesizedType { span, .. }) = parenthesized_type { - self.parser_position.line = span.2; + let begin_type_range = + if let Some(Type2::ParenthesizedType(ParenthesizedType { span, .. })) = parenthesized_type { + self.parser_position.line = span.2; - span.0 - } else { - self.parser_position.range.0 - }; + span.0 + } else { + self.parser_position.range.0 + }; let mut t = Type { type_choices: Vec::new(), @@ -1072,7 +1074,7 @@ where #[cfg(feature = "ast-span")] let mut begin_type1_range = self.lexer_position.range.0; - let t2_1 = if let Some(Type2::ParenthesizedType { + let t2_1 = if let Some(Type2::ParenthesizedType(ParenthesizedType { #[cfg(feature = "ast-comments")] comments_before_type, pt, @@ -1080,7 +1082,7 @@ where comments_after_type, #[cfg(feature = "ast-span")] span, - }) = parenthesized_type + })) = parenthesized_type { #[cfg(feature = "ast-span")] { @@ -1088,7 +1090,7 @@ where begin_type1_range = span.0; } - Type2::ParenthesizedType { + Type2::ParenthesizedType(ParenthesizedType { #[cfg(feature = "ast-comments")] comments_before_type, pt, @@ -1096,7 +1098,7 @@ where comments_after_type, #[cfg(feature = "ast-span")] span, - } + }) } else { self.parse_type2()? }; @@ -1211,64 +1213,68 @@ where ); match value { - token::Value::TEXT(t) => Ok(Type2::TextValue { + token::Value::TEXT(t) => Ok(Type2::TextValue(TextValue { value: t.clone(), #[cfg(feature = "ast-span")] span, - }), - token::Value::INT(i) => Ok(Type2::IntValue { + })), + token::Value::INT(i) => Ok(Type2::IntValue(IntValue { value: *i, #[cfg(feature = "ast-span")] span, - }), - token::Value::UINT(ui) => Ok(Type2::UintValue { + })), + token::Value::UINT(ui) => Ok(Type2::UintValue(UintValue { value: *ui, #[cfg(feature = "ast-span")] span, - }), - token::Value::FLOAT(f) => Ok(Type2::FloatValue { + })), + token::Value::FLOAT(f) => Ok(Type2::FloatValue(FloatValue { value: *f, #[cfg(feature = "ast-span")] span, - }), + })), token::Value::BYTE(token::ByteValue::UTF8(Cow::Borrowed(utf8))) => { - Ok(Type2::UTF8ByteString { + Ok(Type2::UTF8ByteString(Utf8ByteString { value: Cow::Borrowed(utf8), #[cfg(feature = "ast-span")] span, - }) + })) } token::Value::BYTE(token::ByteValue::UTF8(Cow::Owned(utf8))) => { - Ok(Type2::UTF8ByteString { + Ok(Type2::UTF8ByteString(Utf8ByteString { value: Cow::Owned(utf8.to_owned()), #[cfg(feature = "ast-span")] span, - }) + })) } token::Value::BYTE(token::ByteValue::B16(Cow::Borrowed(b16))) => { - Ok(Type2::B16ByteString { + Ok(Type2::B16ByteString(B16ByteString { value: Cow::Borrowed(b16), #[cfg(feature = "ast-span")] span, - }) + })) + } + token::Value::BYTE(token::ByteValue::B16(Cow::Owned(b16))) => { + Ok(Type2::B16ByteString(B16ByteString { + value: Cow::Owned(b16.to_owned()), + #[cfg(feature = "ast-span")] + span, + })) } - token::Value::BYTE(token::ByteValue::B16(Cow::Owned(b16))) => Ok(Type2::B16ByteString { - value: Cow::Owned(b16.to_owned()), - #[cfg(feature = "ast-span")] - span, - }), token::Value::BYTE(token::ByteValue::B64(Cow::Borrowed(b64))) => { - Ok(Type2::B64ByteString { + Ok(Type2::B64ByteString(B64ByteString { value: Cow::Borrowed(b64), #[cfg(feature = "ast-span")] span, - }) + })) + } + token::Value::BYTE(token::ByteValue::B64(Cow::Owned(b64))) => { + Ok(Type2::B64ByteString(B64ByteString { + value: Cow::Owned(b64.to_owned()), + #[cfg(feature = "ast-span")] + span, + })) } - token::Value::BYTE(token::ByteValue::B64(Cow::Owned(b64))) => Ok(Type2::B64ByteString { - value: Cow::Owned(b64.to_owned()), - #[cfg(feature = "ast-span")] - span, - }), } } @@ -1287,12 +1293,12 @@ where #[cfg(feature = "ast-span")] let end_type2_range = self.parser_position.range.1; - return Ok(Type2::Typename { + return Ok(Type2::Typename(Typename { ident, generic_args: Some(ga), #[cfg(feature = "ast-span")] span: Span(begin_type2_range, end_type2_range, begin_type2_line), - }); + })); } #[cfg(feature = "ast-span")] @@ -1301,7 +1307,7 @@ where self.parser_position.line = self.lexer_position.line; } - Ok(Type2::Typename { + Ok(Type2::Typename(Typename { ident: self.identifier_from_ident_token(*ident), generic_args: None, #[cfg(feature = "ast-span")] @@ -1310,7 +1316,7 @@ where self.parser_position.range.1, self.parser_position.line, ), - }) + })) } // ( type ) @@ -1341,7 +1347,7 @@ where #[cfg(not(feature = "ast-comments"))] self.advance_newline()?; - Ok(Type2::ParenthesizedType { + Ok(Type2::ParenthesizedType(ParenthesizedType { #[cfg(feature = "ast-comments")] comments_before_type, #[cfg(feature = "ast-comments")] @@ -1353,7 +1359,7 @@ where self.parser_position.range.1, self.parser_position.line, ), - }) + })) } // { group } @@ -1391,7 +1397,7 @@ where #[cfg(not(feature = "ast-comments"))] self.advance_newline()?; - Ok(Type2::Map { + Ok(Type2::Map(Map { #[cfg(feature = "ast-comments")] comments_before_group, group, @@ -1399,7 +1405,7 @@ where span, #[cfg(feature = "ast-comments")] comments_after_group, - }) + })) } // [ group ] @@ -1441,7 +1447,7 @@ where #[cfg(not(feature = "ast-comments"))] self.advance_newline()?; - Ok(Type2::Array { + Ok(Type2::Array(Array { #[cfg(feature = "ast-comments")] comments_before_group, group, @@ -1449,7 +1455,7 @@ where comments_after_group, #[cfg(feature = "ast-span")] span, - }) + })) } // ~ typename [genericarg] @@ -1473,24 +1479,24 @@ where if self.peek_token_is(&Token::LANGLEBRACKET) { self.next_token()?; - return Ok(Type2::Unwrap { + return Ok(Type2::Unwrap(Unwrap { #[cfg(feature = "ast-comments")] comments, ident, generic_args: Some(self.parse_genericargs()?), #[cfg(feature = "ast-span")] span: Span(0, 0, 0), - }); + })); } - return Ok(Type2::Unwrap { + return Ok(Type2::Unwrap(Unwrap { #[cfg(feature = "ast-comments")] comments, ident, generic_args: None, #[cfg(feature = "ast-span")] span: Span(0, 0, 0), - }); + })); } self.errors.push(Error::PARSER { @@ -1533,7 +1539,7 @@ where #[cfg(not(feature = "ast-comments"))] self.advance_newline()?; - Ok(Type2::ChoiceFromInlineGroup { + Ok(Type2::ChoiceFromInlineGroup(ChoiceFromInlineGroup { #[cfg(feature = "ast-comments")] comments, #[cfg(feature = "ast-comments")] @@ -1547,7 +1553,7 @@ where self.parser_position.range.1, begin_type2_line, ), - }) + })) } Token::IDENT(ident) => { let ident = self.identifier_from_ident_token(*ident); @@ -1556,7 +1562,7 @@ where let generic_args = Some(self.parse_genericargs()?); - return Ok(Type2::ChoiceFromGroup { + return Ok(Type2::ChoiceFromGroup(ChoiceFromGroup { #[cfg(feature = "ast-comments")] comments, ident, @@ -1567,7 +1573,7 @@ where self.parser_position.range.1, begin_type2_line, ), - }); + })); } #[cfg(feature = "ast-span")] @@ -1575,7 +1581,7 @@ where self.parser_position.range.1 = self.lexer_position.range.1; } - Ok(Type2::ChoiceFromGroup { + Ok(Type2::ChoiceFromGroup(ChoiceFromGroup { #[cfg(feature = "ast-comments")] comments, ident, @@ -1586,7 +1592,7 @@ where self.parser_position.range.1, begin_type2_line, ), - }) + })) } _ => { self.errors.push(Error::PARSER { @@ -1651,7 +1657,7 @@ where return Err(Error::INCREMENTAL); } - Ok(Type2::TaggedData { + Ok(Type2::TaggedData(TaggedData { tag, #[cfg(feature = "ast-comments")] comments_before_type, @@ -1664,10 +1670,10 @@ where self.parser_position.range.1, begin_type2_line, ), - }) + })) } // Tagged data of a major type - (Some(mt), constraint) => Ok(Type2::DataMajorType { + (Some(mt), constraint) => Ok(Type2::DataMajorType(DataMajorType { mt, constraint, #[cfg(feature = "ast-span")] @@ -1676,7 +1682,7 @@ where self.lexer_position.range.1, begin_type2_line, ), - }), + })), #[cfg(feature = "ast-span")] _ => Ok(Type2::Any(Span( begin_type2_range, @@ -1702,7 +1708,7 @@ where self.parser_position.line = self.lexer_position.line; } - Ok(Type2::Typename { + Ok(Type2::Typename(Typename { ident, generic_args: None, #[cfg(feature = "ast-span")] @@ -1711,7 +1717,7 @@ where self.parser_position.range.1, self.parser_position.line, ), - }) + })) } None => { #[cfg(feature = "ast-span")] @@ -2014,26 +2020,25 @@ where ); match member_key { - Some(MemberKey::ParenthesizedType { + Some(MemberKey::Parenthesized(ParenthesizedMemberKey { #[cfg(feature = "ast-comments")] - non_member_key: ParenthesizedType::Type(mut entry_type), + non_member_key: ParenthesizedTypeOrGroup::Type(entry_type), #[cfg(not(feature = "ast-comments"))] - non_member_key: ParenthesizedType::Type(entry_type), + non_member_key: ParenthesizedTypeOrGroup::Type(entry_type), #[cfg(feature = "ast-comments")] comments_before_type_or_group, - #[cfg(feature = "ast-comments")] - comments_after_type_or_group, - }) => { + .. + })) => { #[cfg(feature = "ast-span")] if let Token::COMMA = &self.cur_token { span.1 = self.lexer_position.range.1; } #[cfg(feature = "ast-comments")] - let trailing_comments = entry_type.comments_after_type(); + let trailing_comments = entry_type.comments_after_type; #[cfg(feature = "ast-span")] - if let Some((name, generic_args, _)) = entry_type.groupname_entry() { + if let Some((name, generic_args, _)) = entry_type.pt.groupname_entry() { if name.socket.is_none() && token::lookup_ident(name.ident) .in_standard_prelude() @@ -2063,7 +2068,7 @@ where } #[cfg(not(feature = "ast-span"))] - if let Some((name, generic_args)) = entry_type.groupname_entry() { + if let Some((name, generic_args)) = entry_type.pt.groupname_entry() { if name.socket.is_none() && token::lookup_ident(name.ident) .in_standard_prelude() @@ -2098,15 +2103,8 @@ where self.next_token()?; } - #[cfg(feature = "ast-comments")] - let trailing_comments = if let Some(comments) = entry_type.comments_after_type() { - Some(comments) - } else { - comments_after_type_or_group - }; - #[cfg(feature = "ast-span")] - if let Some((ident, _, _)) = entry_type.groupname_entry() { + if let Some((ident, _, _)) = entry_type.pt.groupname_entry() { if ident.socket.is_none() && token::lookup_ident(ident.ident) .in_standard_prelude() @@ -2123,7 +2121,7 @@ where } #[cfg(not(feature = "ast-span"))] - if let Some((ident, _)) = entry_type.groupname_entry() { + if let Some((ident, _)) = entry_type.pt.groupname_entry() { if ident.socket.is_none() && token::lookup_ident(ident.ident) .in_standard_prelude() @@ -2143,7 +2141,7 @@ where ge: Box::from(ValueMemberKeyEntry { occur, member_key: None, - entry_type, + entry_type: entry_type.pt, }), #[cfg(feature = "ast-comments")] leading_comments: comments_before_type_or_group, @@ -2153,13 +2151,13 @@ where span, }) } - Some(MemberKey::ParenthesizedType { - non_member_key: ParenthesizedType::Group(group), + Some(MemberKey::Parenthesized(ParenthesizedMemberKey { + non_member_key: ParenthesizedTypeOrGroup::Group(group), #[cfg(feature = "ast-comments")] comments_before_type_or_group, #[cfg(feature = "ast-comments")] comments_after_type_or_group, - }) => { + })) => { #[cfg(feature = "ast-span")] if let Token::COMMA = &self.cur_token { span.1 = self.lexer_position.range.1; @@ -2468,14 +2466,14 @@ where #[cfg(not(feature = "ast-comments"))] self.advance_newline()?; - let t1 = MemberKey::Type1 { + let t1 = MemberKey::Type1(Type1MemberKey { t1: Box::from(Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident, generic_args: None, #[cfg(feature = "ast-span")] span: Span(begin_memberkey_range, end_t1_range, begin_memberkey_line), - }, + }), operator: None, #[cfg(feature = "ast-comments")] comments_after_type: None, @@ -2495,7 +2493,7 @@ where end_memberkey_range, begin_memberkey_line, ), - }; + }); self.next_token()?; @@ -2516,14 +2514,14 @@ where #[cfg(not(feature = "ast-comments"))] self.advance_newline()?; - let t1 = MemberKey::Type1 { + let t1 = MemberKey::Type1(Type1MemberKey { t1: Box::from(Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident, generic_args: None, #[cfg(feature = "ast-span")] span: Span(begin_memberkey_range, end_t1_range, begin_memberkey_line), - }, + }), operator: None, #[cfg(feature = "ast-comments")] comments_after_type: None, @@ -2543,7 +2541,7 @@ where end_memberkey_range, begin_memberkey_line, ), - }; + }); self.next_token()?; @@ -2558,7 +2556,7 @@ where #[cfg(not(feature = "ast-comments"))] self.advance_newline()?; - Some(MemberKey::Bareword { + Some(MemberKey::Bareword(BarewordMemberKey { ident, #[cfg(feature = "ast-comments")] comments: comments_before_cut, @@ -2570,7 +2568,7 @@ where self.parser_position.range.1, begin_memberkey_line, ), - }) + })) }; Ok(mk) @@ -2657,7 +2655,7 @@ where #[cfg(not(feature = "ast-comments"))] self.advance_newline()?; - Some(MemberKey::Type1 { + Some(MemberKey::Type1(Type1MemberKey { t1: Box::from(t1), #[cfg(feature = "ast-comments")] comments_before_cut, @@ -2672,7 +2670,7 @@ where end_memberkey_range, begin_memberkey_line, ), - }) + })) } else { #[cfg(feature = "ast-comments")] let comments = self.collect_comments()?; @@ -2700,7 +2698,7 @@ where #[cfg(not(feature = "ast-comments"))] self.advance_newline()?; - Some(MemberKey::Value { + Some(MemberKey::Value(ValueMemberKey { value, #[cfg(feature = "ast-comments")] comments, @@ -2712,7 +2710,7 @@ where self.parser_position.range.1, begin_memberkey_line, ), - }) + })) }; if let Token::COLON = &self.cur_token { @@ -2817,13 +2815,13 @@ where Err(e) => return Err(e), }; - return Ok(Some(MemberKey::ParenthesizedType { - non_member_key: ParenthesizedType::Group(group), + return Ok(Some(MemberKey::Parenthesized(ParenthesizedMemberKey { + non_member_key: ParenthesizedTypeOrGroup::Group(group), #[cfg(feature = "ast-comments")] comments_before_type_or_group, #[cfg(feature = "ast-comments")] comments_after_type_or_group, - })); + }))); } // Parse tokens vec as type @@ -2865,9 +2863,9 @@ where #[cfg(feature = "ast-span")] let end_memberkey_range = self.lexer_position.range.1; - let t1 = Some(MemberKey::Type1 { + let t1 = Some(MemberKey::Type1(Type1MemberKey { t1: Box::from(Type1 { - type2: Type2::ParenthesizedType { + type2: Type2::ParenthesizedType(ParenthesizedType { pt: t, #[cfg(feature = "ast-comments")] comments_before_type: comments_before_type_or_group, @@ -2879,7 +2877,7 @@ where closing_parend_index, begin_memberkey_line, ), - }, + }), #[cfg(feature = "ast-comments")] comments_after_type: comments_before_cut.clone(), operator: None, @@ -2903,7 +2901,7 @@ where end_memberkey_range, begin_memberkey_line, ), - }); + })); return Ok(t1); } @@ -2921,9 +2919,9 @@ where #[cfg(not(feature = "ast-comments"))] self.advance_newline()?; - Some(MemberKey::Type1 { + Some(MemberKey::Type1(Type1MemberKey { t1: Box::from(Type1 { - type2: Type2::ParenthesizedType { + type2: Type2::ParenthesizedType(ParenthesizedType { pt: t, #[cfg(feature = "ast-comments")] comments_before_type: comments_before_type_or_group, @@ -2935,7 +2933,7 @@ where closing_parend_index, begin_memberkey_line, ), - }, + }), #[cfg(feature = "ast-comments")] comments_after_type: comments_before_cut.clone(), operator: None, @@ -2959,23 +2957,27 @@ where self.lexer_position.range.0, begin_memberkey_line, ), - }) + })) } else { - Some(MemberKey::ParenthesizedType { - non_member_key: ParenthesizedType::Type(Type { - type_choices: t.type_choices, + Some(MemberKey::Parenthesized(ParenthesizedMemberKey { + non_member_key: ParenthesizedTypeOrGroup::Type(ParenthesizedType { + pt: t, #[cfg(feature = "ast-span")] span: Span( begin_memberkey_range, self.parser_position.range.1, begin_memberkey_line, ), + #[cfg(feature = "ast-comments")] + comments_before_type: comments_before_type_or_group.clone(), + #[cfg(feature = "ast-comments")] + comments_after_type: comments_after_type_or_group.clone(), }), #[cfg(feature = "ast-comments")] - comments_before_type_or_group, - #[cfg(feature = "ast-comments")] comments_after_type_or_group, - }) + #[cfg(feature = "ast-comments")] + comments_before_type_or_group, + })) }; Ok(t1) @@ -3015,7 +3017,7 @@ where #[cfg(not(feature = "ast-comments"))] self.advance_newline()?; - return Ok(Some(MemberKey::Type1 { + return Ok(Some(MemberKey::Type1(Type1MemberKey { t1: Box::from(t1), #[cfg(feature = "ast-comments")] comments_before_cut, @@ -3030,7 +3032,7 @@ where end_memberkey_range, begin_memberkey_line, ), - })); + }))); } let t1 = if let Token::ARROWMAP = &self.cur_token { @@ -3046,7 +3048,7 @@ where #[cfg(not(feature = "ast-comments"))] self.advance_newline()?; - Some(MemberKey::Type1 { + Some(MemberKey::Type1(Type1MemberKey { t1: Box::from(t1), #[cfg(feature = "ast-comments")] comments_before_cut, @@ -3061,17 +3063,29 @@ where self.parser_position.range.1, begin_memberkey_line, ), - }) + })) } else { - Some(MemberKey::ParenthesizedType { - non_member_key: ParenthesizedType::Type(Type { - type_choices: vec![TypeChoice { - #[cfg(feature = "ast-comments")] - comments_before_type: None, - #[cfg(feature = "ast-comments")] - comments_after_type: None, - type1: t1, - }], + Some(MemberKey::Parenthesized(ParenthesizedMemberKey { + non_member_key: ParenthesizedTypeOrGroup::Type(ParenthesizedType { + pt: Type { + type_choices: vec![TypeChoice { + #[cfg(feature = "ast-comments")] + comments_before_type: None, + #[cfg(feature = "ast-comments")] + comments_after_type: None, + type1: t1, + }], + #[cfg(feature = "ast-span")] + span: Span( + begin_memberkey_range, + self.parser_position.range.1, + begin_memberkey_line, + ), + }, + #[cfg(feature = "ast-comments")] + comments_before_type: None, + #[cfg(feature = "ast-comments")] + comments_after_type: None, #[cfg(feature = "ast-span")] span: Span( begin_memberkey_range, @@ -3083,7 +3097,7 @@ where comments_before_type_or_group: None, #[cfg(feature = "ast-comments")] comments_after_type_or_group: comments_before_cut, - }) + })) }; Ok(t1) diff --git a/src/parser_tests.rs b/src/parser_tests.rs index ef242d0d..35f06d7b 100644 --- a/src/parser_tests.rs +++ b/src/parser_tests.rs @@ -247,29 +247,25 @@ mod tests { args: vec![ GenericArg { arg: Box::from(Type1 { - type2: Type2::TextValue { + type2: Type2::TextValue(TextValue { value: "reboot".into(), span: Span(1, 9, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(1, 9, 1), + ..Default::default() }), - comments_before_type: None, - comments_after_type: None, + ..Default::default() }, GenericArg { arg: Box::from(Type1 { - type2: Type2::TextValue { + type2: Type2::TextValue(TextValue { value: "now".into(), span: Span(11, 16, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(11, 16, 1), + ..Default::default() }), - comments_before_type: None, - comments_after_type: None, + ..Default::default() }, ], span: Span(0, 17, 1), @@ -292,12 +288,12 @@ mod tests { let expected_output = Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::ParenthesizedType { + type2: Type2::ParenthesizedType(ParenthesizedType { pt: Type { type_choices: vec![ TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "tchoice1".into(), socket: None, @@ -305,17 +301,15 @@ mod tests { }, generic_args: None, span: Span(2, 10, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(2, 10, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }, TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "tchoice2".into(), socket: None, @@ -323,27 +317,22 @@ mod tests { }, generic_args: None, span: Span(13, 21, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(13, 21, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }, ], span: Span(2, 21, 1), }, - comments_before_type: None, - comments_after_type: None, span: Span(0, 23, 1), - }, - operator: None, - comments_after_type: None, + ..Default::default() + }), span: Span(0, 23, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(0, 23, 1), }; @@ -367,19 +356,19 @@ mod tests { let expected_outputs = [ Type1 { - type2: Type2::UintValue { + type2: Type2::UintValue(UintValue { value: 5, span: Span(0, 1, 1), - }, + }), operator: Some(Operator { operator: RangeCtlOp::RangeOp { is_inclusive: true, span: Span(1, 3, 1), }, - type2: Type2::UintValue { + type2: Type2::UintValue(UintValue { value: 10, span: Span(3, 5, 1), - }, + }), comments_before_operator: None, comments_after_operator: None, }), @@ -387,19 +376,19 @@ mod tests { span: Span(0, 5, 1), }, Type1 { - type2: Type2::FloatValue { + type2: Type2::FloatValue(FloatValue { value: -10.5, span: Span(0, 5, 1), - }, + }), operator: Some(Operator { operator: RangeCtlOp::RangeOp { is_inclusive: false, span: Span(5, 8, 1), }, - type2: Type2::FloatValue { + type2: Type2::FloatValue(FloatValue { value: 10.1, span: Span(8, 12, 1), - }, + }), comments_before_operator: None, comments_after_operator: None, }), @@ -407,19 +396,19 @@ mod tests { span: Span(0, 12, 1), }, Type1 { - type2: Type2::FloatValue { + type2: Type2::FloatValue(FloatValue { value: 1.5, span: Span(0, 3, 1), - }, + }), operator: Some(Operator { operator: RangeCtlOp::RangeOp { is_inclusive: true, span: Span(3, 5, 1), }, - type2: Type2::FloatValue { + type2: Type2::FloatValue(FloatValue { value: 4.5, span: Span(5, 8, 1), - }, + }), comments_before_operator: None, comments_after_operator: None, }), @@ -427,7 +416,7 @@ mod tests { span: Span(0, 8, 1), }, Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "my..lower".into(), socket: None, @@ -435,13 +424,13 @@ mod tests { }, generic_args: None, span: Span(0, 9, 1), - }, + }), operator: Some(Operator { operator: RangeCtlOp::RangeOp { is_inclusive: false, span: Span(10, 13, 1), }, - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "upper".into(), socket: None, @@ -449,7 +438,7 @@ mod tests { }, generic_args: None, span: Span(14, 19, 1), - }, + }), comments_before_operator: None, comments_after_operator: None, }), @@ -457,7 +446,7 @@ mod tests { span: Span(0, 19, 1), }, Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "target".into(), socket: None, @@ -465,13 +454,13 @@ mod tests { }, generic_args: None, span: Span(0, 6, 1), - }, + }), operator: Some(Operator { operator: RangeCtlOp::CtlOp { ctrl: ".lt", span: Span(7, 10, 1), }, - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "controller".into(), socket: None, @@ -479,7 +468,7 @@ mod tests { }, generic_args: None, span: Span(11, 21, 1), - }, + }), comments_before_operator: None, comments_after_operator: None, }), @@ -487,12 +476,12 @@ mod tests { span: Span(0, 21, 1), }, Type1 { - type2: Type2::ParenthesizedType { + type2: Type2::ParenthesizedType(ParenthesizedType { pt: Type { type_choices: vec![ TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "text".into(), socket: None, @@ -500,17 +489,15 @@ mod tests { }, generic_args: None, span: Span(2, 6, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(2, 6, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }, TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "tstr".into(), socket: None, @@ -518,31 +505,28 @@ mod tests { }, generic_args: None, span: Span(9, 13, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(9, 13, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }, ], span: Span(2, 13, 1), }, - comments_before_type: None, - comments_after_type: None, span: Span(0, 15, 1), - }, + ..Default::default() + }), operator: Some(Operator { operator: RangeCtlOp::CtlOp { ctrl: ".eq", span: Span(16, 19, 1), }, - type2: Type2::TextValue { + type2: Type2::TextValue(TextValue { value: "hello".into(), span: Span(20, 27, 1), - }, + }), comments_before_operator: None, comments_after_operator: None, }), @@ -581,11 +565,11 @@ mod tests { ]; let expected_outputs = [ - Type2::TextValue { + Type2::TextValue(TextValue { value: "myvalue".into(), span: Span(0, 9, 1), - }, - Type2::Typename { + }), + Type2::Typename(Typename { ident: Identifier { ident: "message".into(), socket: None, @@ -595,36 +579,32 @@ mod tests { args: vec![ GenericArg { arg: Box::from(Type1 { - type2: Type2::TextValue { + type2: Type2::TextValue(TextValue { value: "reboot".into(), span: Span(8, 16, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(8, 16, 1), + ..Default::default() }), - comments_before_type: None, - comments_after_type: None, + ..Default::default() }, GenericArg { arg: Box::from(Type1 { - type2: Type2::TextValue { + type2: Type2::TextValue(TextValue { value: "now".into(), span: Span(18, 23, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(18, 23, 1), + ..Default::default() }), - comments_before_type: None, - comments_after_type: None, + ..Default::default() }, ], span: Span(7, 24, 1), }), span: Span(0, 24, 1), - }, - Type2::Typename { + }), + Type2::Typename(Typename { ident: Identifier { ident: "tcp-option".into(), socket: Some(SocketPlug::GROUP), @@ -632,23 +612,22 @@ mod tests { }, generic_args: None, span: Span(0, 12, 1), - }, - Type2::Unwrap { + }), + Type2::Unwrap(Unwrap { ident: Identifier { ident: "group1".into(), socket: None, span: Span(1, 7, 1), }, - generic_args: None, - comments: None, span: Span(0, 0, 0), - }, - Type2::TaggedData { + ..Default::default() + }), + Type2::TaggedData(TaggedData { tag: Some(997), t: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "tstr".into(), socket: None, @@ -656,26 +635,23 @@ mod tests { }, generic_args: None, span: Span(7, 11, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(7, 11, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(7, 11, 1), }, - comments_before_type: None, - comments_after_type: None, span: Span(0, 11, 1), - }, - Type2::FloatValue { + ..Default::default() + }), + Type2::FloatValue(FloatValue { value: 9.9, span: Span(0, 3, 1), - }, + }), Type2::Any(Span(0, 1, 1)), - Type2::Array { + Type2::Array(Array { group: Group { group_choices: vec![GroupChoice { group_entries: vec![( @@ -712,11 +688,10 @@ mod tests { }], span: Span(1, 11, 1), }, - comments_before_group: None, - comments_after_group: None, span: Span(0, 12, 1), - }, - Type2::Array { + ..Default::default() + }), + Type2::Array(Array { group: Group { group_choices: vec![GroupChoice { group_entries: vec![( @@ -749,21 +724,19 @@ mod tests { }], span: Span(1, 10, 1), }, - comments_before_group: None, - comments_after_group: None, span: Span(0, 11, 1), - }, - Type2::ChoiceFromGroup { + ..Default::default() + }), + Type2::ChoiceFromGroup(ChoiceFromGroup { ident: Identifier { ident: "groupname".into(), socket: None, span: Span(1, 10, 1), }, - generic_args: None, - comments: None, span: Span(0, 10, 1), - }, - Type2::ChoiceFromInlineGroup { + ..Default::default() + }), + Type2::ChoiceFromInlineGroup(ChoiceFromInlineGroup { group: Group { group_choices: vec![GroupChoice { group_entries: vec![( @@ -792,12 +765,10 @@ mod tests { }], span: Span(3, 14, 1), }, - comments: None, - comments_before_group: None, - comments_after_group: None, span: Span(0, 14, 1), - }, - Type2::Map { + ..Default::default() + }), + Type2::Map(Map { group: Group { group_choices: vec![GroupChoice { group_entries: vec![( @@ -808,26 +779,24 @@ mod tests { comments: None, _a: PhantomData::default(), }), - member_key: Some(MemberKey::Type1 { + member_key: Some(MemberKey::Type1(Type1MemberKey { t1: Box::from(Type1 { - type2: Type2::TextValue { + type2: Type2::TextValue(TextValue { value: "optional-key".into(), span: Span(4, 18, 1), - }, + }), operator: None, comments_after_type: None, span: Span(4, 18, 1), }), is_cut: true, - comments_before_cut: None, - comments_after_cut: None, - comments_after_arrowmap: None, span: Span(4, 23, 1), - }), + ..Default::default() + })), entry_type: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "int".into(), socket: None, @@ -835,13 +804,11 @@ mod tests { }, generic_args: None, span: Span(24, 27, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(24, 27, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(24, 27, 1), }, @@ -861,11 +828,10 @@ mod tests { }], span: Span(2, 28, 1), }, - comments_before_group: None, - comments_after_group: None, span: Span(0, 30, 1), - }, - Type2::Array { + ..Default::default() + }), + Type2::Array(Array { group: Group { group_choices: vec![GroupChoice { group_entries: vec![( @@ -877,7 +843,7 @@ mod tests { GroupEntry::ValueMemberKey { ge: Box::from(ValueMemberKeyEntry { occur: None, - member_key: Some(MemberKey::Bareword { + member_key: Some(MemberKey::Bareword(BarewordMemberKey { ident: Identifier { ident: "a".into(), socket: None, @@ -886,11 +852,11 @@ mod tests { comments: None, comments_after_colon: None, span: Span(4, 6, 1), - }), + })), entry_type: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "int".into(), socket: None, @@ -898,13 +864,11 @@ mod tests { }, generic_args: None, span: Span(7, 10, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(7, 10, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(7, 10, 1), }, @@ -923,20 +887,19 @@ mod tests { GroupEntry::ValueMemberKey { ge: Box::from(ValueMemberKeyEntry { occur: None, - member_key: Some(MemberKey::Bareword { + member_key: Some(MemberKey::Bareword(BarewordMemberKey { ident: Identifier { ident: "b".into(), socket: None, span: Span(12, 13, 1), }, - comments: None, - comments_after_colon: None, span: Span(12, 14, 1), - }), + ..Default::default() + })), entry_type: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "tstr".into(), socket: None, @@ -944,13 +907,11 @@ mod tests { }, generic_args: None, span: Span(15, 19, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(15, 19, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(15, 19, 1), }, @@ -987,10 +948,9 @@ mod tests { }], span: Span(2, 21, 1), }, - comments_before_group: None, - comments_after_group: None, span: Span(0, 23, 1), - }, + ..Default::default() + }), ]; for (idx, expected_output) in expected_outputs.iter().enumerate() { @@ -1013,7 +973,7 @@ mod tests { ]; let expected_ouputs = [ - Type2::Array { + Type2::Array(Array { group: Group { group_choices: vec![GroupChoice { group_entries: vec![ @@ -1025,7 +985,7 @@ mod tests { entry_type: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::Array { + type2: Type2::Array(Array { group: Group { group_choices: vec![GroupChoice { group_entries: vec![( @@ -1058,16 +1018,13 @@ mod tests { }], span: Span(3, 15, 1), }, - comments_before_group: None, - comments_after_group: None, span: Span(2, 16, 1), - }, - operator: None, - comments_after_type: None, + ..Default::default() + }), span: Span(2, 16, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(2, 16, 1), }, @@ -1090,7 +1047,7 @@ mod tests { entry_type: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::Array { + type2: Type2::Array(Array { group: Group { group_choices: vec![GroupChoice { group_entries: vec![( @@ -1123,16 +1080,14 @@ mod tests { }], span: Span(19, 36, 1), }, - comments_before_group: None, - comments_after_group: None, span: Span(18, 37, 1), - }, + ..Default::default() + }), operator: None, comments_after_type: None, span: Span(18, 37, 1), }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(18, 37, 1), }, @@ -1153,11 +1108,10 @@ mod tests { }], span: Span(2, 37, 1), }, - comments_before_group: None, - comments_after_group: None, span: Span(0, 39, 1), - }, - Type2::Map { + ..Default::default() + }), + Type2::Map(Map { group: Group { group_choices: vec![ GroupChoice { @@ -1259,11 +1213,10 @@ mod tests { ], span: Span(2, 23, 1), }, - comments_before_group: None, - comments_after_group: None, span: Span(0, 25, 1), - }, - Type2::Map { + ..Default::default() + }), + Type2::Map(Map { group: Group { group_choices: vec![GroupChoice { group_entries: vec![ @@ -1357,10 +1310,9 @@ mod tests { }], span: Span(2, 21, 1), }, - comments_before_group: None, - comments_after_group: None, span: Span(0, 23, 1), - }, + ..Default::default() + }), ]; for (idx, expected_output) in expected_ouputs.iter().enumerate() { @@ -1393,9 +1345,9 @@ mod tests { comments: None, _a: PhantomData::default(), }), - member_key: Some(MemberKey::Type1 { + member_key: Some(MemberKey::Type1(Type1MemberKey { t1: Box::from(Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "type1".into(), socket: None, @@ -1403,30 +1355,25 @@ mod tests { }, generic_args: None, span: Span(2, 7, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(2, 7, 1), + ..Default::default() }), is_cut: true, - comments_before_cut: None, - comments_after_cut: None, - comments_after_arrowmap: None, span: Span(2, 12, 1), - }), + ..Default::default() + })), entry_type: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::TextValue { + type2: Type2::TextValue(TextValue { value: "value".into(), span: Span(13, 20, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(13, 20, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(13, 20, 1), }, @@ -1438,20 +1385,19 @@ mod tests { GroupEntry::ValueMemberKey { ge: Box::from(ValueMemberKeyEntry { occur: None, - member_key: Some(MemberKey::Bareword { + member_key: Some(MemberKey::Bareword(BarewordMemberKey { ident: Identifier { ident: "type1".into(), socket: None, span: Span(0, 5, 1), }, - comments: None, - comments_after_colon: None, span: Span(0, 6, 1), - }), + ..Default::default() + })), entry_type: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "type2".into(), socket: None, @@ -1459,13 +1405,11 @@ mod tests { }, generic_args: None, span: Span(7, 12, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(7, 12, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(7, 12, 1), }, @@ -1495,16 +1439,16 @@ mod tests { comments: None, _a: PhantomData::default(), }), - member_key: Some(MemberKey::Value { + member_key: Some(MemberKey::Value(ValueMemberKey { value: token::Value::UINT(0), comments: None, comments_after_colon: None, span: Span(2, 4, 1), - }), + })), entry_type: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "addrdistr".into(), socket: None, @@ -1512,13 +1456,11 @@ mod tests { }, generic_args: None, span: Span(5, 14, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(5, 14, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(5, 14, 1), }, @@ -1530,16 +1472,16 @@ mod tests { GroupEntry::ValueMemberKey { ge: Box::from(ValueMemberKeyEntry { occur: None, - member_key: Some(MemberKey::Value { + member_key: Some(MemberKey::Value(ValueMemberKey { value: token::Value::UINT(0), comments: None, comments_after_colon: None, span: Span(0, 2, 1), - }), + })), entry_type: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "finite_set".into(), socket: None, @@ -1548,7 +1490,7 @@ mod tests { generic_args: Some(GenericArgs { args: vec![GenericArg { arg: Box::from(Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "transaction_input".into(), socket: None, @@ -1556,25 +1498,21 @@ mod tests { }, generic_args: None, span: Span(14, 31, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(14, 31, 1), + ..Default::default() }), - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(13, 32, 1), }), span: Span(3, 32, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(3, 32, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(3, 32, 1), }, @@ -1590,9 +1528,9 @@ mod tests { comments: None, _a: PhantomData::default(), }), - member_key: Some(MemberKey::Type1 { + member_key: Some(MemberKey::Type1(Type1MemberKey { t1: Box::from(Type1 { - type2: Type2::Array { + type2: Type2::Array(Array { group: Group { group_choices: vec![GroupChoice { group_entries: vec![( @@ -1621,24 +1559,20 @@ mod tests { }], span: Span(3, 13, 1), }, - comments_before_group: None, - comments_after_group: None, span: Span(2, 14, 1), - }, - operator: None, - comments_after_type: None, + ..Default::default() + }), span: Span(2, 14, 1), + ..Default::default() }), is_cut: false, - comments_before_cut: None, - comments_after_cut: None, - comments_after_arrowmap: None, span: Span(2, 22, 1), - }), + ..Default::default() + })), entry_type: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "coin".into(), socket: None, @@ -1646,13 +1580,11 @@ mod tests { }, generic_args: None, span: Span(18, 22, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(18, 22, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(18, 22, 1), }, @@ -1686,9 +1618,9 @@ mod tests { ]; let expected_outputs = [ - MemberKey::Type1 { + MemberKey::Type1(Type1MemberKey { t1: Box::from(Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "type1".into(), socket: None, @@ -1696,38 +1628,32 @@ mod tests { }, generic_args: None, span: Span(0, 5, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(0, 5, 1), + ..Default::default() }), - is_cut: false, - comments_before_cut: None, - comments_after_cut: None, - comments_after_arrowmap: None, span: Span(0, 8, 1), - }, - MemberKey::Type1 { + ..Default::default() + }), + MemberKey::Type1(Type1MemberKey { t1: Box::from(Type1 { - type2: Type2::ParenthesizedType { + type2: Type2::ParenthesizedType(ParenthesizedType { pt: Type { type_choices: vec![ TypeChoice { type1: Type1 { - type2: Type2::TextValue { + type2: Type2::TextValue(TextValue { value: "mytype1".into(), span: Span(2, 11, 1), - }, - operator: None, + }), span: Span(2, 11, 1), - comments_after_type: None, + ..Default::default() }, - comments_after_type: None, - comments_before_type: None, + ..Default::default() }, TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "int", span: Span(14, 17, 1), @@ -1735,63 +1661,55 @@ mod tests { }, span: Span(14, 17, 1), generic_args: None, - }, + }), span: Span(14, 17, 1), - comments_after_type: None, - operator: None, + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }, ], span: Span(2, 17, 1), }, span: Span(0, 19, 1), - comments_before_type: None, - comments_after_type: None, - }, - operator: None, - comments_after_type: None, + ..Default::default() + }), span: Span(0, 19, 1), + ..Default::default() }), is_cut: true, - comments_before_cut: None, - comments_after_cut: None, - comments_after_arrowmap: None, span: Span(0, 24, 1), - }, - MemberKey::Bareword { + ..Default::default() + }), + MemberKey::Bareword(BarewordMemberKey { ident: Identifier { ident: "mybareword".into(), socket: None, span: Span(0, 10, 1), }, - comments: None, - comments_after_colon: None, span: Span(0, 11, 1), - }, - MemberKey::Bareword { + ..Default::default() + }), + MemberKey::Bareword(BarewordMemberKey { ident: Identifier { ident: "my..bareword".into(), socket: None, span: Span(0, 12, 1), }, - comments: None, - comments_after_colon: None, span: Span(0, 13, 1), - }, - MemberKey::Value { + ..Default::default() + }), + MemberKey::Value(ValueMemberKey { value: token::Value::TEXT("myvalue".into()), comments: None, comments_after_colon: None, span: Span(0, 10, 1), - }, - MemberKey::Value { + }), + MemberKey::Value(ValueMemberKey { value: token::Value::UINT(0), comments: None, comments_after_colon: None, span: Span(0, 2, 1), - }, + }), ]; for (idx, expected_output) in expected_outputs.iter().enumerate() { diff --git a/src/util.rs b/src/util.rs index 425dd736..c8f5ffd3 100644 --- a/src/util.rs +++ b/src/util.rs @@ -23,14 +23,16 @@ pub fn text_value_from_ident<'a>(cddl: &'a CDDL, ident: &Identifier) -> Option<& if tc.type1.operator.is_none() { match &tc.type1.type2 { Type2::TextValue { .. } | Type2::UTF8ByteString { .. } => Some(&tc.type1.type2), - Type2::Typename { ident, .. } => text_value_from_ident(cddl, ident), - Type2::ParenthesizedType { pt, .. } => pt.type_choices.iter().find_map(|tc| { - if tc.type1.operator.is_none() { - text_value_from_type2(cddl, &tc.type1.type2) - } else { - None - } - }), + Type2::Typename(Typename { ident, .. }) => text_value_from_ident(cddl, ident), + Type2::ParenthesizedType(ParenthesizedType { pt, .. }) => { + pt.type_choices.iter().find_map(|tc| { + if tc.type1.operator.is_none() { + text_value_from_type2(cddl, &tc.type1.type2) + } else { + None + } + }) + } _ => None, } } else { @@ -45,9 +47,9 @@ pub fn text_value_from_ident<'a>(cddl: &'a CDDL, ident: &Identifier) -> Option<& /// Find text values from a given Type2 pub fn text_value_from_type2<'a>(cddl: &'a CDDL, t2: &'a Type2<'a>) -> Option<&'a Type2<'a>> { match t2 { - Type2::TextValue { .. } | Type2::UTF8ByteString { .. } => Some(t2), - Type2::Typename { ident, .. } => text_value_from_ident(cddl, ident), - Type2::Array { group, .. } => group.group_choices.iter().find_map(|gc| { + Type2::TextValue(_) | Type2::UTF8ByteString(_) => Some(t2), + Type2::Typename(Typename { ident, .. }) => text_value_from_ident(cddl, ident), + Type2::Array(Array { group, .. }) => group.group_choices.iter().find_map(|gc| { if gc.group_entries.len() == 2 { if let Some(ge) = gc.group_entries.first() { if let GroupEntry::ValueMemberKey { ge, .. } = &ge.0 { @@ -72,13 +74,15 @@ pub fn text_value_from_type2<'a>(cddl: &'a CDDL, t2: &'a Type2<'a>) -> Option<&' None } }), - Type2::ParenthesizedType { pt, .. } => pt.type_choices.iter().find_map(|tc| { - if tc.type1.operator.is_none() { - text_value_from_type2(cddl, &tc.type1.type2) - } else { - None - } - }), + Type2::ParenthesizedType(ParenthesizedType { pt, .. }) => { + pt.type_choices.iter().find_map(|tc| { + if tc.type1.operator.is_none() { + text_value_from_type2(cddl, &tc.type1.type2) + } else { + None + } + }) + } _ => None, } } @@ -106,11 +110,11 @@ pub fn unwrap_rule_from_ident<'a>(cddl: &'a CDDL, ident: &Identifier) -> Option< if type_choices.iter().any(match_fn) { Some(r) } else if let Some(ident) = type_choices.iter().find_map(|tc| { - if let Type2::Typename { + if let Type2::Typename(Typename { ident, generic_args: None, .. - } = &tc.type1.type2 + }) = &tc.type1.type2 { Some(ident) } else { @@ -161,7 +165,7 @@ pub fn numeric_range_bound_from_ident<'a>( Type2::IntValue { .. } | Type2::UintValue { .. } | Type2::FloatValue { .. } => { Some(type2) } - Type2::Typename { ident, .. } => numeric_range_bound_from_ident(cddl, ident), + Type2::Typename(Typename { ident, .. }) => numeric_range_bound_from_ident(cddl, ident), _ => None, }, _ => None, @@ -257,7 +261,7 @@ pub fn is_ident_null_data_type(cddl: &CDDL, ident: &Identifier) -> bool { cddl.rules.iter().any(|r| match r { Rule::Type { rule, .. } if &rule.name == ident => rule.value.type_choices.iter().any(|tc| { - if let Type2::Typename { ident, .. } = &tc.type1.type2 { + if let Type2::Typename(Typename { ident, .. }) = &tc.type1.type2 { is_ident_null_data_type(cddl, ident) } else { false @@ -275,7 +279,7 @@ pub fn is_ident_bool_data_type(cddl: &CDDL, ident: &Identifier) -> bool { cddl.rules.iter().any(|r| match r { Rule::Type { rule, .. } if &rule.name == ident => rule.value.type_choices.iter().any(|tc| { - if let Type2::Typename { ident, .. } = &tc.type1.type2 { + if let Type2::Typename(Typename { ident, .. }) = &tc.type1.type2 { is_ident_bool_data_type(cddl, ident) } else { false @@ -301,7 +305,7 @@ pub fn ident_matches_bool_value(cddl: &CDDL, ident: &Identifier, value: bool) -> cddl.rules.iter().any(|r| match r { Rule::Type { rule, .. } if &rule.name == ident => rule.value.type_choices.iter().any(|tc| { - if let Type2::Typename { ident, .. } = &tc.type1.type2 { + if let Type2::Typename(Typename { ident, .. }) = &tc.type1.type2 { ident_matches_bool_value(cddl, ident, value) } else { false @@ -319,7 +323,7 @@ pub fn is_ident_uri_data_type(cddl: &CDDL, ident: &Identifier) -> bool { cddl.rules.iter().any(|r| match r { Rule::Type { rule, .. } if &rule.name == ident => rule.value.type_choices.iter().any(|tc| { - if let Type2::Typename { ident, .. } = &tc.type1.type2 { + if let Type2::Typename(Typename { ident, .. }) = &tc.type1.type2 { is_ident_uri_data_type(cddl, ident) } else { false @@ -337,7 +341,7 @@ pub fn is_ident_b64url_data_type(cddl: &CDDL, ident: &Identifier) -> bool { cddl.rules.iter().any(|r| match r { Rule::Type { rule, .. } if &rule.name == ident => rule.value.type_choices.iter().any(|tc| { - if let Type2::Typename { ident, .. } = &tc.type1.type2 { + if let Type2::Typename(Typename { ident, .. }) = &tc.type1.type2 { is_ident_b64url_data_type(cddl, ident) } else { false @@ -355,7 +359,7 @@ pub fn is_ident_tdate_data_type(cddl: &CDDL, ident: &Identifier) -> bool { cddl.rules.iter().any(|r| match r { Rule::Type { rule, .. } if &rule.name == ident => rule.value.type_choices.iter().any(|tc| { - if let Type2::Typename { ident, .. } = &tc.type1.type2 { + if let Type2::Typename(Typename { ident, .. }) = &tc.type1.type2 { is_ident_tdate_data_type(cddl, ident) } else { false @@ -373,7 +377,7 @@ pub fn is_ident_time_data_type(cddl: &CDDL, ident: &Identifier) -> bool { cddl.rules.iter().any(|r| match r { Rule::Type { rule, .. } if &rule.name == ident => rule.value.type_choices.iter().any(|tc| { - if let Type2::Typename { ident, .. } = &tc.type1.type2 { + if let Type2::Typename(Typename { ident, .. }) = &tc.type1.type2 { is_ident_time_data_type(cddl, ident) } else { false @@ -403,7 +407,7 @@ pub fn is_ident_numeric_data_type(cddl: &CDDL, ident: &Identifier) -> bool { cddl.rules.iter().any(|r| match r { Rule::Type { rule, .. } if rule.name == *ident => rule.value.type_choices.iter().any(|tc| { - if let Type2::Typename { ident, .. } = &tc.type1.type2 { + if let Type2::Typename(Typename { ident, .. }) = &tc.type1.type2 { is_ident_numeric_data_type(cddl, ident) } else { false @@ -421,7 +425,7 @@ pub fn is_ident_uint_data_type(cddl: &CDDL, ident: &Identifier) -> bool { cddl.rules.iter().any(|r| match r { Rule::Type { rule, .. } if rule.name == *ident => rule.value.type_choices.iter().any(|tc| { - if let Type2::Typename { ident, .. } = &tc.type1.type2 { + if let Type2::Typename(Typename { ident, .. }) = &tc.type1.type2 { is_ident_uint_data_type(cddl, ident) } else { false @@ -439,7 +443,7 @@ pub fn is_ident_nint_data_type(cddl: &CDDL, ident: &Identifier) -> bool { cddl.rules.iter().any(|r| match r { Rule::Type { rule, .. } if rule.name == *ident => rule.value.type_choices.iter().any(|tc| { - if let Type2::Typename { ident, .. } = &tc.type1.type2 { + if let Type2::Typename(Typename { ident, .. }) = &tc.type1.type2 { is_ident_nint_data_type(cddl, ident) } else { false @@ -459,7 +463,7 @@ pub fn is_ident_integer_data_type(cddl: &CDDL, ident: &Identifier) -> bool { cddl.rules.iter().any(|r| match r { Rule::Type { rule, .. } if rule.name == *ident => rule.value.type_choices.iter().any(|tc| { - if let Type2::Typename { ident, .. } = &tc.type1.type2 { + if let Type2::Typename(Typename { ident, .. }) = &tc.type1.type2 { is_ident_integer_data_type(cddl, ident) } else { false @@ -483,7 +487,7 @@ pub fn is_ident_float_data_type(cddl: &CDDL, ident: &Identifier) -> bool { cddl.rules.iter().any(|r| match r { Rule::Type { rule, .. } if rule.name == *ident => rule.value.type_choices.iter().any(|tc| { - if let Type2::Typename { ident, .. } = &tc.type1.type2 { + if let Type2::Typename(Typename { ident, .. }) = &tc.type1.type2 { is_ident_float_data_type(cddl, ident) } else { false @@ -501,7 +505,7 @@ pub fn is_ident_string_data_type(cddl: &CDDL, ident: &Identifier) -> bool { cddl.rules.iter().any(|r| match r { Rule::Type { rule, .. } if rule.name == *ident => rule.value.type_choices.iter().any(|tc| { - if let Type2::Typename { ident, .. } = &tc.type1.type2 { + if let Type2::Typename(Typename { ident, .. }) = &tc.type1.type2 { is_ident_string_data_type(cddl, ident) } else { false @@ -519,7 +523,7 @@ pub fn is_ident_any_type(cddl: &CDDL, ident: &Identifier) -> bool { cddl.rules.iter().any(|r| match r { Rule::Type { rule, .. } if rule.name == *ident => rule.value.type_choices.iter().any(|tc| { - if let Type2::Typename { ident, .. } = &tc.type1.type2 { + if let Type2::Typename(Typename { ident, .. }) = &tc.type1.type2 { is_ident_any_type(cddl, ident) } else { false @@ -537,7 +541,7 @@ pub fn is_ident_byte_string_data_type(cddl: &CDDL, ident: &Identifier) -> bool { cddl.rules.iter().any(|r| match r { Rule::Type { rule, .. } if rule.name == *ident => rule.value.type_choices.iter().any(|tc| { - if let Type2::Typename { ident, .. } = &tc.type1.type2 { + if let Type2::Typename(Typename { ident, .. }) = &tc.type1.type2 { is_ident_byte_string_data_type(cddl, ident) } else { false diff --git a/src/validator/cbor.rs b/src/validator/cbor.rs index 102385b3..10a5a53b 100644 --- a/src/validator/cbor.rs +++ b/src/validator/cbor.rs @@ -743,8 +743,8 @@ where } match lower { - Type2::IntValue { value: l, .. } => match upper { - Type2::IntValue { value: u, .. } => { + Type2::IntValue(IntValue { value: l, .. }) => match upper { + Type2::IntValue(IntValue { value: u, .. }) => { let error_str = if is_inclusive { format!( "expected integer to be in range {} <= value <= {}, got {:?}", @@ -778,7 +778,7 @@ where } } } - Type2::UintValue { value: u, .. } => { + Type2::UintValue(UintValue { value: u, .. }) => { let error_str = if is_inclusive { format!( "expected integer to be in range {} <= value <= {}, got {:?}", @@ -820,8 +820,8 @@ where return Ok(()); } }, - Type2::UintValue { value: l, .. } => match upper { - Type2::UintValue { value: u, .. } => { + Type2::UintValue(UintValue { value: l, .. }) => match upper { + Type2::UintValue(UintValue { value: u, .. }) => { let error_str = if is_inclusive { format!( "expected uint to be in range {} <= value <= {}, got {:?}", @@ -889,8 +889,8 @@ where return Ok(()); } }, - Type2::FloatValue { value: l, .. } => match upper { - Type2::FloatValue { value: u, .. } => { + Type2::FloatValue(FloatValue { value: l, .. }) => match upper { + Type2::FloatValue(FloatValue { value: u, .. }) => { let error_str = if is_inclusive { format!( "expected float to be in range {} <= value <= {}, got {:?}", @@ -951,15 +951,15 @@ where ctrl: &Token<'a>, controller: &Type2<'a>, ) -> visitor::Result> { - if let Type2::Typename { + if let Type2::Typename(Typename { ident: target_ident, .. - } = target + }) = target { - if let Type2::Typename { + if let Type2::Typename(Typename { ident: controller_ident, .. - } = controller + }) = controller { if let Some(name) = self.eval_generic_rule { if let Some(gr) = self @@ -1007,14 +1007,14 @@ where match ctrl { Token::EQ => { match target { - Type2::Typename { ident, .. } => { + Type2::Typename(Typename { ident, .. }) => { if is_ident_string_data_type(self.cddl, ident) || is_ident_numeric_data_type(self.cddl, ident) { return self.visit_type2(controller); } } - Type2::Array { group, .. } => { + Type2::Array(Array { group, .. }) => { if let Value::Array(_) = &self.cbor { let mut entry_counts = Vec::new(); for gc in group.group_choices.iter() { @@ -1027,7 +1027,7 @@ where return Ok(()); } } - Type2::Map { .. } => { + Type2::Map(Map { .. }) => { if let Value::Map(_) = &self.cbor { self.ctrl = Some(ctrl.clone()); self.is_ctrl_map_equality = true; @@ -1046,7 +1046,7 @@ where } Token::NE => { match target { - Type2::Typename { ident, .. } => { + Type2::Typename(Typename { ident, .. }) => { if is_ident_string_data_type(self.cddl, ident) || is_ident_numeric_data_type(self.cddl, ident) { @@ -1056,7 +1056,7 @@ where return Ok(()); } } - Type2::Array { .. } => { + Type2::Array(Array { .. }) => { if let Value::Array(_) = &self.cbor { self.ctrl = Some(ctrl.clone()); self.visit_type2(controller)?; @@ -1064,7 +1064,7 @@ where return Ok(()); } } - Type2::Map { .. } => { + Type2::Map(Map { .. }) => { if let Value::Map(_) = &self.cbor { self.ctrl = Some(ctrl.clone()); self.is_ctrl_map_equality = true; @@ -1082,7 +1082,7 @@ where Ok(()) } Token::LT | Token::GT | Token::GE | Token::LE => match target { - Type2::Typename { ident, .. } if is_ident_numeric_data_type(self.cddl, ident) => { + Type2::Typename(Typename { ident, .. }) if is_ident_numeric_data_type(self.cddl, ident) => { self.ctrl = Some(ctrl.clone()); self.visit_type2(controller)?; self.ctrl = None; @@ -1097,7 +1097,7 @@ where } }, Token::SIZE => match target { - Type2::Typename { ident, .. } + Type2::Typename(Typename { ident, .. }) if is_ident_string_data_type(self.cddl, ident) || is_ident_uint_data_type(self.cddl, ident) || is_ident_byte_string_data_type(self.cddl, ident) => @@ -1169,7 +1169,9 @@ where Token::REGEXP | Token::PCRE => { self.ctrl = Some(ctrl.clone()); match target { - Type2::Typename { ident, .. } if is_ident_string_data_type(self.cddl, ident) => { + Type2::Typename(Typename { ident, .. }) + if is_ident_string_data_type(self.cddl, ident) => + { match self.cbor { Value::Text(_) | Value::Array(_) => self.visit_type2(controller)?, _ => self.add_error(format!( @@ -1190,7 +1192,9 @@ where Token::CBOR | Token::CBORSEQ => { self.ctrl = Some(ctrl.clone()); match target { - Type2::Typename { ident, .. } if is_ident_byte_string_data_type(self.cddl, ident) => { + Type2::Typename(Typename { ident, .. }) + if is_ident_byte_string_data_type(self.cddl, ident) => + { match &self.cbor { Value::Bytes(_) | Value::Array(_) => self.visit_type2(controller)?, _ => self.add_error(format!( @@ -1211,7 +1215,7 @@ where Token::BITS => { self.ctrl = Some(ctrl.clone()); match target { - Type2::Typename { ident, .. } + Type2::Typename(Typename { ident, .. }) if is_ident_byte_string_data_type(self.cddl, ident) || is_ident_uint_data_type(self.cddl, ident) => { @@ -1324,10 +1328,12 @@ where self.ctrl = Some(ctrl.clone()); match target { - Type2::Typename { ident, .. } if is_ident_string_data_type(self.cddl, ident) => { + Type2::Typename(Typename { ident, .. }) + if is_ident_string_data_type(self.cddl, ident) => + { match self.cbor { Value::Text(_) | Value::Array(_) => { - if let Type2::ParenthesizedType { pt, .. } = controller { + if let Type2::ParenthesizedType(ParenthesizedType { pt, .. }) = controller { match abnf_from_complex_controller(self.cddl, pt) { Ok(values) => { let error_count = self.errors.len(); @@ -1372,10 +1378,12 @@ where self.ctrl = Some(ctrl.clone()); match target { - Type2::Typename { ident, .. } if is_ident_byte_string_data_type(self.cddl, ident) => { + Type2::Typename(Typename { ident, .. }) + if is_ident_byte_string_data_type(self.cddl, ident) => + { match self.cbor { Value::Bytes(_) | Value::Array(_) => { - if let Type2::ParenthesizedType { pt, .. } = controller { + if let Type2::ParenthesizedType(ParenthesizedType { pt, .. }) = controller { match abnf_from_complex_controller(self.cddl, pt) { Ok(values) => { let error_count = self.errors.len(); @@ -1422,7 +1430,7 @@ where if let Some(ef) = self.enabled_features { let tv = text_value_from_type2(self.cddl, controller); - if let Some(Type2::TextValue { value, .. }) = tv { + if let Some(Type2::TextValue(TextValue { value, .. })) = tv { if ef.contains(&&**value) { let err_count = self.errors.len(); self.visit_type2(target)?; @@ -1436,7 +1444,7 @@ where .get_or_insert(vec![value.to_string()]) .push(value.to_string()); } - } else if let Some(Type2::UTF8ByteString { value, .. }) = tv { + } else if let Some(Type2::UTF8ByteString(Utf8ByteString { value, .. })) = tv { let value = std::str::from_utf8(value).map_err(Error::UTF8Parsing)?; if ef.contains(&value) { let err_count = self.errors.len(); @@ -1465,7 +1473,7 @@ where if let Some(ef) = &self.enabled_features { let tv = text_value_from_type2(self.cddl, controller); - if let Some(Type2::TextValue { value, .. }) = tv { + if let Some(Type2::TextValue(TextValue { value, .. })) = tv { if ef.contains(&JsValue::from(value.as_ref())) { let err_count = self.errors.len(); self.visit_type2(target)?; @@ -1479,7 +1487,7 @@ where .get_or_insert(vec![value.to_string()]) .push(value.to_string()); } - } else if let Some(Type2::UTF8ByteString { value, .. }) = tv { + } else if let Some(Type2::UTF8ByteString(Utf8ByteString { value, .. })) = tv { let value = std::str::from_utf8(value).map_err(Error::UTF8Parsing)?; if ef.contains(&JsValue::from(value)) { let err_count = self.errors.len(); @@ -1598,8 +1606,10 @@ where } match t2 { - Type2::TextValue { value, .. } => self.visit_value(&token::Value::TEXT(value.clone())), - Type2::Map { group, .. } => match &self.cbor { + Type2::TextValue(TextValue { value, .. }) => { + self.visit_value(&token::Value::TEXT(value.clone())) + } + Type2::Map(Map { group, .. }) => match &self.cbor { Value::Map(m) => { if self.is_member_key { let current_location = self.cbor_location.clone(); @@ -1759,7 +1769,7 @@ where Ok(()) } }, - Type2::Array { group, .. } => match &self.cbor { + Type2::Array(Array { group, .. }) => match &self.cbor { Value::Array(a) => { if group.group_choices.len() == 1 && group.group_choices[0].group_entries.is_empty() @@ -1847,11 +1857,11 @@ where Ok(()) } }, - Type2::ChoiceFromGroup { + Type2::ChoiceFromGroup(ChoiceFromGroup { ident, generic_args, .. - } => { + }) => { if let Some(ga) = generic_args { if let Some(rule) = rule_from_ident(self.cddl, ident) { if let Some(gr) = self @@ -1904,17 +1914,17 @@ where Ok(()) } - Type2::ChoiceFromInlineGroup { group, .. } => { + Type2::ChoiceFromInlineGroup(ChoiceFromInlineGroup { group, .. }) => { self.is_group_to_choice_enum = true; self.visit_group(group)?; self.is_group_to_choice_enum = false; Ok(()) } - Type2::Typename { + Type2::Typename(Typename { ident, generic_args, .. - } => { + }) => { if let Some(ga) = generic_args { if let Some(rule) = rule_from_ident(self.cddl, ident) { if let Some(gr) = self @@ -1954,19 +1964,21 @@ where self.visit_identifier(ident) } - Type2::IntValue { value, .. } => self.visit_value(&token::Value::INT(*value)), - Type2::UintValue { value, .. } => self.visit_value(&token::Value::UINT(*value)), - Type2::FloatValue { value, .. } => self.visit_value(&token::Value::FLOAT(*value)), - Type2::ParenthesizedType { pt, .. } => self.visit_type(pt), - Type2::Unwrap { + Type2::IntValue(IntValue { value, .. }) => self.visit_value(&token::Value::INT(*value)), + Type2::UintValue(UintValue { value, .. }) => self.visit_value(&token::Value::UINT(*value)), + Type2::FloatValue(FloatValue { value, .. }) => self.visit_value(&token::Value::FLOAT(*value)), + Type2::ParenthesizedType(ParenthesizedType { pt, .. }) => self.visit_type(pt), + Type2::Unwrap(Unwrap { ident, generic_args, .. - } => { + }) => { // Per // https://github.com/w3c/did-spec-registries/pull/138#issuecomment-719739215, // strip tag and validate underlying type - if let Some(Type2::TaggedData { t, .. }) = tag_from_token(&lookup_ident(ident.ident)) { + if let Some(Type2::TaggedData(TaggedData { t, .. })) = + tag_from_token(&lookup_ident(ident.ident)) + { return self.visit_type(&t); } @@ -2018,7 +2030,7 @@ where Ok(()) } - Type2::TaggedData { tag, t, .. } => match &self.cbor { + Type2::TaggedData(TaggedData { tag, t, .. }) => match &self.cbor { Value::Tag(actual_tag, value) => { if let Some(tag) = tag { if *tag as u64 != *actual_tag { @@ -2074,7 +2086,7 @@ where Ok(()) } }, - Type2::DataMajorType { mt, constraint, .. } => match &self.cbor { + Type2::DataMajorType(DataMajorType { mt, constraint, .. }) => match &self.cbor { Value::Integer(i) => { match mt { 0u8 => match constraint { @@ -3116,7 +3128,7 @@ where fn visit_memberkey(&mut self, mk: &MemberKey<'a>) -> visitor::Result> { match mk { - MemberKey::Type1 { is_cut, .. } => { + MemberKey::Type1(Type1MemberKey { is_cut, .. }) => { self.is_cut_present = *is_cut; walk_memberkey(self, mk)?; self.is_cut_present = false; diff --git a/src/validator/control.rs b/src/validator/control.rs index cffe9c91..be149706 100644 --- a/src/validator/control.rs +++ b/src/validator/control.rs @@ -1,7 +1,7 @@ #![cfg(any(feature = "json", feature = "cbor"))] #![cfg(not(feature = "lsp"))] -use crate::ast::{Identifier, Operator, RangeCtlOp, Rule, Type2, CDDL}; +use crate::ast::*; #[cfg(feature = "additional-controls")] use crate::{ast::Type, token::lookup_control_from_str, validator::ByteValue, Token}; @@ -23,7 +23,7 @@ pub fn string_literals_from_ident<'a>(cddl: &'a CDDL, ident: &Identifier) -> Vec | t @ Type2::UTF8ByteString { .. } | t @ Type2::B16ByteString { .. } | t @ Type2::B64ByteString { .. } => literals.push(t), - Type2::Typename { ident, .. } => { + Type2::Typename(Typename { ident, .. }) => { literals.append(&mut string_literals_from_ident(cddl, ident)) } _ => continue, @@ -48,7 +48,7 @@ pub fn numeric_values_from_ident<'a>(cddl: &'a CDDL, ident: &Identifier) -> Vec< t @ Type2::IntValue { .. } | t @ Type2::UintValue { .. } | t @ Type2::FloatValue { .. } => literals.push(t), - Type2::Typename { ident, .. } => { + Type2::Typename(Typename { ident, .. }) => { literals.append(&mut numeric_values_from_ident(cddl, ident)) } _ => continue, @@ -74,11 +74,11 @@ pub fn cat_operation<'a>( let ctrl = if is_dedent { Token::DET } else { Token::CAT }; match target { - Type2::TextValue { value, .. } => match controller { + Type2::TextValue(TextValue { value, .. }) => match controller { // "testing" .cat "123" - Type2::TextValue { + Type2::TextValue(TextValue { value: controller, .. - } => { + }) => { if is_dedent { literals.push(format!("{}{}", dedent_str(value), dedent_str(controller)).into()) } else { @@ -86,7 +86,7 @@ pub fn cat_operation<'a>( } } // "testing" .cat a - Type2::Typename { ident, .. } => { + Type2::Typename(Typename { ident, .. }) => { let sl = string_literals_from_ident(cddl, ident); if sl.is_empty() { return Err(format!( @@ -99,9 +99,9 @@ pub fn cat_operation<'a>( } } // "testing" .cat '123' - Type2::UTF8ByteString { + Type2::UTF8ByteString(Utf8ByteString { value: controller, .. - } => match std::str::from_utf8(controller) { + }) => match std::str::from_utf8(controller) { Ok(controller) => { let controller = controller.trim_start_matches('\'').trim_end_matches('\''); @@ -114,9 +114,9 @@ pub fn cat_operation<'a>( Err(e) => return Err(format!("error parsing byte string: {}", e)), }, // "testing" .cat h'313233' - Type2::B16ByteString { + Type2::B16ByteString(B16ByteString { value: controller, .. - } => match base16::decode(controller) { + }) => match base16::decode(controller) { Ok(controller) => match String::from_utf8(controller) { Ok(controller) => { if is_dedent { @@ -130,9 +130,9 @@ pub fn cat_operation<'a>( Err(e) => return Err(format!("error decoding base16 byte string literal: {}", e)), }, // "testing" .cat b64'MTIz' - Type2::B64ByteString { + Type2::B64ByteString(B64ByteString { value: controller, .. - } => match base64::decode_config(controller, base64::URL_SAFE) { + }) => match base64::decode_config(controller, base64::URL_SAFE) { Ok(controller) => match String::from_utf8(controller) { Ok(controller) => { if is_dedent { @@ -151,7 +151,7 @@ pub fn cat_operation<'a>( } }, // "testing" .cat ( "123" / "1234" ) - Type2::ParenthesizedType { pt: controller, .. } => { + Type2::ParenthesizedType(ParenthesizedType { pt: controller, .. }) => { for controller in controller.type_choices.iter() { if controller.type1.operator.is_none() { literals.append(&mut cat_operation( @@ -166,7 +166,7 @@ pub fn cat_operation<'a>( _ => return Err(format!("invalid controller used for {} operation", ctrl)), }, // a .cat "123" - Type2::Typename { ident, .. } => { + Type2::Typename(Typename { ident, .. }) => { // Only grab the first type choice literal from the target per // https://github.com/cbor-wg/cddl-control/issues/2#issuecomment-729253368 if let Some(value) = string_literals_from_ident(cddl, ident).first() { @@ -176,7 +176,7 @@ pub fn cat_operation<'a>( } } // ( "test" / "testing" ) .cat "123" - Type2::ParenthesizedType { pt: target, .. } => { + Type2::ParenthesizedType(ParenthesizedType { pt: target, .. }) => { // Only grab the first type choice literal from the target per // https://github.com/cbor-wg/cddl-control/issues/2#issuecomment-729253368 if let Some(tc) = target.type_choices.first() { @@ -193,12 +193,12 @@ pub fn cat_operation<'a>( return Err(format!("invalid target type in {} control operator", ctrl)); } - Type2::UTF8ByteString { value, .. } => match std::str::from_utf8(value) { + Type2::UTF8ByteString(Utf8ByteString { value, .. }) => match std::str::from_utf8(value) { Ok(value) => match controller { // 'testing' .cat "123" - Type2::TextValue { + Type2::TextValue(TextValue { value: controller, .. - } => { + }) => { let value = value.trim_start_matches('\'').trim_end_matches('\''); if is_dedent { @@ -207,7 +207,7 @@ pub fn cat_operation<'a>( literals.push(format!("{}{}", value, controller).into()) } } - Type2::Typename { ident, .. } => { + Type2::Typename(Typename { ident, .. }) => { let sl = string_literals_from_ident(cddl, ident); if sl.is_empty() { return Err(format!( @@ -220,9 +220,9 @@ pub fn cat_operation<'a>( } } // 'testing' .cat '123 - Type2::UTF8ByteString { + Type2::UTF8ByteString(Utf8ByteString { value: controller, .. - } => match std::str::from_utf8(controller) { + }) => match std::str::from_utf8(controller) { Ok(controller) => { let value = value.trim_start_matches('\'').trim_end_matches('\''); let controller = controller.trim_start_matches('\'').trim_end_matches('\''); @@ -236,9 +236,9 @@ pub fn cat_operation<'a>( Err(e) => return Err(format!("error parsing byte string: {}", e)), }, // 'testing' .cat h'313233' - Type2::B16ByteString { + Type2::B16ByteString(B16ByteString { value: controller, .. - } => match base16::decode(controller) { + }) => match base16::decode(controller) { Ok(controller) => match String::from_utf8(controller) { Ok(controller) => { let value = value.trim_start_matches('\'').trim_end_matches('\''); @@ -254,9 +254,9 @@ pub fn cat_operation<'a>( Err(e) => return Err(format!("error decoding base16 byte string literal: {}", e)), }, // 'testing' .cat b64'MTIz' - Type2::B64ByteString { + Type2::B64ByteString(B64ByteString { value: controller, .. - } => match base64::decode_config(controller, base64::URL_SAFE) { + }) => match base64::decode_config(controller, base64::URL_SAFE) { Ok(controller) => match String::from_utf8(controller) { Ok(controller) => { let value = value.trim_start_matches('\'').trim_end_matches('\''); @@ -277,7 +277,7 @@ pub fn cat_operation<'a>( } }, // 'testing' .cat ( "123" / "1234" ) - Type2::ParenthesizedType { pt: controller, .. } => { + Type2::ParenthesizedType(ParenthesizedType { pt: controller, .. }) => { for controller in controller.type_choices.iter() { if controller.type1.operator.is_none() { literals.append(&mut cat_operation( @@ -293,11 +293,11 @@ pub fn cat_operation<'a>( }, Err(e) => return Err(format!("error parsing byte string: {}", e)), }, - Type2::B16ByteString { value, .. } => match controller { + Type2::B16ByteString(B16ByteString { value, .. }) => match controller { // h'74657374696E67' .cat "123" - Type2::TextValue { + Type2::TextValue(TextValue { value: controller, .. - } => { + }) => { let controller = if is_dedent { base16::encode_lower(dedent_str(controller).as_bytes()) } else { @@ -316,7 +316,7 @@ pub fn cat_operation<'a>( } } // h'74657374696E67' .cat b - Type2::Typename { ident, .. } => { + Type2::Typename(Typename { ident, .. }) => { let sl = string_literals_from_ident(cddl, ident); if sl.is_empty() { return Err(format!( @@ -329,9 +329,9 @@ pub fn cat_operation<'a>( } } // h'74657374696E67' .cat '123' - Type2::UTF8ByteString { + Type2::UTF8ByteString(Utf8ByteString { value: controller, .. - } => { + }) => { let controller = if is_dedent { base16::encode_lower(&dedent_bytes(controller, true)?) } else { @@ -351,9 +351,9 @@ pub fn cat_operation<'a>( } } // h'74657374696E67' .cat h'313233' - Type2::B16ByteString { + Type2::B16ByteString(B16ByteString { value: controller, .. - } => { + }) => { let concat = if is_dedent { [ &dedent_bytes(value, false)?[..], @@ -370,9 +370,9 @@ pub fn cat_operation<'a>( } } // h'74657374696E67' .cat b64'MTIz' - Type2::B64ByteString { + Type2::B64ByteString(B64ByteString { value: controller, .. - } => match base64::decode_config(controller, base64::URL_SAFE) { + }) => match base64::decode_config(controller, base64::URL_SAFE) { Ok(controller) => { let controller = base16::encode_lower(&controller); let concat = if is_dedent { @@ -393,7 +393,7 @@ pub fn cat_operation<'a>( Err(e) => return Err(format!("controller is invalid base64: {}", e)), }, // h'74657374696E67' .cat ( "123" / "1234" ) - Type2::ParenthesizedType { pt: controller, .. } => { + Type2::ParenthesizedType(ParenthesizedType { pt: controller, .. }) => { for controller in controller.type_choices.iter() { if controller.type1.operator.is_none() { literals.append(&mut cat_operation( @@ -407,11 +407,11 @@ pub fn cat_operation<'a>( } _ => return Err(format!("invalid controller used for {} operation", ctrl)), }, - Type2::B64ByteString { value, .. } => match controller { + Type2::B64ByteString(B64ByteString { value, .. }) => match controller { // b64'dGVzdGluZw==' .cat "123" - Type2::TextValue { + Type2::TextValue(TextValue { value: controller, .. - } => match base64::decode_config(value, base64::URL_SAFE) { + }) => match base64::decode_config(value, base64::URL_SAFE) { Ok(value) => { let concat = if is_dedent { [ @@ -435,7 +435,7 @@ pub fn cat_operation<'a>( Err(e) => return Err(format!("target is invalid base64: {}", e)), }, // b64'dGVzdGluZw==' .cat b - Type2::Typename { ident, .. } => { + Type2::Typename(Typename { ident, .. }) => { let sl = string_literals_from_ident(cddl, ident); if sl.is_empty() { return Err(format!( @@ -448,9 +448,9 @@ pub fn cat_operation<'a>( } } // b64'dGVzdGluZw==' .cat '123' - Type2::UTF8ByteString { + Type2::UTF8ByteString(Utf8ByteString { value: controller, .. - } => match base64::decode_config(value, base64::URL_SAFE) { + }) => match base64::decode_config(value, base64::URL_SAFE) { Ok(value) => { let concat = if is_dedent { [ @@ -474,9 +474,9 @@ pub fn cat_operation<'a>( Err(e) => return Err(format!("target is invalid base64: {}", e)), }, // b64'dGVzdGluZw==' .cat h'313233' - Type2::B16ByteString { + Type2::B16ByteString(B16ByteString { value: controller, .. - } => match base64::decode_config(value, base64::URL_SAFE) { + }) => match base64::decode_config(value, base64::URL_SAFE) { Ok(value) => match base16::decode(controller) { Ok(controller) => { let concat = if is_dedent { @@ -502,9 +502,9 @@ pub fn cat_operation<'a>( Err(e) => return Err(format!("target is invalid base64: {}", e)), }, // b64'dGVzdGluZw==' .cat b64'MTIz' - Type2::B64ByteString { + Type2::B64ByteString(B64ByteString { value: controller, .. - } => match base64::decode_config(value, base64::URL_SAFE) { + }) => match base64::decode_config(value, base64::URL_SAFE) { Ok(value) => match base64::decode_config(controller, base64::URL_SAFE) { Ok(controller) => { let concat = if is_dedent { @@ -530,7 +530,7 @@ pub fn cat_operation<'a>( Err(e) => return Err(format!("target is invalid base64: {}", e)), }, // b64'dGVzdGluZw==' .cat ( "123" / "1234" ) - Type2::ParenthesizedType { pt: controller, .. } => { + Type2::ParenthesizedType(ParenthesizedType { pt: controller, .. }) => { for controller in controller.type_choices.iter() { if controller.type1.operator.is_none() { literals.append(&mut cat_operation( @@ -607,17 +607,17 @@ pub fn plus_operation<'a>( ) -> Result>, String> { let mut values = Vec::new(); match target { - Type2::UintValue { value, .. } => match controller { - Type2::UintValue { + Type2::UintValue(UintValue { value, .. }) => match controller { + Type2::UintValue(UintValue { value: controller, .. - } => values.push((value + controller).into()), - Type2::IntValue { + }) => values.push((value + controller).into()), + Type2::IntValue(IntValue { value: controller, .. - } => values.push(((*value as isize + controller) as usize).into()), - Type2::FloatValue { + }) => values.push(((*value as isize + controller) as usize).into()), + Type2::FloatValue(FloatValue { value: controller, .. - } => values.push(((*value as isize + *controller as isize) as usize).into()), - Type2::Typename { ident, .. } => { + }) => values.push(((*value as isize + *controller as isize) as usize).into()), + Type2::Typename(Typename { ident, .. }) => { let nv = numeric_values_from_ident(cddl, ident); if nv.is_empty() { return Err(format!( @@ -630,7 +630,7 @@ pub fn plus_operation<'a>( values.append(&mut plus_operation(cddl, target, controller)?) } } - Type2::ParenthesizedType { pt: controller, .. } => { + Type2::ParenthesizedType(ParenthesizedType { pt: controller, .. }) => { for controller in controller.type_choices.iter() { match &controller.type1.operator { Some(Operator { @@ -649,17 +649,17 @@ pub fn plus_operation<'a>( } _ => return Err("invalid controller used for .plus operation".to_string()), }, - Type2::IntValue { value, .. } => match controller { - Type2::IntValue { + Type2::IntValue(IntValue { value, .. }) => match controller { + Type2::IntValue(IntValue { value: controller, .. - } => values.push((value + controller).into()), - Type2::UintValue { + }) => values.push((value + controller).into()), + Type2::UintValue(UintValue { value: controller, .. - } => values.push((value + *controller as isize).into()), - Type2::FloatValue { + }) => values.push((value + *controller as isize).into()), + Type2::FloatValue(FloatValue { value: controller, .. - } => values.push((value + *controller as isize).into()), - Type2::Typename { ident, .. } => { + }) => values.push((value + *controller as isize).into()), + Type2::Typename(Typename { ident, .. }) => { let nv = numeric_values_from_ident(cddl, ident); if nv.is_empty() { return Err(format!( @@ -672,7 +672,7 @@ pub fn plus_operation<'a>( values.append(&mut plus_operation(cddl, target, controller)?) } } - Type2::ParenthesizedType { pt: controller, .. } => { + Type2::ParenthesizedType(ParenthesizedType { pt: controller, .. }) => { for controller in controller.type_choices.iter() { match &controller.type1.operator { Some(Operator { @@ -691,14 +691,14 @@ pub fn plus_operation<'a>( } _ => return Err("invalid controller used for .plus operation".to_string()), }, - Type2::FloatValue { value, .. } => match controller { - Type2::IntValue { + Type2::FloatValue(FloatValue { value, .. }) => match controller { + Type2::IntValue(IntValue { value: controller, .. - } => values.push((value + *controller as f64).into()), - Type2::FloatValue { + }) => values.push((value + *controller as f64).into()), + Type2::FloatValue(FloatValue { value: controller, .. - } => values.push((value + controller).into()), - Type2::Typename { ident, .. } => { + }) => values.push((value + controller).into()), + Type2::Typename(Typename { ident, .. }) => { let nv = numeric_values_from_ident(cddl, ident); if nv.is_empty() { return Err(format!( @@ -711,7 +711,7 @@ pub fn plus_operation<'a>( values.append(&mut plus_operation(cddl, target, controller)?) } } - Type2::ParenthesizedType { pt: controller, .. } => { + Type2::ParenthesizedType(ParenthesizedType { pt: controller, .. }) => { for controller in controller.type_choices.iter() { match &controller.type1.operator { Some(Operator { @@ -730,7 +730,7 @@ pub fn plus_operation<'a>( } _ => return Err("invalid controller used for .plus operation".to_string()), }, - Type2::Typename { ident, .. } => { + Type2::Typename(Typename { ident, .. }) => { // Only grab the first type choice value from the target per // https://github.com/cbor-wg/cddl-control/issues/2#issuecomment-729253368 if let Some(value) = numeric_values_from_ident(cddl, ident).first() { @@ -739,7 +739,7 @@ pub fn plus_operation<'a>( return Err("invalid controller used for .plus operation".to_string()); } } - Type2::ParenthesizedType { pt: target, .. } => { + Type2::ParenthesizedType(ParenthesizedType { pt: target, .. }) => { // Only grab the first type choice value from the target per // https://github.com/cbor-wg/cddl-control/issues/2#issuecomment-729253368 if let Some(tc) = target.type_choices.first() { @@ -859,11 +859,10 @@ mod tests { )), false, )?, - vec![Type2::TextValue { + vec![Type2::TextValue(TextValue { value: "foo\n bar\n baz\n".into(), - #[cfg(feature = "ast-span")] - span: Span::default(), - }], + ..Default::default() + })], ); Ok(()) @@ -889,19 +888,16 @@ mod tests { cat_operation( &cddl, &Type2::from("foo".to_string()), - &Type2::Typename { + &Type2::Typename(Typename { ident: "b".into(), - generic_args: None, - #[cfg(feature = "ast-span")] - span: Span::default(), - }, + ..Default::default() + }), true, )?, - vec![Type2::TextValue { + vec![Type2::TextValue(TextValue { value: "foo\nbar\nbaz\n".into(), - #[cfg(feature = "ast-span")] - span: Span::default(), - }] + ..Default::default() + })] ); Ok(()) diff --git a/src/validator/json.rs b/src/validator/json.rs index c4c88700..16ee2afb 100644 --- a/src/validator/json.rs +++ b/src/validator/json.rs @@ -911,8 +911,8 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { } match lower { - Type2::IntValue { value: l, .. } => match upper { - Type2::IntValue { value: u, .. } => { + Type2::IntValue(IntValue { value: l, .. }) => match upper { + Type2::IntValue(IntValue { value: u, .. }) => { let error_str = if is_inclusive { format!( "expected integer to be in range {} <= value <= {}, got {}", @@ -951,7 +951,7 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { } } } - Type2::UintValue { value: u, .. } => { + Type2::UintValue(UintValue { value: u, .. }) => { let error_str = if is_inclusive { format!( "expected integer to be in range {} <= value <= {}, got {}", @@ -998,8 +998,8 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { return Ok(()); } }, - Type2::UintValue { value: l, .. } => match upper { - Type2::UintValue { value: u, .. } => { + Type2::UintValue(UintValue { value: l, .. }) => match upper { + Type2::UintValue(UintValue { value: u, .. }) => { let error_str = if is_inclusive { format!( "expected uint to be in range {} <= value <= {}, got {}", @@ -1072,8 +1072,8 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { return Ok(()); } }, - Type2::FloatValue { value: l, .. } => match upper { - Type2::FloatValue { value: u, .. } => { + Type2::FloatValue(FloatValue { value: l, .. }) => match upper { + Type2::FloatValue(FloatValue { value: u, .. }) => { let error_str = if is_inclusive { format!( "expected float to be in range {} <= value <= {}, got {}", @@ -1139,15 +1139,15 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { ctrl: &Token<'a>, controller: &Type2<'a>, ) -> visitor::Result { - if let Type2::Typename { + if let Type2::Typename(Typename { ident: target_ident, .. - } = target + }) = target { - if let Type2::Typename { + if let Type2::Typename(Typename { ident: controller_ident, .. - } = controller + }) = controller { if let Some(name) = self.eval_generic_rule { if let Some(gr) = self @@ -1194,14 +1194,14 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { match ctrl { Token::EQ => match target { - Type2::Typename { ident, .. } => { + Type2::Typename(Typename { ident, .. }) => { if is_ident_string_data_type(self.cddl, ident) || is_ident_numeric_data_type(self.cddl, ident) { return self.visit_type2(controller); } } - Type2::Array { group, .. } => { + Type2::Array(Array { group, .. }) => { if let Value::Array(_) = &self.json { let mut entry_counts = Vec::new(); for gc in group.group_choices.iter() { @@ -1214,7 +1214,7 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { return Ok(()); } } - Type2::Map { .. } => { + Type2::Map(Map { .. }) => { if let Value::Object(_) = &self.json { self.ctrl = Some(ctrl.clone()); self.is_ctrl_map_equality = true; @@ -1230,7 +1230,7 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { )), }, Token::NE => match target { - Type2::Typename { ident, .. } => { + Type2::Typename(Typename { ident, .. }) => { if is_ident_string_data_type(self.cddl, ident) || is_ident_numeric_data_type(self.cddl, ident) { @@ -1240,7 +1240,7 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { return Ok(()); } } - Type2::Array { .. } => { + Type2::Array(Array { .. }) => { if let Value::Array(_) = &self.json { self.ctrl = Some(ctrl.clone()); self.visit_type2(controller)?; @@ -1248,7 +1248,7 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { return Ok(()); } } - Type2::Map { .. } => { + Type2::Map(Map { .. }) => { if let Value::Object(_) = &self.json { self.ctrl = Some(ctrl.clone()); self.is_ctrl_map_equality = true; @@ -1264,7 +1264,7 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { )), }, Token::LT | Token::GT | Token::GE | Token::LE => match target { - Type2::Typename { ident, .. } if is_ident_numeric_data_type(self.cddl, ident) => { + Type2::Typename(Typename { ident, .. }) if is_ident_numeric_data_type(self.cddl, ident) => { self.ctrl = Some(ctrl.clone()); self.visit_type2(controller)?; self.ctrl = None; @@ -1277,7 +1277,7 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { } }, Token::SIZE => match target { - Type2::Typename { ident, .. } + Type2::Typename(Typename { ident, .. }) if is_ident_string_data_type(self.cddl, ident) || is_ident_uint_data_type(self.cddl, ident) => { @@ -1342,7 +1342,9 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { Token::REGEXP | Token::PCRE => { self.ctrl = Some(ctrl.clone()); match target { - Type2::Typename { ident, .. } if is_ident_string_data_type(self.cddl, ident) => { + Type2::Typename(Typename { ident, .. }) + if is_ident_string_data_type(self.cddl, ident) => + { match self.json { Value::String(_) | Value::Array(_) => self.visit_type2(controller)?, _ => self.add_error(format!( @@ -1443,10 +1445,12 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { self.ctrl = Some(ctrl.clone()); match target { - Type2::Typename { ident, .. } if is_ident_string_data_type(self.cddl, ident) => { + Type2::Typename(Typename { ident, .. }) + if is_ident_string_data_type(self.cddl, ident) => + { match self.json { Value::String(_) | Value::Array(_) => { - if let Type2::ParenthesizedType { pt, .. } = controller { + if let Type2::ParenthesizedType(ParenthesizedType { pt, .. }) = controller { match abnf_from_complex_controller(self.cddl, pt) { Ok(values) => { let error_count = self.errors.len(); @@ -1491,7 +1495,7 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { if let Some(ef) = self.enabled_features { let tv = text_value_from_type2(self.cddl, controller); - if let Some(Type2::TextValue { value, .. }) = tv { + if let Some(Type2::TextValue(TextValue { value, .. })) = tv { if ef.contains(&&**value) { let err_count = self.errors.len(); self.visit_type2(target)?; @@ -1505,7 +1509,7 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { .get_or_insert(vec![value.to_string()]) .push(value.to_string()); } - } else if let Some(Type2::UTF8ByteString { value, .. }) = tv { + } else if let Some(Type2::UTF8ByteString(Utf8ByteString { value, .. })) = tv { let value = std::str::from_utf8(value).map_err(Error::UTF8Parsing)?; if ef.contains(&value) { let err_count = self.errors.len(); @@ -1532,7 +1536,7 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { if let Some(ef) = &self.enabled_features { let tv = text_value_from_type2(self.cddl, controller); - if let Some(Type2::TextValue { value, .. }) = tv { + if let Some(Type2::TextValue(TextValue { value, .. })) = tv { if ef.contains(&JsValue::from(value.as_ref())) { let err_count = self.errors.len(); self.visit_type2(target)?; @@ -1546,7 +1550,7 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { .get_or_insert(vec![value.to_string()]) .push(value.to_string()); } - } else if let Some(Type2::UTF8ByteString { value, .. }) = tv { + } else if let Some(Type2::UTF8ByteString(Utf8ByteString { value, .. })) = tv { let value = std::str::from_utf8(value).map_err(Error::UTF8Parsing)?; if ef.contains(&JsValue::from(value)) { let err_count = self.errors.len(); @@ -1576,8 +1580,10 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { fn visit_type2(&mut self, t2: &Type2<'a>) -> visitor::Result { match t2 { - Type2::TextValue { value, .. } => self.visit_value(&token::Value::TEXT(value.clone())), - Type2::Map { group, .. } => match &self.json { + Type2::TextValue(TextValue { value, .. }) => { + self.visit_value(&token::Value::TEXT(value.clone())) + } + Type2::Map(Map { group, .. }) => match &self.json { Value::Object(o) => { #[allow(clippy::needless_collect)] let o = o.keys().cloned().collect::>(); @@ -1697,7 +1703,7 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { Ok(()) } }, - Type2::Array { group, .. } => match &self.json { + Type2::Array(Array { group, .. }) => match &self.json { Value::Array(a) => { if group.group_choices.len() == 1 && group.group_choices[0].group_entries.is_empty() @@ -1740,11 +1746,11 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { Ok(()) } }, - Type2::ChoiceFromGroup { + Type2::ChoiceFromGroup(ChoiceFromGroup { ident, generic_args, .. - } => { + }) => { if let Some(ga) = generic_args { if let Some(rule) = rule_from_ident(self.cddl, ident) { if let Some(gr) = self @@ -1797,17 +1803,17 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { Ok(()) } - Type2::ChoiceFromInlineGroup { group, .. } => { + Type2::ChoiceFromInlineGroup(ChoiceFromInlineGroup { group, .. }) => { self.is_group_to_choice_enum = true; self.visit_group(group)?; self.is_group_to_choice_enum = false; Ok(()) } - Type2::Typename { + Type2::Typename(Typename { ident, generic_args, .. - } => { + }) => { if let Some(ga) = generic_args { if let Some(rule) = rule_from_ident(self.cddl, ident) { if let Some(gr) = self @@ -1847,19 +1853,21 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { self.visit_identifier(ident) } - Type2::IntValue { value, .. } => self.visit_value(&token::Value::INT(*value)), - Type2::UintValue { value, .. } => self.visit_value(&token::Value::UINT(*value)), - Type2::FloatValue { value, .. } => self.visit_value(&token::Value::FLOAT(*value)), - Type2::ParenthesizedType { pt, .. } => self.visit_type(pt), - Type2::Unwrap { + Type2::IntValue(IntValue { value, .. }) => self.visit_value(&token::Value::INT(*value)), + Type2::UintValue(UintValue { value, .. }) => self.visit_value(&token::Value::UINT(*value)), + Type2::FloatValue(FloatValue { value, .. }) => self.visit_value(&token::Value::FLOAT(*value)), + Type2::ParenthesizedType(ParenthesizedType { pt, .. }) => self.visit_type(pt), + Type2::Unwrap(Unwrap { ident, generic_args, .. - } => { + }) => { // Per // https://github.com/w3c/did-spec-registries/pull/138#issuecomment-719739215, // strip tag and validate underlying type - if let Some(Type2::TaggedData { t, .. }) = tag_from_token(&lookup_ident(ident.ident)) { + if let Some(Type2::TaggedData(TaggedData { t, .. })) = + tag_from_token(&lookup_ident(ident.ident)) + { return self.visit_type(&t); } @@ -2343,7 +2351,7 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { fn visit_memberkey(&mut self, mk: &MemberKey<'a>) -> visitor::Result { match mk { - MemberKey::Type1 { is_cut, .. } => { + MemberKey::Type1(Type1MemberKey { is_cut, .. }) => { self.is_cut_present = *is_cut; walk_memberkey(self, mk)?; self.is_cut_present = false; diff --git a/src/visitor.rs b/src/visitor.rs index be075c3a..c4f344a2 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -138,7 +138,7 @@ pub trait Visitor<'a, E: Error> { } /// Visit nonmemberkey - fn visit_nonmemberkey(&mut self, nmk: &ParenthesizedType<'a>) -> Result { + fn visit_nonmemberkey(&mut self, nmk: &ParenthesizedTypeOrGroup<'a>) -> Result { walk_nonmemberkey(self, nmk) } } @@ -259,47 +259,47 @@ where V: Visitor<'a, E> + ?Sized, { match t2 { - Type2::Array { group, .. } => visitor.visit_group(group), - Type2::Map { group, .. } => visitor.visit_group(group), - Type2::ChoiceFromGroup { + Type2::Array(Array { group, .. }) => visitor.visit_group(group), + Type2::Map(Map { group, .. }) => visitor.visit_group(group), + Type2::ChoiceFromGroup(ChoiceFromGroup { generic_args, ident, .. - } => { + }) => { if let Some(ga) = generic_args { visitor.visit_genericargs(ga)?; } visitor.visit_identifier(ident) } - Type2::ChoiceFromInlineGroup { group, .. } => visitor.visit_group(group), - Type2::TaggedData { t, .. } => visitor.visit_type(t), - Type2::Typename { ident, .. } => visitor.visit_identifier(ident), - Type2::Unwrap { + Type2::ChoiceFromInlineGroup(ChoiceFromInlineGroup { group, .. }) => visitor.visit_group(group), + Type2::TaggedData(TaggedData { t, .. }) => visitor.visit_type(t), + Type2::Typename(Typename { ident, .. }) => visitor.visit_identifier(ident), + Type2::Unwrap(Unwrap { generic_args, ident, .. - } => { + }) => { if let Some(ga) = generic_args { visitor.visit_genericargs(ga)?; } visitor.visit_identifier(ident) } - Type2::ParenthesizedType { pt, .. } => visitor.visit_type(pt), - Type2::B16ByteString { value, .. } => { + Type2::ParenthesizedType(ParenthesizedType { pt, .. }) => visitor.visit_type(pt), + Type2::B16ByteString(B16ByteString { value, .. }) => { visitor.visit_value(&Value::BYTE(ByteValue::B16(value.clone()))) } - Type2::B64ByteString { value, .. } => { + Type2::B64ByteString(B64ByteString { value, .. }) => { visitor.visit_value(&Value::BYTE(ByteValue::B64(value.clone()))) } - Type2::UTF8ByteString { value, .. } => { + Type2::UTF8ByteString(Utf8ByteString { value, .. }) => { visitor.visit_value(&Value::BYTE(ByteValue::UTF8(value.clone()))) } - Type2::FloatValue { value, .. } => visitor.visit_value(&Value::FLOAT(*value)), - Type2::IntValue { value, .. } => visitor.visit_value(&Value::INT(*value)), - Type2::UintValue { value, .. } => visitor.visit_value(&Value::UINT(*value)), - Type2::TextValue { value, .. } => visitor.visit_value(&Value::TEXT(value.clone())), + Type2::FloatValue(FloatValue { value, .. }) => visitor.visit_value(&Value::FLOAT(*value)), + Type2::IntValue(IntValue { value, .. }) => visitor.visit_value(&Value::INT(*value)), + Type2::UintValue(UintValue { value, .. }) => visitor.visit_value(&Value::UINT(*value)), + Type2::TextValue(TextValue { value, .. }) => visitor.visit_value(&Value::TEXT(value.clone())), _ => Ok(()), } } @@ -409,10 +409,10 @@ where V: Visitor<'a, E> + ?Sized, { match mk { - MemberKey::Type1 { t1, .. } => visitor.visit_type1(t1), - MemberKey::Bareword { ident, .. } => visitor.visit_identifier(ident), - MemberKey::Value { value, .. } => visitor.visit_value(value), - MemberKey::ParenthesizedType { non_member_key, .. } => { + MemberKey::Type1(Type1MemberKey { t1, .. }) => visitor.visit_type1(t1), + MemberKey::Bareword(BarewordMemberKey { ident, .. }) => visitor.visit_identifier(ident), + MemberKey::Value(ValueMemberKey { value, .. }) => visitor.visit_value(value), + MemberKey::Parenthesized(ParenthesizedMemberKey { non_member_key, .. }) => { visitor.visit_nonmemberkey(non_member_key) } } @@ -441,13 +441,13 @@ where } /// Walk nonmemberkey -pub fn walk_nonmemberkey<'a, E, V>(visitor: &mut V, nmk: &ParenthesizedType<'a>) -> Result +pub fn walk_nonmemberkey<'a, E, V>(visitor: &mut V, nmk: &ParenthesizedTypeOrGroup<'a>) -> Result where E: Error, V: Visitor<'a, E> + ?Sized, { match nmk { - ParenthesizedType::Group(group) => visitor.visit_group(group), - ParenthesizedType::Type(t) => visitor.visit_type(t), + ParenthesizedTypeOrGroup::Group(group) => visitor.visit_group(group), + ParenthesizedTypeOrGroup::Type(t) => visitor.visit_type(&t.pt), } } diff --git a/tests/parser.rs b/tests/parser.rs index 4bb852a6..5da421f3 100644 --- a/tests/parser.rs +++ b/tests/parser.rs @@ -47,7 +47,7 @@ fn verify_cddl() -> Result<()> { value: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "secondrule".into(), socket: None, @@ -55,19 +55,16 @@ fn verify_cddl() -> Result<()> { }, generic_args: None, span: Span(9, 19, 1), - }, - operator: None, - comments_after_type: None, + }), span: Span(9, 19, 1), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(9, 19, 1), }, - comments_before_assignt: None, - comments_after_assignt: None, + ..Default::default() }, comments_after_rule: None, span: Span(0, 19, 1), @@ -79,21 +76,19 @@ fn verify_cddl() -> Result<()> { socket: None, span: Span(20, 27, 2), }, - generic_params: None, - is_type_choice_alternate: false, value: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::UintValue { + type2: Type2::UintValue(UintValue { value: 10, span: Span(30, 32, 2), - }, + }), operator: Some(Operator { operator: RangeCtlOp::RangeOp { is_inclusive: true, span: Span(32, 34, 2), }, - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "upper".into(), socket: None, @@ -101,7 +96,7 @@ fn verify_cddl() -> Result<()> { }, generic_args: None, span: Span(34, 39, 2), - }, + }), comments_before_operator: None, comments_after_operator: None, }), @@ -113,8 +108,7 @@ fn verify_cddl() -> Result<()> { }], span: Span(30, 39, 2), }, - comments_before_assignt: None, - comments_after_assignt: None, + ..Default::default() }, comments_after_rule: None, span: Span(20, 39, 2), @@ -132,35 +126,30 @@ fn verify_cddl() -> Result<()> { type_choices: vec![ TypeChoice { type1: Type1 { - type2: Type2::UintValue { + type2: Type2::UintValue(UintValue { value: 500, span: Span(48, 51, 3), - }, - operator: None, - comments_after_type: None, + }), span: Span(48, 51, 3), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }, TypeChoice { type1: Type1 { - type2: Type2::UintValue { + type2: Type2::UintValue(UintValue { value: 600, span: Span(54, 57, 3), - }, - operator: None, - comments_after_type: None, + }), span: Span(54, 57, 3), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }, ], span: Span(48, 57, 3), }, - comments_before_assignt: None, - comments_after_assignt: None, + ..Default::default() }, comments_after_rule: None, span: Span(40, 57, 3), @@ -234,7 +223,7 @@ fn verify_cddl() -> Result<()> { value: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "message".into(), socket: None, @@ -244,50 +233,42 @@ fn verify_cddl() -> Result<()> { args: vec![ GenericArg { arg: Box::from(Type1 { - type2: Type2::TextValue { + type2: Type2::TextValue(TextValue { value: "reboot".into(), span: Span(94, 102, 5), - }, - operator: None, - comments_after_type: None, + }), span: Span(94, 102, 5), + ..Default::default() }), - comments_before_type: None, - comments_after_type: None, + ..Default::default() }, GenericArg { arg: Box::from(Type1 { - type2: Type2::TextValue { + type2: Type2::TextValue(TextValue { value: "now".into(), span: Span(104, 109, 5), - }, - operator: None, - comments_after_type: None, + }), span: Span(104, 109, 5), + ..Default::default() }), - comments_before_type: None, - comments_after_type: None, + ..Default::default() }, ], - span: Span(93, 110, 5), }), span: Span(86, 110, 5), - }, - operator: None, - comments_after_type: None, + }), span: Span(86, 110, 5), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(86, 110, 5), }, - comments_before_assignt: None, - comments_after_assignt: None, + ..Default::default() }, - comments_after_rule: None, span: Span(75, 110, 5), + comments_after_rule: None, }, Rule::Type { rule: TypeRule { @@ -304,8 +285,7 @@ fn verify_cddl() -> Result<()> { socket: None, span: Span(119, 120, 6), }, - comments_before_ident: None, - comments_after_ident: None, + ..Default::default() }, GenericParam { param: Identifier { @@ -313,8 +293,7 @@ fn verify_cddl() -> Result<()> { socket: None, span: Span(122, 123, 6), }, - comments_before_ident: None, - comments_after_ident: None, + ..Default::default() }, ], span: Span(118, 124, 6), @@ -323,7 +302,7 @@ fn verify_cddl() -> Result<()> { value: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::Map { + type2: Type2::Map(Map { group: Group { group_choices: vec![GroupChoice { group_entries: vec![ @@ -331,29 +310,26 @@ fn verify_cddl() -> Result<()> { GroupEntry::ValueMemberKey { ge: Box::from(ValueMemberKeyEntry { occur: None, - member_key: Some(MemberKey::Bareword { + member_key: Some(MemberKey::Bareword(BarewordMemberKey { ident: Identifier { ident: "type".into(), socket: None, span: Span(128, 132, 6), }, - comments: None, - comments_after_colon: None, span: Span(128, 133, 6), - }), + ..Default::default() + })), entry_type: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::UintValue { + type2: Type2::UintValue(UintValue { value: 2, span: Span(134, 135, 6), - }, - operator: None, - comments_after_type: None, + }), span: Span(134, 135, 6), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(134, 135, 6), }, @@ -372,20 +348,19 @@ fn verify_cddl() -> Result<()> { GroupEntry::ValueMemberKey { ge: Box::from(ValueMemberKeyEntry { occur: None, - member_key: Some(MemberKey::Bareword { + member_key: Some(MemberKey::Bareword(BarewordMemberKey { ident: Identifier { ident: "value".into(), socket: None, span: Span(137, 142, 6), }, - comments: None, - comments_after_colon: None, span: Span(137, 143, 6), - }), + ..Default::default() + })), entry_type: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "v".into(), socket: None, @@ -393,13 +368,11 @@ fn verify_cddl() -> Result<()> { }, generic_args: None, span: Span(144, 145, 6), - }, - operator: None, - comments_after_type: None, + }), span: Span(144, 145, 6), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(144, 145, 6), }, @@ -420,21 +393,17 @@ fn verify_cddl() -> Result<()> { }], span: Span(128, 145, 6), }, - comments_before_group: None, - comments_after_group: None, span: Span(127, 146, 6), - }, - operator: None, - comments_after_type: None, + ..Default::default() + }), span: Span(127, 146, 6), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(127, 146, 6), }, - comments_before_assignt: None, - comments_after_assignt: None, + ..Default::default() }, comments_after_rule: None, span: Span(111, 146, 6), @@ -451,27 +420,23 @@ fn verify_cddl() -> Result<()> { value: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::ChoiceFromGroup { + type2: Type2::ChoiceFromGroup(ChoiceFromGroup { ident: Identifier { ident: "colors".into(), socket: None, span: Span(156, 162, 7), }, - generic_args: None, - comments: None, span: Span(155, 162, 7), - }, - operator: None, - comments_after_type: None, + ..Default::default() + }), span: Span(155, 162, 7), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(155, 162, 7), }, - comments_before_assignt: None, - comments_after_assignt: None, + ..Default::default() }, comments_after_rule: None, span: Span(147, 162, 7), @@ -493,29 +458,26 @@ fn verify_cddl() -> Result<()> { GroupEntry::ValueMemberKey { ge: Box::from(ValueMemberKeyEntry { occur: None, - member_key: Some(MemberKey::Bareword { + member_key: Some(MemberKey::Bareword(BarewordMemberKey { ident: Identifier { ident: "red".into(), socket: None, span: Span(174, 177, 8), }, - comments: None, - comments_after_colon: None, span: Span(174, 178, 8), - }), + ..Default::default() + })), entry_type: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::TextValue { + type2: Type2::TextValue(TextValue { value: "red".into(), span: Span(179, 184, 8), - }, - operator: None, - comments_after_type: None, + }), span: Span(179, 184, 8), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(179, 184, 8), }, @@ -557,12 +519,12 @@ fn verify_cddl() -> Result<()> { value: Type { type_choices: vec![TypeChoice { type1: Type1 { - type2: Type2::ParenthesizedType { + type2: Type2::ParenthesizedType(ParenthesizedType { pt: Type { type_choices: vec![ TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "int".into(), socket: None, @@ -570,17 +532,15 @@ fn verify_cddl() -> Result<()> { }, generic_args: None, span: Span(196, 199, 9), - }, - operator: None, - comments_after_type: None, + }), span: Span(196, 199, 9), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }, TypeChoice { type1: Type1 { - type2: Type2::Typename { + type2: Type2::Typename(Typename { ident: Identifier { ident: "float".into(), socket: None, @@ -588,27 +548,22 @@ fn verify_cddl() -> Result<()> { }, generic_args: None, span: Span(202, 207, 9), - }, - operator: None, - comments_after_type: None, + }), span: Span(202, 207, 9), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }, ], span: Span(196, 207, 9), }, - comments_before_type: None, - comments_after_type: None, span: Span(194, 209, 9), - }, - operator: None, - comments_after_type: None, + ..Default::default() + }), span: Span(194, 210, 9), + ..Default::default() }, - comments_before_type: None, - comments_after_type: None, + ..Default::default() }], span: Span(194, 210, 9),