Merge pull request #1511 from joshrotenberg/example-semver

Use semver crate to validate version compatibility in nop-preprocessor example
This commit is contained in:
Eric Huss 2021-05-24 11:10:21 -07:00 committed by GitHub
commit 3db0c0b9a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 3 deletions

21
Cargo.lock generated
View File

@ -1,5 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.18"
@ -818,6 +820,7 @@ dependencies = [
"pulldown-cmark",
"regex",
"select",
"semver",
"serde",
"serde_derive",
"serde_json",
@ -1360,6 +1363,24 @@ dependencies = [
"markup5ever_rcdom",
]
[[package]]
name = "semver"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6"
dependencies = [
"semver-parser",
]
[[package]]
name = "semver-parser"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7"
dependencies = [
"pest",
]
[[package]]
name = "serde"
version = "1.0.125"

View File

@ -49,6 +49,7 @@ ammonia = { version = "3", optional = true }
[dev-dependencies]
select = "0.5"
semver = "0.11.0"
pretty_assertions = "0.6"
walkdir = "2.0"

View File

@ -3,6 +3,7 @@ use clap::{App, Arg, ArgMatches, SubCommand};
use mdbook::book::Book;
use mdbook::errors::Error;
use mdbook::preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext};
use semver::{Version, VersionReq};
use std::io;
use std::process;
@ -33,9 +34,10 @@ fn main() {
fn handle_preprocessing(pre: &dyn Preprocessor) -> Result<(), Error> {
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?;
if ctx.mdbook_version != mdbook::MDBOOK_VERSION {
// We should probably use the `semver` crate to check compatibility
// here...
let book_version = Version::parse(&ctx.mdbook_version)?;
let version_req = VersionReq::parse(mdbook::MDBOOK_VERSION)?;
if version_req.matches(&book_version) != true {
eprintln!(
"Warning: The {} plugin was built against version {} of mdbook, \
but we're being called from version {}",