From 5c3bc8d0e1aa9c26e618bd89886f0d061fd4727a Mon Sep 17 00:00:00 2001 From: Ed Page Date: Sat, 4 May 2024 22:44:28 +0200 Subject: [PATCH 1/2] Revert "feat(embedded): Add prefix-char frontmatter syntax support" This reverts commit b77ce7fa292622c5482047ee428c1ead1f4c3643. --- src/cargo/util/toml/embedded.rs | 66 ++++----------------------------- 1 file changed, 8 insertions(+), 58 deletions(-) diff --git a/src/cargo/util/toml/embedded.rs b/src/cargo/util/toml/embedded.rs index 10a081a787c..f20ed8d193d 100644 --- a/src/cargo/util/toml/embedded.rs +++ b/src/cargo/util/toml/embedded.rs @@ -185,7 +185,7 @@ fn sanitize_name(name: &str) -> String { struct Source<'s> { shebang: Option<&'s str>, info: Option<&'s str>, - frontmatter: Option, + frontmatter: Option<&'s str>, content: &'s str, } @@ -234,14 +234,11 @@ fn split_source(input: &str) -> CargoResult> { 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), @@ -255,7 +252,7 @@ fn split_source(input: &str) -> CargoResult> { let Some((frontmatter, content)) = source.content.split_once(fence_pattern) else { anyhow::bail!("no closing `{fence_pattern}` found for frontmatter"); }; - source.frontmatter = Some(frontmatter.to_owned()); + source.frontmatter = Some(frontmatter); source.content = content; let (line, content) = source @@ -271,22 +268,6 @@ fn split_source(input: &str) -> CargoResult> { Ok(source) } -fn split_prefix_source<'s>(mut source: Source<'s>, prefix: &str) -> CargoResult> { - let mut frontmatter = String::new(); - while let Some(rest) = source.content.strip_prefix(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::*; @@ -394,7 +375,7 @@ fn main() {} } #[test] - fn test_dash_fence() { + fn test_dash() { snapbox::assert_matches( r#"[[bin]] name = "test-" @@ -427,7 +408,7 @@ fn main() {} } #[test] - fn test_hash_fence() { + fn test_hash() { snapbox::assert_matches( r#"[[bin]] name = "test-" @@ -455,37 +436,6 @@ 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() {} "#), ); } From 229385bcc80a643e8103447325e0f522251922e0 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Sat, 4 May 2024 23:11:19 +0200 Subject: [PATCH 2/2] fix(toml): Remove dashed and hashed frontmatter syntax --- src/cargo/util/toml/embedded.rs | 81 ++------------------------------- tests/testsuite/script.rs | 16 +++---- 2 files changed, 11 insertions(+), 86 deletions(-) diff --git a/src/cargo/util/toml/embedded.rs b/src/cargo/util/toml/embedded.rs index f20ed8d193d..14c57db4576 100644 --- a/src/cargo/util/toml/embedded.rs +++ b/src/cargo/util/toml/embedded.rs @@ -218,12 +218,7 @@ fn split_source(input: &str) -> CargoResult> { } // Experiment: let us try which char works better - let tick_char = source - .content - .chars() - .filter(|c| ['`', '#', '-'].contains(c)) - .next() - .unwrap_or('`'); + let tick_char = '-'; let tick_end = source .content @@ -235,10 +230,6 @@ fn split_source(input: &str) -> CargoResult> { return Ok(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), @@ -332,10 +323,10 @@ strip = true [workspace] "#, - si!(r#"```cargo + si!(r#"---cargo [dependencies] time="0.1.25" -``` +--- fn main() {} "#), ); @@ -363,39 +354,6 @@ name = "test-" [profile.release] strip = true -[workspace] -"#, - si!(r#"``` -[dependencies] -time="0.1.25" -``` -fn main() {} -"#), - ); - } - - #[test] - fn test_dash() { - 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#"--- @@ -403,39 +361,6 @@ strip = true time="0.1.25" --- fn main() {} -"#), - ); - } - - #[test] - fn test_hash() { - 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() {} "#), ); } diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index 9f7b13ac98b..c3abefe455c 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -210,10 +210,10 @@ fn requires_z_flag() { #[cargo_test] fn clean_output_with_edition() { let script = r#"#!/usr/bin/env cargo -```cargo +--- [package] edition = "2018" -``` +--- fn main() { println!("Hello world!"); @@ -241,9 +241,9 @@ fn main() { #[cargo_test] fn warning_without_edition() { let script = r#"#!/usr/bin/env cargo -```cargo +--- [package] -``` +--- fn main() { println!("Hello world!"); @@ -714,10 +714,10 @@ fn did_you_mean_command_stable() { fn test_name_same_as_dependency() { Package::new("script", "1.0.0").publish(); let script = r#"#!/usr/bin/env cargo -```cargo +--- [dependencies] script = "1.0.0" -``` +--- fn main() { println!("Hello world!"); @@ -751,10 +751,10 @@ fn main() { #[cargo_test] fn test_path_dep() { let script = r#"#!/usr/bin/env cargo -```cargo +--- [dependencies] bar.path = "./bar" -``` +--- fn main() { println!("Hello world!");