Skip to content

Commit cfbc4b7

Browse files
committed
Auto merge of #13861 - epage:frontmatter, r=ehuss
fix(toml): Remove unstable rejrected frontmatter syntax for cargo script ### What does this PR try to resolve? With rust-lang/rfcs#3503 approved, we no longer need to allow easy, high fidelity experiments with alternative cargo script syntax. ### How should we test and review this PR? ### Additional information We still need to improve the experience for users writing bad syntax but that can come later.
2 parents 85a4d70 + 229385b commit cfbc4b7

File tree

2 files changed

+13
-138
lines changed

2 files changed

+13
-138
lines changed

src/cargo/util/toml/embedded.rs

+5-130
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<String>,
188+
frontmatter: Option<&'s str>,
189189
content: &'s str,
190190
}
191191

@@ -218,12 +218,7 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
218218
}
219219

220220
// Experiment: let us try which char works better
221-
let tick_char = source
222-
.content
223-
.chars()
224-
.filter(|c| ['`', '#', '-'].contains(c))
225-
.next()
226-
.unwrap_or('`');
221+
let tick_char = '-';
227222

228223
let tick_end = source
229224
.content
@@ -234,13 +229,6 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
234229
0 => {
235230
return Ok(source);
236231
}
237-
1 if tick_char == '#' => {
238-
// Attribute
239-
return Ok(source);
240-
}
241-
2 if tick_char == '#' => {
242-
return split_prefix_source(source, "##");
243-
}
244232
1 | 2 => {
245233
anyhow::bail!("found {tick_end} `{tick_char}` in rust frontmatter, expected at least 3")
246234
}
@@ -255,7 +243,7 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
255243
let Some((frontmatter, content)) = source.content.split_once(fence_pattern) else {
256244
anyhow::bail!("no closing `{fence_pattern}` found for frontmatter");
257245
};
258-
source.frontmatter = Some(frontmatter.to_owned());
246+
source.frontmatter = Some(frontmatter);
259247
source.content = content;
260248

261249
let (line, content) = source
@@ -271,22 +259,6 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
271259
Ok(source)
272260
}
273261

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-
290262
#[cfg(test)]
291263
mod test_expand {
292264
use super::*;
@@ -351,10 +323,10 @@ strip = true
351323
352324
[workspace]
353325
"#,
354-
si!(r#"```cargo
326+
si!(r#"---cargo
355327
[dependencies]
356328
time="0.1.25"
357-
```
329+
---
358330
fn main() {}
359331
"#),
360332
);
@@ -382,110 +354,13 @@ name = "test-"
382354
[profile.release]
383355
strip = true
384356
385-
[workspace]
386-
"#,
387-
si!(r#"```
388-
[dependencies]
389-
time="0.1.25"
390-
```
391-
fn main() {}
392-
"#),
393-
);
394-
}
395-
396-
#[test]
397-
fn test_dash_fence() {
398-
snapbox::assert_matches(
399-
r#"[[bin]]
400-
name = "test-"
401-
path = [..]
402-
403-
[dependencies]
404-
time = "0.1.25"
405-
406-
[package]
407-
autobenches = false
408-
autobins = false
409-
autoexamples = false
410-
autotests = false
411-
build = false
412-
edition = "2021"
413-
name = "test-"
414-
415-
[profile.release]
416-
strip = true
417-
418357
[workspace]
419358
"#,
420359
si!(r#"---
421360
[dependencies]
422361
time="0.1.25"
423362
---
424363
fn main() {}
425-
"#),
426-
);
427-
}
428-
429-
#[test]
430-
fn test_hash_fence() {
431-
snapbox::assert_matches(
432-
r#"[[bin]]
433-
name = "test-"
434-
path = [..]
435-
436-
[dependencies]
437-
time = "0.1.25"
438-
439-
[package]
440-
autobenches = false
441-
autobins = false
442-
autoexamples = false
443-
autotests = false
444-
build = false
445-
edition = "2021"
446-
name = "test-"
447-
448-
[profile.release]
449-
strip = true
450-
451-
[workspace]
452-
"#,
453-
si!(r#"###
454-
[dependencies]
455-
time="0.1.25"
456-
###
457-
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() {}
489364
"#),
490365
);
491366
}

tests/testsuite/script.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,10 @@ fn requires_z_flag() {
210210
#[cargo_test]
211211
fn clean_output_with_edition() {
212212
let script = r#"#!/usr/bin/env cargo
213-
```cargo
213+
---
214214
[package]
215215
edition = "2018"
216-
```
216+
---
217217
218218
fn main() {
219219
println!("Hello world!");
@@ -241,9 +241,9 @@ fn main() {
241241
#[cargo_test]
242242
fn warning_without_edition() {
243243
let script = r#"#!/usr/bin/env cargo
244-
```cargo
244+
---
245245
[package]
246-
```
246+
---
247247
248248
fn main() {
249249
println!("Hello world!");
@@ -714,10 +714,10 @@ fn did_you_mean_command_stable() {
714714
fn test_name_same_as_dependency() {
715715
Package::new("script", "1.0.0").publish();
716716
let script = r#"#!/usr/bin/env cargo
717-
```cargo
717+
---
718718
[dependencies]
719719
script = "1.0.0"
720-
```
720+
---
721721
722722
fn main() {
723723
println!("Hello world!");
@@ -751,10 +751,10 @@ fn main() {
751751
#[cargo_test]
752752
fn test_path_dep() {
753753
let script = r#"#!/usr/bin/env cargo
754-
```cargo
754+
---
755755
[dependencies]
756756
bar.path = "./bar"
757-
```
757+
---
758758
759759
fn main() {
760760
println!("Hello world!");

0 commit comments

Comments
 (0)