Skip to content

Commit 82836b0

Browse files
committed
fix(toml): Strip blank linkes before frontmatter
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.
1 parent c1248ad commit 82836b0

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/cargo/util/toml/embedded.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,17 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
228228

229229
const FENCE_CHAR: char = '-';
230230

231-
let fence_end = source
232-
.content
231+
let mut trimmed_content = source.content;
232+
while !trimmed_content.is_empty() {
233+
let c = trimmed_content;
234+
let c = c.trim_start_matches([' ', '\t']);
235+
let c = c.trim_start_matches(['\r', '\n']);
236+
if c == trimmed_content {
237+
break;
238+
}
239+
trimmed_content = c;
240+
}
241+
let fence_end = trimmed_content
233242
.char_indices()
234243
.find_map(|(i, c)| (c != FENCE_CHAR).then_some(i))
235244
.unwrap_or(source.content.len());
@@ -242,7 +251,7 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
242251
"found {fence_end} `{FENCE_CHAR}` in rust frontmatter, expected at least 3"
243252
)
244253
}
245-
_ => source.content.split_at(fence_end),
254+
_ => trimmed_content.split_at(fence_end),
246255
};
247256
let (info, content) = rest.split_once("\n").unwrap_or((rest, ""));
248257
let info = info.trim();
@@ -438,8 +447,8 @@ fn main() {}
438447
str![[r##"
439448
shebang: "#!/usr/bin/env cargo\n"
440449
info: None
441-
frontmatter: None
442-
content: " \n\n\n---\n[dependencies]\ntime=\"0.1.25\"\n---\n\n\nfn main() {}\n"
450+
frontmatter: "[dependencies]\ntime=\"0.1.25\"\n"
451+
content: "\n\nfn main() {}\n"
443452
444453
"##]],
445454
);

0 commit comments

Comments
 (0)