1
1
extern crate nom;
2
2
use 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 ,
5
7
} ;
6
8
7
9
#[ derive( Default , Debug , PartialEq ) ]
8
10
pub struct RuleMetadata {
9
- pub name : Option < String > ,
10
- pub is_newtype : bool ,
11
+ pub name : Option < String > ,
12
+ pub is_newtype : bool ,
11
13
}
12
14
13
15
fn merge_metadata ( r1 : & RuleMetadata , r2 : & RuleMetadata ) -> RuleMetadata {
14
16
RuleMetadata {
15
17
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 ( ) ,
19
23
} ,
20
- is_newtype : r1. is_newtype || r2. is_newtype
24
+ is_newtype : r1. is_newtype || r2. is_newtype ,
21
25
}
22
26
}
23
27
24
28
enum ParseResult {
25
- NewType ,
26
- Name ( String )
29
+ NewType ,
30
+ Name ( String ) ,
27
31
}
28
32
29
33
impl 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
42
52
}
43
- base
44
- }
45
53
}
46
54
47
55
fn 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) ?;
51
59
52
- Ok ( ( input, ParseResult :: Name ( name. to_string ( ) ) ) )
60
+ Ok ( ( input, ParseResult :: Name ( name. to_string ( ) ) ) )
53
61
}
54
62
fn tag_newtype ( input : & str ) -> IResult < & str , ParseResult > {
55
- let ( input, _) = tag ( "@newtype" ) ( input) ?;
63
+ let ( input, _) = tag ( "@newtype" ) ( input) ?;
56
64
57
- Ok ( ( input, ParseResult :: NewType ) )
65
+ Ok ( ( input, ParseResult :: NewType ) )
58
66
}
59
67
fn 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) ?;
62
70
63
- Ok ( ( input, result) )
71
+ Ok ( ( input, result) )
64
72
}
65
73
66
74
fn 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) ?;
69
76
70
- Ok ( ( input, RuleMetadata :: from_parse_results ( & parse_results) ) )
77
+ Ok ( ( input, RuleMetadata :: from_parse_results ( & parse_results) ) )
71
78
}
72
79
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
+ }
79
86
}
80
- }
81
87
}
82
88
83
89
pub fn metadata_from_comments ( comments : & [ & str ] ) -> RuleMetadata {
@@ -86,38 +92,62 @@ pub fn metadata_from_comments(comments: &[&str]) -> RuleMetadata {
86
92
if let Ok ( comment_metadata) = rule_metadata ( comment) {
87
93
result = merge_metadata ( & result, & comment_metadata. 1 ) ;
88
94
}
89
- } ;
95
+ }
90
96
result
91
97
}
92
98
93
99
#[ test]
94
100
fn 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
+ ) ;
99
111
}
100
112
101
113
#[ test]
102
114
fn 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
+ ) ;
107
125
}
108
126
109
127
#[ test]
110
128
fn 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
+ ) ;
115
139
}
116
140
117
141
#[ test]
118
142
fn 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