Skip to content

Commit 7c03de7

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 066b9e9 commit 7c03de7

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/cargo/util/toml/embedded.rs

+16-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();
@@ -470,8 +479,10 @@ Ok(
470479
"#!/usr/bin/env cargo\n",
471480
),
472481
info: None,
473-
frontmatter: None,
474-
content: " \n\n\n---\n[dependencies]\ntime=\"0.1.25\"\n---\n\n\nfn main() {}\n",
482+
frontmatter: Some(
483+
"[dependencies]\ntime=\"0.1.25\"\n",
484+
),
485+
content: "\n\nfn main() {}\n",
475486
},
476487
)
477488

0 commit comments

Comments
 (0)