11extern crate nom;
22use nom:: {
3+ branch:: alt,
4+ bytes:: complete:: { tag, take_while, take_while1} ,
5+ multi:: many0,
36 IResult ,
4- bytes:: complete:: { tag, take_while1, take_while} , branch:: alt, multi:: many0,
57} ;
68
79#[ derive( Default , Debug , PartialEq ) ]
@@ -14,9 +16,11 @@ pub struct RuleMetadata {
1416pub fn merge_metadata ( r1 : & RuleMetadata , r2 : & RuleMetadata ) -> RuleMetadata {
1517 let merged = RuleMetadata {
1618 name : match ( r1. name . as_ref ( ) , r2. name . as_ref ( ) ) {
17- ( Some ( val1) , Some ( val2) ) => panic ! ( "Key \" name\" specified twice: {:?} {:?}" , val1, val2) ,
18- ( val@Some ( _) , _) => val. cloned ( ) ,
19- ( _, val) => val. cloned ( )
19+ ( Some ( val1) , Some ( val2) ) => {
20+ panic ! ( "Key \" name\" specified twice: {:?} {:?}" , val1, val2)
21+ }
22+ ( val @ Some ( _) , _) => val. cloned ( ) ,
23+ ( _, val) => val. cloned ( ) ,
2024 } ,
2125 is_newtype : r1. is_newtype || r2. is_newtype ,
2226 no_alias : r1. no_alias || r2. no_alias ,
@@ -36,14 +40,20 @@ impl RuleMetadata {
3640 let mut base = RuleMetadata :: default ( ) ;
3741 for result in results {
3842 match result {
39- ParseResult :: Name ( name) => {
40- match base. name . as_ref ( ) {
41- Some ( old_name) => panic ! ( "Key \" name\" specified twice: {:?} {:?}" , old_name, name) ,
42- None => { base. name = Some ( name. to_string ( ) ) ; }
43+ ParseResult :: Name ( name) => match base. name . as_ref ( ) {
44+ Some ( old_name) => {
45+ panic ! ( "Key \" name\" specified twice: {:?} {:?}" , old_name, name)
46+ }
47+ None => {
48+ base. name = Some ( name. to_string ( ) ) ;
4349 }
4450 } ,
45- ParseResult :: NewType => { base. is_newtype = true ; } ,
46- ParseResult :: DontGenAlias => { base. no_alias = true ; } ,
51+ ParseResult :: NewType => {
52+ base. is_newtype = true ;
53+ }
54+ ParseResult :: DontGenAlias => {
55+ base. no_alias = true ;
56+ }
4757 }
4858 }
4959 base. verify ( ) ;
@@ -87,17 +97,15 @@ fn whitespace_then_tag(input: &str) -> IResult<&str, ParseResult> {
8797
8898fn rule_metadata ( input : & str ) -> IResult < & str , RuleMetadata > {
8999 let ( input, parse_results) = many0 ( whitespace_then_tag) ( input) ?;
90-
91100
92101 Ok ( ( input, RuleMetadata :: from_parse_results ( & parse_results) ) )
93102}
94103
95-
96- impl < ' a > From < Option < & ' a cddl:: ast:: Comments < ' a > > > for RuleMetadata {
104+ impl < ' a > From < Option < & ' a cddl:: ast:: Comments < ' a > > > for RuleMetadata {
97105 fn from ( comments : Option < & ' a cddl:: ast:: Comments < ' a > > ) -> RuleMetadata {
98106 match comments {
99107 None => RuleMetadata :: default ( ) ,
100- Some ( c) => metadata_from_comments ( & c. 0 )
108+ Some ( c) => metadata_from_comments ( & c. 0 ) ,
101109 }
102110 }
103111}
@@ -108,53 +116,83 @@ pub fn metadata_from_comments(comments: &[&str]) -> RuleMetadata {
108116 if let Ok ( comment_metadata) = rule_metadata ( comment) {
109117 result = merge_metadata ( & result, & comment_metadata. 1 ) ;
110118 }
111- } ;
119+ }
112120 result
113121}
114122
115123#[ test]
116124fn parse_comment_name ( ) {
117- assert_eq ! ( rule_metadata( "@name foo" ) , Ok ( ( "" , RuleMetadata {
118- name: Some ( "foo" . to_string( ) ) ,
119- is_newtype: false ,
120- no_alias: false ,
121- } ) ) ) ;
125+ assert_eq ! (
126+ rule_metadata( "@name foo" ) ,
127+ Ok ( (
128+ "" ,
129+ RuleMetadata {
130+ name: Some ( "foo" . to_string( ) ) ,
131+ is_newtype: false ,
132+ no_alias: false ,
133+ }
134+ ) )
135+ ) ;
122136}
123137
124138#[ test]
125139fn parse_comment_newtype ( ) {
126- assert_eq ! ( rule_metadata( "@newtype" ) , Ok ( ( "" , RuleMetadata {
127- name: None ,
128- is_newtype: true ,
129- no_alias: false ,
130- } ) ) ) ;
140+ assert_eq ! (
141+ rule_metadata( "@newtype" ) ,
142+ Ok ( (
143+ "" ,
144+ RuleMetadata {
145+ name: None ,
146+ is_newtype: true ,
147+ no_alias: false ,
148+ }
149+ ) )
150+ ) ;
131151}
132152
133153#[ test]
134154fn parse_comment_newtype_and_name ( ) {
135- assert_eq ! ( rule_metadata( "@newtype @name foo" ) , Ok ( ( "" , RuleMetadata {
136- name: Some ( "foo" . to_string( ) ) ,
137- is_newtype: true ,
138- no_alias: false ,
139- } ) ) ) ;
155+ assert_eq ! (
156+ rule_metadata( "@newtype @name foo" ) ,
157+ Ok ( (
158+ "" ,
159+ RuleMetadata {
160+ name: Some ( "foo" . to_string( ) ) ,
161+ is_newtype: true ,
162+ no_alias: false ,
163+ }
164+ ) )
165+ ) ;
140166}
141167
142168#[ test]
143169fn parse_comment_newtype_and_name_inverse ( ) {
144- assert_eq ! ( rule_metadata( "@name foo @newtype" ) , Ok ( ( "" , RuleMetadata {
145- name: Some ( "foo" . to_string( ) ) ,
146- is_newtype: true ,
147- no_alias: false ,
148- } ) ) ) ;
170+ assert_eq ! (
171+ rule_metadata( "@name foo @newtype" ) ,
172+ Ok ( (
173+ "" ,
174+ RuleMetadata {
175+ name: Some ( "foo" . to_string( ) ) ,
176+ is_newtype: true ,
177+ no_alias: false ,
178+ }
179+ ) )
180+ ) ;
149181}
150182
151183#[ test]
152184fn parse_comment_name_noalias ( ) {
153- assert_eq ! ( rule_metadata( "@no_alias @name foo" ) , Ok ( ( "" , RuleMetadata {
154- name: Some ( "foo" . to_string( ) ) ,
155- is_newtype: false ,
156- no_alias: true ,
157- } ) ) ) ;
185+ assert_eq ! (
186+ rule_metadata( "@no_alias @name foo" ) ,
187+ Ok ( (
188+ "" ,
189+ RuleMetadata {
190+ name: Some ( "foo" . to_string( ) ) ,
191+ is_newtype: false ,
192+ no_alias: true ,
193+ }
194+ ) )
195+ ) ;
158196}
159197
160198#[ test]
0 commit comments