Skip to content

Commit

Permalink
feat(embedded): Add prefix-char frontmatter syntax support
Browse files Browse the repository at this point in the history
This is a follow up to rust-lang#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.
  • Loading branch information
epage committed Jan 3, 2024
1 parent f9946d1 commit a3609b9
Showing 1 changed file with 58 additions and 8 deletions.
66 changes: 58 additions & 8 deletions src/cargo/util/toml/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn sanitize_name(name: &str) -> String {
struct Source<'s> {
shebang: Option<&'s str>,
info: Option<&'s str>,
frontmatter: Option<&'s str>,
frontmatter: Option<String>,
content: &'s str,
}

Expand Down Expand Up @@ -234,11 +234,14 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
0 => {
return Ok(source);
}
1 if tick_char == '#' => {
// Attribute
return Ok(source);
}
2 if tick_char == '#' => {
return split_prefix_source(source);
}
1 | 2 => {
if tick_char == '#' {
// Attribute
return Ok(source);
}
anyhow::bail!("found {tick_end} `{tick_char}` in rust frontmatter, expected at least 3")
}
_ => source.content.split_at(tick_end),
Expand All @@ -252,7 +255,7 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
let Some((frontmatter, content)) = source.content.split_once(fence_pattern) else {
anyhow::bail!("no closing `{fence_pattern}` found for frontmatter");
};
source.frontmatter = Some(frontmatter);
source.frontmatter = Some(frontmatter.to_owned());
source.content = content;

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

fn split_prefix_source(mut source: Source<'_>) -> CargoResult<Source<'_>> {
let mut frontmatter = String::new();
while let Some(rest) = source.content.strip_prefix("##") {
if !rest.is_empty() && !rest.starts_with(' ') {
anyhow::bail!("frontmatter must have a space between `##` and the content");
}
let (line, rest) = rest.split_once('\n').unwrap_or((rest, ""));
frontmatter.push_str(" ");
frontmatter.push_str(line);
frontmatter.push('\n');
source.content = rest;
}
source.frontmatter = Some(frontmatter);
Ok(source)
}

#[cfg(test)]
mod test_expand {
use super::*;
Expand Down Expand Up @@ -375,7 +394,7 @@ fn main() {}
}

#[test]
fn test_dash() {
fn test_dash_fence() {
snapbox::assert_matches(
r#"[[bin]]
name = "test-"
Expand Down Expand Up @@ -408,7 +427,7 @@ fn main() {}
}

#[test]
fn test_hash() {
fn test_hash_fence() {
snapbox::assert_matches(
r#"[[bin]]
name = "test-"
Expand Down Expand Up @@ -436,6 +455,37 @@ strip = true
time="0.1.25"
###
fn main() {}
"#),
);
}

#[test]
fn test_hash_prefix() {
snapbox::assert_matches(
r#"[[bin]]
name = "test-"
path = [..]
[dependencies]
time = "0.1.25"
[package]
autobenches = false
autobins = false
autoexamples = false
autotests = false
build = false
edition = "2021"
name = "test-"
[profile.release]
strip = true
[workspace]
"#,
si!(r#"## [dependencies]
## time="0.1.25"
fn main() {}
"#),
);
}
Expand Down

0 comments on commit a3609b9

Please sign in to comment.