README: update with MSRV, and update mechanism for doing so.

Thanks to @kaesluder and @bioinformatist for various improvements that
are working together here!
This commit is contained in:
Asherah Connor 2024-04-18 15:59:24 +03:00
parent 15609f7a2c
commit 56581d7275
4 changed files with 140 additions and 48 deletions

81
Cargo.lock generated
View File

@ -148,6 +148,7 @@ dependencies = [
"shell-words",
"slug",
"syntect",
"toml 0.8.12",
"typed-arena",
"unicode_categories",
"xdg",
@ -280,6 +281,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5320ae4c3782150d900b79807611a59a99fc9a1d61d686faafc24b93fc8d7ca"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "errno"
version = "0.3.8"
@ -350,6 +357,12 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "hashbrown"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
[[package]]
name = "heck"
version = "0.4.0"
@ -387,7 +400,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399"
dependencies = [
"autocfg",
"hashbrown",
"hashbrown 0.12.3",
]
[[package]]
name = "indexmap"
version = "2.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
dependencies = [
"equivalent",
"hashbrown 0.14.3",
]
[[package]]
@ -579,7 +602,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd39bc6cdc9355ad1dc5eeedefee696bb35c34caf21768741e81826c0bbd7225"
dependencies = [
"base64",
"indexmap",
"indexmap 1.9.2",
"line-wrap",
"serde",
"time",
@ -600,7 +623,7 @@ checksum = "eda0fc3b0fb7c975631757e14d9049da17374063edb6ebbcbc54d880d4fe94e9"
dependencies = [
"once_cell",
"thiserror",
"toml",
"toml 0.5.10",
]
[[package]]
@ -867,6 +890,15 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_spanned"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1"
dependencies = [
"serde",
]
[[package]]
name = "shell-words"
version = "1.1.0"
@ -1016,6 +1048,40 @@ dependencies = [
"serde",
]
[[package]]
name = "toml"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.22.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e40bb779c5187258fd7aad0eb68cb8706a0a81fa712fbea808ab43c4b8374c4"
dependencies = [
"indexmap 2.2.6",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]]
name = "typed-arena"
version = "2.0.1"
@ -1250,6 +1316,15 @@ version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8"
[[package]]
name = "winnow"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0c976aaaa0e1f90dbb21e9587cdaf1d9679a1cde8875c0d6bd83ab96a208352"
dependencies = [
"memchr",
]
[[package]]
name = "xdg"
version = "2.4.1"

View File

@ -50,6 +50,7 @@ in-place = { version = "0.2.0", optional = true }
[dev-dependencies]
ntest = "0.9"
toml = "0.8"
[target.'cfg(not(target_arch="wasm32"))'.dev-dependencies]
propfuzz = "0.0.1"

View File

@ -24,7 +24,7 @@ Specify it as a requirement in `Cargo.toml`:
comrak = "0.22"
```
Comrak supports Rust stable.
Comrak's library supports Rust <span class="msrv">1.62.1</span>+.
### Mac & Linux Binaries
@ -56,6 +56,9 @@ Options:
[default: /Users/kivikakk/.config/comrak/config]
-i, --inplace
To perform an in-place formatting
--hardbreaks
Treat newlines as hard line breaks

View File

@ -1,64 +1,77 @@
// Update the "comrak --help" text in Comrak's own README.
use in_place::InPlace;
use std::error::Error;
use std::fmt::Write;
use std::str;
use toml::Table;
use comrak::nodes::{AstNode, NodeValue};
use comrak::nodes::NodeValue;
use comrak::{format_commonmark, parse_document, Arena, Options};
const DEPENDENCIES: &str = "[dependencies]\ncomrak = ";
const HELP: &str = "$ comrak --help\n";
fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
fn main() -> Result<(), Box<dyn Error>> {
let arena = Arena::new();
let readme = std::fs::read_to_string("README.md")?;
let inp = InPlace::new("README.md").open()?;
let readme = std::io::read_to_string(inp.reader())?;
let doc = parse_document(&arena, &readme, &Options::default());
fn iter_nodes<'a, F>(node: &'a AstNode<'a>, f: &F)
where
F: Fn(&'a AstNode<'a>),
{
f(node);
for c in node.children() {
iter_nodes(c, f);
let cargo_toml = std::fs::read_to_string("Cargo.toml")?.parse::<Table>()?;
let msrv = cargo_toml["package"].as_table().unwrap()["rust-version"]
.as_str()
.unwrap();
let mut in_msrv = false;
for node in doc.descendants() {
match node.data.borrow_mut().value {
NodeValue::CodeBlock(ref mut ncb) => {
// Look for the Cargo.toml example block.
if ncb.info == "toml" && ncb.literal.starts_with(DEPENDENCIES) {
let mut content = DEPENDENCIES.to_string();
let mut version_parts = comrak::version().split('.').collect::<Vec<&str>>();
version_parts.pop();
write!(content, "\"{}\"", version_parts.join(".")).unwrap();
ncb.literal = content;
}
// Look for a console code block whose contents starts with the HELP string.
// Replace its contents with the same string and the actual command output.
if ncb.info == "console" && ncb.literal.starts_with(HELP) {
let mut content = HELP.to_string();
let mut cmd = std::process::Command::new("cargo");
content.push_str(
str::from_utf8(
&cmd.args(["run", "--all-features", "--", "--help"])
.output()
.unwrap()
.stdout,
)
.unwrap(),
);
ncb.literal = content;
}
}
NodeValue::HtmlInline(ref mut s) => {
if s == "<span class=\"msrv\">" {
in_msrv = true;
} else if in_msrv && s == "</span>" {
in_msrv = false;
}
}
NodeValue::Text(ref mut t) => {
if in_msrv {
std::mem::swap(t, &mut msrv.to_string());
}
}
_ => {}
}
}
iter_nodes(doc, &|node| {
if let NodeValue::CodeBlock(ref mut ncb) = node.data.borrow_mut().value {
// Look for the Cargo.toml example block.
if ncb.info == "toml" && ncb.literal.starts_with(DEPENDENCIES) {
let mut content = DEPENDENCIES.to_string();
let mut version_parts = comrak::version().split('.').collect::<Vec<&str>>();
version_parts.pop();
write!(content, "\"{}\"", version_parts.join(".")).unwrap();
ncb.literal = content;
}
// Look for a console code block whose contents starts with the HELP string.
// Replace its contents with the same string and the actual command output.
if ncb.info == "console" && ncb.literal.starts_with(HELP) {
let mut content = HELP.to_string();
let mut cmd = std::process::Command::new("cargo");
content.push_str(
str::from_utf8(
&cmd.args(["run", "--all-features", "--", "--help"])
.output()
.unwrap()
.stdout,
)
.unwrap(),
);
ncb.literal = content;
}
}
});
let mut out = vec![];
format_commonmark(doc, &Options::default(), &mut out).unwrap();
std::fs::write("README.md", &out)?;
format_commonmark(doc, &Options::default(), &mut inp.writer())?;
inp.save()?;
Ok(())
}