@@ -185,7 +185,7 @@ fn sanitize_name(name: &str) -> String {
185
185
struct Source < ' s > {
186
186
shebang : Option < & ' s str > ,
187
187
info : Option < & ' s str > ,
188
- frontmatter : Option < & ' s str > ,
188
+ frontmatter : Option < String > ,
189
189
content : & ' s str ,
190
190
}
191
191
@@ -234,11 +234,14 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
234
234
0 => {
235
235
return Ok ( source) ;
236
236
}
237
+ 1 if tick_char == '#' => {
238
+ // Attribute
239
+ return Ok ( source) ;
240
+ }
241
+ 2 if tick_char == '#' => {
242
+ return split_prefix_source ( source, "##" ) ;
243
+ }
237
244
1 | 2 => {
238
- if tick_char == '#' {
239
- // Attribute
240
- return Ok ( source) ;
241
- }
242
245
anyhow:: bail!( "found {tick_end} `{tick_char}` in rust frontmatter, expected at least 3" )
243
246
}
244
247
_ => source. content . split_at ( tick_end) ,
@@ -252,7 +255,7 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
252
255
let Some ( ( frontmatter, content) ) = source. content . split_once ( fence_pattern) else {
253
256
anyhow:: bail!( "no closing `{fence_pattern}` found for frontmatter" ) ;
254
257
} ;
255
- source. frontmatter = Some ( frontmatter) ;
258
+ source. frontmatter = Some ( frontmatter. to_owned ( ) ) ;
256
259
source. content = content;
257
260
258
261
let ( line, content) = source
@@ -268,6 +271,22 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
268
271
Ok ( source)
269
272
}
270
273
274
+ fn split_prefix_source < ' s > ( mut source : Source < ' s > , prefix : & str ) -> CargoResult < Source < ' s > > {
275
+ let mut frontmatter = String :: new ( ) ;
276
+ while let Some ( rest) = source. content . strip_prefix ( prefix) {
277
+ if !rest. is_empty ( ) && !rest. starts_with ( ' ' ) {
278
+ anyhow:: bail!( "frontmatter must have a space between `##` and the content" ) ;
279
+ }
280
+ let ( line, rest) = rest. split_once ( '\n' ) . unwrap_or ( ( rest, "" ) ) ;
281
+ frontmatter. push_str ( " " ) ;
282
+ frontmatter. push_str ( line) ;
283
+ frontmatter. push ( '\n' ) ;
284
+ source. content = rest;
285
+ }
286
+ source. frontmatter = Some ( frontmatter) ;
287
+ Ok ( source)
288
+ }
289
+
271
290
#[ cfg( test) ]
272
291
mod test_expand {
273
292
use super :: * ;
@@ -375,7 +394,7 @@ fn main() {}
375
394
}
376
395
377
396
#[ test]
378
- fn test_dash ( ) {
397
+ fn test_dash_fence ( ) {
379
398
snapbox:: assert_matches (
380
399
r#"[[bin]]
381
400
name = "test-"
@@ -408,7 +427,7 @@ fn main() {}
408
427
}
409
428
410
429
#[ test]
411
- fn test_hash ( ) {
430
+ fn test_hash_fence ( ) {
412
431
snapbox:: assert_matches (
413
432
r#"[[bin]]
414
433
name = "test-"
@@ -436,6 +455,37 @@ strip = true
436
455
time="0.1.25"
437
456
###
438
457
fn main() {}
458
+ "# ) ,
459
+ ) ;
460
+ }
461
+
462
+ #[ test]
463
+ fn test_hash_prefix ( ) {
464
+ snapbox:: assert_matches (
465
+ r#"[[bin]]
466
+ name = "test-"
467
+ path = [..]
468
+
469
+ [dependencies]
470
+ time = "0.1.25"
471
+
472
+ [package]
473
+ autobenches = false
474
+ autobins = false
475
+ autoexamples = false
476
+ autotests = false
477
+ build = false
478
+ edition = "2021"
479
+ name = "test-"
480
+
481
+ [profile.release]
482
+ strip = true
483
+
484
+ [workspace]
485
+ "# ,
486
+ si ! ( r#"## [dependencies]
487
+ ## time="0.1.25"
488
+ fn main() {}
439
489
"# ) ,
440
490
) ;
441
491
}
0 commit comments