@@ -8,15 +8,15 @@ pub struct ListType(pub String);
8
8
pub struct ListIndent ( pub String ) ;
9
9
10
10
#[ derive( Debug , PartialEq , Eq ) ]
11
- pub enum Token {
12
- Comment ( String ) ,
11
+ pub enum Token < ' input > {
12
+ Comment ( & ' input str ) ,
13
13
Footnote ( String , String ) ,
14
14
ListItem ( ListIndent , ListType , String ) ,
15
- Literal ( String ) ,
15
+ Literal ( & ' input str ) ,
16
16
Paragraph ( String ) ,
17
17
Subject ( String ) ,
18
- Scissored ( String ) ,
19
- Trailer ( String ) ,
18
+ Scissored ( & ' input str ) ,
19
+ Trailer ( & ' input str ) ,
20
20
VerticalSpace ,
21
21
}
22
22
@@ -58,22 +58,13 @@ pub fn parse(input: &str, comment_char: char) -> Vec<Token> {
58
58
let mut px = false ;
59
59
for line in lines {
60
60
if has_scissors {
61
- match * toks. last_mut ( ) . expect ( "has_scissors" ) {
62
- Token :: Scissored ( ref mut s) => {
63
- s. push_str ( line) ;
64
- s. push ( '\n' ) ;
65
- }
66
- _ => unreachable ! ( ) ,
67
- }
61
+ toks. push ( Token :: Scissored ( line) ) ;
68
62
} else if line. starts_with ( comment_char) {
69
63
let t = if & line[ 1 ..] == " ------------------------ >8 ------------------------" {
70
64
has_scissors = true ;
71
- let mut raw = String :: with_capacity ( 20 * 60 ) ; // Toilet maths.
72
- raw. push_str ( line) ;
73
- raw. push ( '\n' ) ; // Recover linefeed lost from iterator.
74
- Token :: Scissored ( raw)
65
+ Token :: Scissored ( line)
75
66
} else {
76
- Token :: Comment ( line. to_owned ( ) )
67
+ Token :: Comment ( line)
77
68
} ;
78
69
toks. push ( t) ;
79
70
} else if blank_or_empty. is_match ( line) {
@@ -92,7 +83,7 @@ pub fn parse(input: &str, comment_char: char) -> Vec<Token> {
92
83
let rest = splitter. next ( ) . unwrap ( ) . trim ( ) . to_owned ( ) ;
93
84
toks. push ( Token :: Footnote ( key, rest) ) ;
94
85
} else if trailer. is_match ( line) {
95
- toks. push ( Token :: Trailer ( line. to_owned ( ) ) ) ;
86
+ toks. push ( Token :: Trailer ( line) ) ;
96
87
} else if let Some ( y) = match toks. last_mut ( ) {
97
88
Some ( & mut Token :: Footnote ( _, ref mut b) ) => {
98
89
b. push ( ' ' ) ;
@@ -117,9 +108,7 @@ pub fn parse(input: &str, comment_char: char) -> Vec<Token> {
117
108
if list_item. is_match ( line) {
118
109
Some ( list_item_from_line ( & list_item, line) )
119
110
} else if indented. is_match ( line) {
120
- let mut raw = line. to_owned ( ) ;
121
- raw. push ( '\n' ) ; // Recover linefeed lost from iterator.
122
- Some ( Token :: Literal ( raw) )
111
+ Some ( Token :: Literal ( line) )
123
112
} else {
124
113
px = false ;
125
114
Some ( Token :: Paragraph ( line. trim ( ) . to_owned ( ) ) )
@@ -180,7 +169,7 @@ fn parse_subject(line: &str, toks: &mut Vec<Token>) {
180
169
}
181
170
}
182
171
183
- fn list_item_from_line ( pat : & Regex , line : & str ) -> Token {
172
+ fn list_item_from_line < ' a > ( pat : & Regex , line : & str ) -> Token < ' a > {
184
173
let captures = pat. captures ( line) . unwrap ( ) ;
185
174
let indent = captures. name ( "indent" ) . unwrap ( ) ;
186
175
let li = captures. name ( "li" ) . unwrap ( ) ;
@@ -229,24 +218,20 @@ mod tests {
229
218
230
219
#[ test]
231
220
fn parses_default_comment ( ) {
232
- assert_eq ! ( super :: parse( "# foo" , '#' ) , [ Comment ( "# foo" . to_owned ( ) ) ] ) ;
221
+ assert_eq ! ( super :: parse( "# foo" , '#' ) , [ Comment ( "# foo" ) ] ) ;
233
222
}
234
223
235
224
#[ test]
236
225
fn parses_custom_comment ( ) {
237
- assert_eq ! ( super :: parse( "@ foo" , '@' ) , [ Comment ( "@ foo" . to_owned ( ) ) ] ) ;
226
+ assert_eq ! ( super :: parse( "@ foo" , '@' ) , [ Comment ( "@ foo" ) ] ) ;
238
227
assert_eq ! ( super :: parse( "# foo" , '@' ) , [ Subject ( "# foo" . to_owned( ) ) ] ) ;
239
228
}
240
229
241
230
#[ test]
242
231
fn parses_mixed_comment_and_content ( ) {
243
232
assert_eq ! (
244
233
parse( "# foo\n \n # bar" ) ,
245
- [
246
- Comment ( "# foo" . to_owned( ) ) ,
247
- VerticalSpace ,
248
- Subject ( "# bar" . to_owned( ) ) ,
249
- ] ,
234
+ [ Comment ( "# foo" ) , VerticalSpace , Subject ( "# bar" . to_owned( ) ) , ] ,
250
235
) ;
251
236
}
252
237
@@ -273,7 +258,7 @@ mod tests {
273
258
#[ test]
274
259
fn parses_fitting_subject ( ) {
275
260
let s = "f" . repeat ( SUBJECT_CHAR_LIMIT ) ;
276
- assert_eq ! ( parse( & s) , [ Subject ( s) ] ) ;
261
+ assert_eq ! ( parse( & s) , [ Subject ( s. to_owned ( ) ) ] ) ;
277
262
}
278
263
279
264
#[ test]
@@ -299,7 +284,10 @@ mod tests {
299
284
prefix = autosquash_prefix,
300
285
subject = original_subject
301
286
) ;
302
- assert_eq ! ( parse( & autosquash_subject) , [ Subject ( autosquash_subject) , ] , ) ;
287
+ assert_eq ! (
288
+ parse( & autosquash_subject) ,
289
+ [ Subject ( autosquash_subject. to_owned( ) ) , ] ,
290
+ ) ;
303
291
}
304
292
}
305
293
@@ -371,7 +359,7 @@ paragraphs
371
359
Paragraph ( "this is one paragraph" . to_owned( ) ) ,
372
360
VerticalSpace ,
373
361
Paragraph ( "this is" . to_owned( ) ) ,
374
- Comment ( "# two" . to_owned ( ) ) ,
362
+ Comment ( "# two" ) ,
375
363
Paragraph ( "paragraphs" . to_owned( ) ) ,
376
364
]
377
365
) ;
@@ -417,8 +405,8 @@ some other paragraph
417
405
VerticalSpace ,
418
406
Paragraph ( "some paragraph" . to_owned( ) ) ,
419
407
VerticalSpace ,
420
- Literal ( " some 4-space literal\n " . to_owned ( ) ) ,
421
- Literal ( " continuation\n " . to_owned ( ) ) ,
408
+ Literal ( " some 4-space literal" ) ,
409
+ Literal ( " continuation" ) ,
422
410
VerticalSpace ,
423
411
Paragraph ( "some other paragraph no literal without vertical space" . to_owned( ) ) ,
424
412
] ,
@@ -448,9 +436,9 @@ some other paragraph
448
436
VerticalSpace ,
449
437
Paragraph ( "some paragraph" . to_owned( ) ) ,
450
438
VerticalSpace ,
451
- Literal ( "\t some 4-space literal\n " . to_owned ( ) ) ,
452
- Literal ( "\t continuation\n " . to_owned ( ) ) ,
453
- Literal ( "\t \t continuation\n " . to_owned ( ) ) ,
439
+ Literal ( "\t some 4-space literal" ) ,
440
+ Literal ( "\t continuation" ) ,
441
+ Literal ( "\t \t continuation" ) ,
454
442
VerticalSpace ,
455
443
Paragraph ( "some other paragraph no literal without vertical space" . to_owned( ) ) ,
456
444
] ,
@@ -487,9 +475,9 @@ some other paragraph
487
475
VerticalSpace ,
488
476
Paragraph ( "some paragraph" . to_owned( ) ) ,
489
477
VerticalSpace ,
490
- Literal ( " some 4-space literal\n " . to_owned ( ) ) ,
491
- Literal ( " some 4-space literal\n " . to_owned ( ) ) ,
492
- Literal ( " some 4-space literal\n " . to_owned ( ) ) ,
478
+ Literal ( " some 4-space literal" ) ,
479
+ Literal ( " some 4-space literal" ) ,
480
+ Literal ( " some 4-space literal" ) ,
493
481
VerticalSpace ,
494
482
Paragraph ( "some other paragraph" . to_owned( ) ) ,
495
483
] ,
520
508
VerticalSpace ,
521
509
Subject ( "subject" . to_owned( ) ) ,
522
510
VerticalSpace ,
523
- Trailer ( "Fixes: All the things" . to_owned ( ) ) ,
524
- Trailer ( "Cc: John Doe <[email protected] >" . to_owned ( ) ) ,
525
- Trailer ( "Reviewed-by: NSA" . to_owned ( ) ) ,
526
- Trailer ( "Signed-off-by: Jane Doe <[email protected] >" . to_owned ( ) ) ,
511
+ Trailer ( "Fixes: All the things" ) ,
512
+ Trailer ( "Cc: John Doe <[email protected] >" ) ,
513
+ Trailer ( "Reviewed-by: NSA" ) ,
514
+ Trailer ( "Signed-off-by: Jane Doe <[email protected] >" ) ,
527
515
] ,
528
516
) ;
529
517
}
874
862
VerticalSpace ,
875
863
Paragraph ( "format this" . to_owned( ) ) ,
876
864
VerticalSpace ,
877
- Scissored (
878
- r#"# ------------------------ >8 ------------------------
879
- do
880
- not
881
- format
882
- this
883
- "#
884
- . to_owned( )
885
- ) ,
865
+ Scissored ( "# ------------------------ >8 ------------------------" ) ,
866
+ Scissored ( "do" ) ,
867
+ Scissored ( " not" ) ,
868
+ Scissored ( " format" ) ,
869
+ Scissored ( " this" ) ,
886
870
] ,
887
871
) ;
888
872
}
910
894
VerticalSpace ,
911
895
Paragraph ( "# ------------------------ >8 ------------------------ above is not a comment; do the needful" . to_owned( ) ) ,
912
896
VerticalSpace ,
913
- Scissored (
914
- r#"$ ------------------------ >8 ------------------------
915
- do
916
- not
917
- format
918
- "# . to_owned( )
919
- ) ,
897
+ Scissored ( "$ ------------------------ >8 ------------------------" ) ,
898
+ Scissored ( "do" ) ,
899
+ Scissored ( " not" ) ,
900
+ Scissored ( " format" ) ,
920
901
] ,
921
902
) ;
922
903
}
0 commit comments