Skip to content

Commit

Permalink
fix(toml): Strip blank linkes before frontmatter
Browse files Browse the repository at this point in the history
From the [RFC](https://rust-lang.github.io/rfcs/3503-frontmatter.html)

> When parsing Rust source, after stripping the shebang (#!), rustc will strip the frontmatter:
>
> - May include 0+ blank lines (whitespace + newline)

The question is what is a non-newline whitespace and what is a newline.
Looking at the [Reference](https://doc.rust-lang.org/reference/whitespace.html),
the answer is "unsure", particularly because the Rust language doesn't
generally try to distinguish them.
I kept things basic for now and we can revisit later.
  • Loading branch information
epage committed Nov 12, 2024
1 parent c1248ad commit 82836b0
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/cargo/util/toml/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,17 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {

const FENCE_CHAR: char = '-';

let fence_end = source
.content
let mut trimmed_content = source.content;
while !trimmed_content.is_empty() {
let c = trimmed_content;
let c = c.trim_start_matches([' ', '\t']);
let c = c.trim_start_matches(['\r', '\n']);
if c == trimmed_content {
break;
}
trimmed_content = c;
}
let fence_end = trimmed_content
.char_indices()
.find_map(|(i, c)| (c != FENCE_CHAR).then_some(i))
.unwrap_or(source.content.len());
Expand All @@ -242,7 +251,7 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
"found {fence_end} `{FENCE_CHAR}` in rust frontmatter, expected at least 3"
)
}
_ => source.content.split_at(fence_end),
_ => trimmed_content.split_at(fence_end),
};
let (info, content) = rest.split_once("\n").unwrap_or((rest, ""));
let info = info.trim();
Expand Down Expand Up @@ -438,8 +447,8 @@ fn main() {}
str![[r##"
shebang: "#!/usr/bin/env cargo\n"
info: None
frontmatter: None
content: " \n\n\n---\n[dependencies]\ntime=\"0.1.25\"\n---\n\n\nfn main() {}\n"
frontmatter: "[dependencies]\ntime=\"0.1.25\"\n"
content: "\n\nfn main() {}\n"
"##]],
);
Expand Down

0 comments on commit 82836b0

Please sign in to comment.