11extern crate nom;
22use nom:: {
3- IResult ,
4- bytes:: complete:: { tag, take_while1, take_while} , branch:: alt, multi:: many0,
3+ branch:: alt,
4+ bytes:: complete:: { tag, take_while, take_while1} ,
5+ multi:: many0,
6+ IResult ,
57} ;
68
79#[ derive( Default , Debug , PartialEq ) ]
810pub struct RuleMetadata {
9- pub name : Option < String > ,
10- pub is_newtype : bool ,
11+ pub name : Option < String > ,
12+ pub is_newtype : bool ,
1113}
1214
1315fn merge_metadata ( r1 : & RuleMetadata , r2 : & RuleMetadata ) -> RuleMetadata {
1416 RuleMetadata {
1517 name : match ( r1. name . as_ref ( ) , r2. name . as_ref ( ) ) {
16- ( Some ( val1) , Some ( val2) ) => panic ! ( "Key \" name\" specified twice: {:?} {:?}" , val1, val2) ,
17- ( val@Some ( _) , _) => val. cloned ( ) ,
18- ( _, val) => val. cloned ( )
18+ ( Some ( val1) , Some ( val2) ) => {
19+ panic ! ( "Key \" name\" specified twice: {:?} {:?}" , val1, val2)
20+ }
21+ ( val @ Some ( _) , _) => val. cloned ( ) ,
22+ ( _, val) => val. cloned ( ) ,
1923 } ,
20- is_newtype : r1. is_newtype || r2. is_newtype
24+ is_newtype : r1. is_newtype || r2. is_newtype ,
2125 }
2226}
2327
2428enum ParseResult {
25- NewType ,
26- Name ( String )
29+ NewType ,
30+ Name ( String ) ,
2731}
2832
2933impl RuleMetadata {
30- fn from_parse_results ( results : & [ ParseResult ] ) -> RuleMetadata {
31- let mut base = RuleMetadata :: default ( ) ;
32- for result in results {
33- match result {
34- ParseResult :: Name ( name) => {
35- match base. name . as_ref ( ) {
36- Some ( old_name) => panic ! ( "Key \" name\" specified twice: {:?} {:?}" , old_name, name) ,
37- None => { base. name = Some ( name. to_string ( ) ) ; }
38- }
39- } ,
40- ParseResult :: NewType => { base. is_newtype = true ; }
41- }
34+ fn from_parse_results ( results : & [ ParseResult ] ) -> RuleMetadata {
35+ let mut base = RuleMetadata :: default ( ) ;
36+ for result in results {
37+ match result {
38+ ParseResult :: Name ( name) => match base. name . as_ref ( ) {
39+ Some ( old_name) => {
40+ panic ! ( "Key \" name\" specified twice: {:?} {:?}" , old_name, name)
41+ }
42+ None => {
43+ base. name = Some ( name. to_string ( ) ) ;
44+ }
45+ } ,
46+ ParseResult :: NewType => {
47+ base. is_newtype = true ;
48+ }
49+ }
50+ }
51+ base
4252 }
43- base
44- }
4553}
4654
4755fn tag_name ( input : & str ) -> IResult < & str , ParseResult > {
48- let ( input, _) = tag ( "@name" ) ( input) ?;
49- let ( input, _) = take_while ( char:: is_whitespace) ( input) ?;
50- let ( input, name) = take_while1 ( |ch| !char:: is_whitespace ( ch) ) ( input) ?;
56+ let ( input, _) = tag ( "@name" ) ( input) ?;
57+ let ( input, _) = take_while ( char:: is_whitespace) ( input) ?;
58+ let ( input, name) = take_while1 ( |ch| !char:: is_whitespace ( ch) ) ( input) ?;
5159
52- Ok ( ( input, ParseResult :: Name ( name. to_string ( ) ) ) )
60+ Ok ( ( input, ParseResult :: Name ( name. to_string ( ) ) ) )
5361}
5462fn tag_newtype ( input : & str ) -> IResult < & str , ParseResult > {
55- let ( input, _) = tag ( "@newtype" ) ( input) ?;
63+ let ( input, _) = tag ( "@newtype" ) ( input) ?;
5664
57- Ok ( ( input, ParseResult :: NewType ) )
65+ Ok ( ( input, ParseResult :: NewType ) )
5866}
5967fn whitespace_then_tag ( input : & str ) -> IResult < & str , ParseResult > {
60- let ( input, _) = take_while ( char:: is_whitespace) ( input) ?;
61- let ( input, result) = alt ( ( tag_name, tag_newtype) ) ( input) ?;
68+ let ( input, _) = take_while ( char:: is_whitespace) ( input) ?;
69+ let ( input, result) = alt ( ( tag_name, tag_newtype) ) ( input) ?;
6270
63- Ok ( ( input, result) )
71+ Ok ( ( input, result) )
6472}
6573
6674fn rule_metadata ( input : & str ) -> IResult < & str , RuleMetadata > {
67- let ( input, parse_results) = many0 ( whitespace_then_tag) ( input) ?;
68-
75+ let ( input, parse_results) = many0 ( whitespace_then_tag) ( input) ?;
6976
70- Ok ( ( input, RuleMetadata :: from_parse_results ( & parse_results) ) )
77+ Ok ( ( input, RuleMetadata :: from_parse_results ( & parse_results) ) )
7178}
7279
73-
74- impl < ' a > From < Option < & ' a cddl:: ast:: Comments < ' a > > > for RuleMetadata {
75- fn from ( comments : Option < & ' a cddl :: ast :: Comments < ' a > > ) -> RuleMetadata {
76- match comments {
77- None => RuleMetadata :: default ( ) ,
78- Some ( c ) => metadata_from_comments ( & c . 0 )
80+ impl < ' a > From < Option < & ' a cddl :: ast :: Comments < ' a > > > for RuleMetadata {
81+ fn from ( comments : Option < & ' a cddl:: ast:: Comments < ' a > > ) -> RuleMetadata {
82+ match comments {
83+ None => RuleMetadata :: default ( ) ,
84+ Some ( c ) => metadata_from_comments ( & c . 0 ) ,
85+ }
7986 }
80- }
8187}
8288
8389pub fn metadata_from_comments ( comments : & [ & str ] ) -> RuleMetadata {
@@ -86,38 +92,62 @@ pub fn metadata_from_comments(comments: &[&str]) -> RuleMetadata {
8692 if let Ok ( comment_metadata) = rule_metadata ( comment) {
8793 result = merge_metadata ( & result, & comment_metadata. 1 ) ;
8894 }
89- } ;
95+ }
9096 result
9197}
9298
9399#[ test]
94100fn parse_comment_name ( ) {
95- assert_eq ! ( rule_metadata( "@name foo" ) , Ok ( ( "" , RuleMetadata {
96- name: Some ( "foo" . to_string( ) ) ,
97- is_newtype: false ,
98- } ) ) ) ;
101+ assert_eq ! (
102+ rule_metadata( "@name foo" ) ,
103+ Ok ( (
104+ "" ,
105+ RuleMetadata {
106+ name: Some ( "foo" . to_string( ) ) ,
107+ is_newtype: false ,
108+ }
109+ ) )
110+ ) ;
99111}
100112
101113#[ test]
102114fn parse_comment_newtype ( ) {
103- assert_eq ! ( rule_metadata( "@newtype" ) , Ok ( ( "" , RuleMetadata {
104- name: None ,
105- is_newtype: true
106- } ) ) ) ;
115+ assert_eq ! (
116+ rule_metadata( "@newtype" ) ,
117+ Ok ( (
118+ "" ,
119+ RuleMetadata {
120+ name: None ,
121+ is_newtype: true
122+ }
123+ ) )
124+ ) ;
107125}
108126
109127#[ test]
110128fn parse_comment_newtype_and_name ( ) {
111- assert_eq ! ( rule_metadata( "@newtype @name foo" ) , Ok ( ( "" , RuleMetadata {
112- name: Some ( "foo" . to_string( ) ) ,
113- is_newtype: true
114- } ) ) ) ;
129+ assert_eq ! (
130+ rule_metadata( "@newtype @name foo" ) ,
131+ Ok ( (
132+ "" ,
133+ RuleMetadata {
134+ name: Some ( "foo" . to_string( ) ) ,
135+ is_newtype: true
136+ }
137+ ) )
138+ ) ;
115139}
116140
117141#[ test]
118142fn parse_comment_newtype_and_name_inverse ( ) {
119- assert_eq ! ( rule_metadata( "@name foo @newtype" ) , Ok ( ( "" , RuleMetadata {
120- name: Some ( "foo" . to_string( ) ) ,
121- is_newtype: true
122- } ) ) ) ;
123- }
143+ assert_eq ! (
144+ rule_metadata( "@name foo @newtype" ) ,
145+ Ok ( (
146+ "" ,
147+ RuleMetadata {
148+ name: Some ( "foo" . to_string( ) ) ,
149+ is_newtype: true
150+ }
151+ ) )
152+ ) ;
153+ }
0 commit comments