Skip to content

Commit

Permalink
feat(embedded): Allow experimenting with triple dash/hash fences
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 2, 2024
1 parent 0435ef3 commit 628c869
Showing 1 changed file with 79 additions and 1 deletion.
80 changes: 79 additions & 1 deletion src/cargo/util/toml/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,28 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
source.content = content;
}

// Experiment: let us try which char works better
let tick_char = source
.content
.chars()
.filter(|c| ['`', '#', '-'].contains(c))
.next()
.unwrap_or('`');

let tick_end = source
.content
.char_indices()
.find_map(|(i, c)| (c != '`').then_some(i))
.find_map(|(i, c)| (c != tick_char).then_some(i))
.unwrap_or(source.content.len());
let (fence_pattern, rest) = match tick_end {
0 => {
return Ok(source);
}
1 | 2 => {
if tick_char == '#' {
// Attribute
return Ok(source);
}
anyhow::bail!("found {tick_end} backticks in rust frontmatter, expected at least 3")
}
_ => source.content.split_at(tick_end),
Expand Down Expand Up @@ -358,6 +370,72 @@ strip = true
time="0.1.25"
```
fn main() {}
"#),
);
}

#[test]
fn test_dash() {
snapbox::assert_matches(
r#"[[bin]]
name = "test-"
path = "[..]/test.rs"
[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() {}
"#),
);
}

#[test]
fn test_hash() {
snapbox::assert_matches(
r#"[[bin]]
name = "test-"
path = "[..]/test.rs"
[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() {}
"#),
);
}
Expand Down

0 comments on commit 628c869

Please sign in to comment.