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 3, 2024
1 parent e46f916 commit 22200b7
Showing 1 changed file with 80 additions and 2 deletions.
82 changes: 80 additions & 2 deletions src/cargo/util/toml/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,29 @@ 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 => {
anyhow::bail!("found {tick_end} backticks in rust frontmatter, expected at least 3")
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),
};
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 = [..]
[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 = [..]
[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 22200b7

Please sign in to comment.