1
1
use nom:: {
2
- bytes:: complete:: { take_until , tag } ,
2
+ bytes:: complete:: { tag , take_until } ,
3
3
character:: complete:: { newline, not_line_ending} ,
4
4
combinator:: opt,
5
5
multi:: many0,
@@ -22,14 +22,17 @@ fn parse_todo<'a>(todo_tag: &'a str, input: &'a str) -> IResult<&'a str, Todo> {
22
22
// discard optional line ending
23
23
let ( input, _) = opt ( newline) ( input) ?;
24
24
25
- Ok ( ( input, Todo {
26
- tag : todo_tag. to_owned ( ) ,
27
- text : text. to_owned ( ) ,
28
- } ) )
25
+ Ok ( (
26
+ input,
27
+ Todo {
28
+ tag : todo_tag. to_owned ( ) ,
29
+ text : text. to_owned ( ) ,
30
+ } ,
31
+ ) )
29
32
}
30
33
31
34
fn parse_todos ( file_content : & str ) -> IResult < & str , Vec < Todo > > {
32
- let line_parser = | file_content | parse_todo ( "TODO:" , file_content) ;
35
+ let line_parser = |file_content| parse_todo ( "TODO:" , file_content) ;
33
36
let ( input, todos) = many0 ( line_parser) ( file_content) ?;
34
37
Ok ( ( input, todos) )
35
38
}
@@ -41,86 +44,85 @@ pub fn parse_file(file_content: &str) -> Vec<Todo> {
41
44
}
42
45
}
43
46
44
-
45
47
#[ cfg( test) ]
46
48
mod test {
47
- use crate :: commands:: parser:: { parse_todos , Todo , parse_todo } ;
49
+ use crate :: commands:: parser:: { parse_todo , parse_todos , Todo } ;
48
50
49
51
#[ test]
50
52
fn todos ( ) {
51
- let input =
52
- "line 1\n \
53
+ let input = "line 1\n \
53
54
TODO: todo1\n \
54
55
line 2\n \
55
56
line 3 -- TODO: todo2\n ";
56
57
57
- assert_eq ! ( parse_todos( input) ,
58
- Ok ( ( "" ,
59
- vec![
60
- Todo { tag: "TODO:" . to_owned( ) ,
61
- text: " todo1" . to_owned( ) } ,
62
- Todo { tag: "TODO:" . to_owned( ) ,
63
- text: " todo2" . to_owned( ) } ,
64
- ] )
65
- )
58
+ assert_eq ! (
59
+ parse_todos( input) ,
60
+ Ok ( (
61
+ "" ,
62
+ vec![
63
+ Todo {
64
+ tag: "TODO:" . to_owned( ) ,
65
+ text: " todo1" . to_owned( )
66
+ } ,
67
+ Todo {
68
+ tag: "TODO:" . to_owned( ) ,
69
+ text: " todo2" . to_owned( )
70
+ } ,
71
+ ]
72
+ ) )
66
73
) ;
67
74
68
75
let input = "line 1\n " ;
69
- assert_eq ! ( parse_todos( input) ,
70
- Ok ( ( "line 1\n " ,
71
- vec![ ] )
72
- )
73
- ) ;
76
+ assert_eq ! ( parse_todos( input) , Ok ( ( "line 1\n " , vec![ ] ) ) ) ;
74
77
75
78
let input = "line1 TODO: todo1\n " ;
76
- assert_eq ! ( parse_todos( input) ,
77
- Ok ( ( "" ,
78
- vec![
79
- Todo { tag: "TODO:" . to_owned( ) ,
80
- text: " todo1" . to_owned( ) } ] )
81
- )
79
+ assert_eq ! (
80
+ parse_todos( input) ,
81
+ Ok ( (
82
+ "" ,
83
+ vec![ Todo {
84
+ tag: "TODO:" . to_owned( ) ,
85
+ text: " todo1" . to_owned( )
86
+ } ]
87
+ ) )
82
88
) ;
83
89
84
- let input =
85
- "line1\n \
90
+ let input = "line1\n \
86
91
line2\n \
87
92
line3\n \
88
93
TODO: todo1\n \
89
94
line4\n ";
90
- assert_eq ! ( parse_todos( input) ,
91
- Ok ( ( "line4\n " ,
92
- vec![
93
- Todo { tag: "TODO:" . to_owned( ) ,
94
- text: " todo1" . to_owned( ) } ] )
95
- )
95
+ assert_eq ! (
96
+ parse_todos( input) ,
97
+ Ok ( (
98
+ "line4\n " ,
99
+ vec![ Todo {
100
+ tag: "TODO:" . to_owned( ) ,
101
+ text: " todo1" . to_owned( )
102
+ } ]
103
+ ) )
96
104
) ;
97
105
98
- let input =
99
- "line1\n \
106
+ let input = "line1\n \
100
107
line2\n \
101
108
line3\n \
102
109
FIXME: todo1\n \
103
110
line4\n ";
104
- assert_eq ! ( parse_todos( input) ,
105
- Ok ( ( input,
106
- vec![ ] )
107
- )
108
- ) ;
109
-
111
+ assert_eq ! ( parse_todos( input) , Ok ( ( input, vec![ ] ) ) ) ;
110
112
}
111
113
112
-
113
114
#[ test]
114
115
fn todo_clean ( ) {
115
116
let input = "TODO: test todo" ;
116
117
assert_eq ! (
117
118
parse_todo( "TODO:" , input) ,
118
- Ok ( ( "" ,
119
+ Ok ( (
120
+ "" ,
119
121
Todo {
120
122
tag: "TODO:" . to_owned( ) ,
121
123
text: " test todo" . to_owned( )
122
- } )
123
- )
124
+ }
125
+ ) )
124
126
) ;
125
127
}
126
128
@@ -129,34 +131,37 @@ mod test {
129
131
let input = "TODO:\n line1" ;
130
132
assert_eq ! (
131
133
parse_todo( "TODO:" , input) ,
132
- Ok ( ( "line1" ,
134
+ Ok ( (
135
+ "line1" ,
133
136
Todo {
134
137
tag: "TODO:" . to_owned( ) ,
135
138
text: "" . to_owned( )
136
- } )
137
- )
139
+ }
140
+ ) )
138
141
) ;
139
142
140
143
let input = "TODO:" ;
141
144
assert_eq ! (
142
145
parse_todo( "TODO:" , input) ,
143
- Ok ( ( "" ,
146
+ Ok ( (
147
+ "" ,
144
148
Todo {
145
149
tag: "TODO:" . to_owned( ) ,
146
150
text: "" . to_owned( )
147
- } )
148
- )
151
+ }
152
+ ) )
149
153
) ;
150
154
151
155
let input = "TODO:\n " ;
152
156
assert_eq ! (
153
157
parse_todo( "TODO:" , input) ,
154
- Ok ( ( "" ,
158
+ Ok ( (
159
+ "" ,
155
160
Todo {
156
161
tag: "TODO:" . to_owned( ) ,
157
162
text: "" . to_owned( )
158
- } )
159
- )
163
+ }
164
+ ) )
160
165
) ;
161
166
}
162
167
@@ -165,12 +170,13 @@ mod test {
165
170
let input = "TODO: test todo\n line1" ;
166
171
assert_eq ! (
167
172
parse_todo( "TODO:" , input) ,
168
- Ok ( ( "line1" ,
173
+ Ok ( (
174
+ "line1" ,
169
175
Todo {
170
176
tag: "TODO:" . to_owned( ) ,
171
177
text: " test todo" . to_owned( )
172
- } )
173
- )
178
+ }
179
+ ) )
174
180
) ;
175
181
}
176
182
@@ -179,12 +185,13 @@ mod test {
179
185
let input = "// TODO: test todo" ;
180
186
assert_eq ! (
181
187
parse_todo( "TODO:" , input) ,
182
- Ok ( ( "" ,
188
+ Ok ( (
189
+ "" ,
183
190
Todo {
184
191
tag: "TODO:" . to_owned( ) ,
185
192
text: " test todo" . to_owned( )
186
- } )
187
- )
193
+ }
194
+ ) )
188
195
) ;
189
196
}
190
197
0 commit comments