Skip to content

Commit 2ce4560

Browse files
committed
Auto merge of #13247 - epage:prefix, r=Muscraft
feat(embedded): Add prefix-char frontmatter syntax support This is a follow up to #13241 with another syntax being discussed. This one is a bit more polarizing but we're hoping first-hand experience with it can help people get a feel for how well it works in practice. As the experiment is meant to be short-lived, this is implemented in a hacky way and docs aren't updated.
2 parents 87eb374 + b77ce7f commit 2ce4560

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

src/cargo/util/toml/embedded.rs

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ fn sanitize_name(name: &str) -> String {
185185
struct Source<'s> {
186186
shebang: Option<&'s str>,
187187
info: Option<&'s str>,
188-
frontmatter: Option<&'s str>,
188+
frontmatter: Option<String>,
189189
content: &'s str,
190190
}
191191

@@ -234,11 +234,14 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
234234
0 => {
235235
return Ok(source);
236236
}
237+
1 if tick_char == '#' => {
238+
// Attribute
239+
return Ok(source);
240+
}
241+
2 if tick_char == '#' => {
242+
return split_prefix_source(source, "##");
243+
}
237244
1 | 2 => {
238-
if tick_char == '#' {
239-
// Attribute
240-
return Ok(source);
241-
}
242245
anyhow::bail!("found {tick_end} `{tick_char}` in rust frontmatter, expected at least 3")
243246
}
244247
_ => source.content.split_at(tick_end),
@@ -252,7 +255,7 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
252255
let Some((frontmatter, content)) = source.content.split_once(fence_pattern) else {
253256
anyhow::bail!("no closing `{fence_pattern}` found for frontmatter");
254257
};
255-
source.frontmatter = Some(frontmatter);
258+
source.frontmatter = Some(frontmatter.to_owned());
256259
source.content = content;
257260

258261
let (line, content) = source
@@ -268,6 +271,22 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
268271
Ok(source)
269272
}
270273

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+
271290
#[cfg(test)]
272291
mod test_expand {
273292
use super::*;
@@ -375,7 +394,7 @@ fn main() {}
375394
}
376395

377396
#[test]
378-
fn test_dash() {
397+
fn test_dash_fence() {
379398
snapbox::assert_matches(
380399
r#"[[bin]]
381400
name = "test-"
@@ -408,7 +427,7 @@ fn main() {}
408427
}
409428

410429
#[test]
411-
fn test_hash() {
430+
fn test_hash_fence() {
412431
snapbox::assert_matches(
413432
r#"[[bin]]
414433
name = "test-"
@@ -436,6 +455,37 @@ strip = true
436455
time="0.1.25"
437456
###
438457
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() {}
439489
"#),
440490
);
441491
}

0 commit comments

Comments
 (0)