From 23eeaa7ba485883ed0e6dca830cc862f5a4e8b22 Mon Sep 17 00:00:00 2001 From: Max Heller Date: Tue, 2 Jan 2024 19:08:26 -0500 Subject: [PATCH] fix: correctly check mdBook version compatibility --- src/lib.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d03de53..5e6c11b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ use std::{ use anyhow::{anyhow, Context as _}; use mdbook::config::HtmlConfig; +use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; mod book; @@ -53,14 +54,27 @@ impl mdbook::Renderer for Renderer { } fn render(&self, ctx: &mdbook::renderer::RenderContext) -> anyhow::Result<()> { - let compiled_mdbook_version = semver::VersionReq::parse(mdbook::MDBOOK_VERSION).unwrap(); + // If we're compiled against mdbook version I.J.K, require ^I.J + // This allows using a version of mdbook with an earlier patch version as a server + static MDBOOK_VERSION_REQ: Lazy = Lazy::new(|| { + let compiled_mdbook_version = semver::Version::parse(mdbook::MDBOOK_VERSION).unwrap(); + semver::VersionReq { + comparators: vec![semver::Comparator { + op: semver::Op::Caret, + major: compiled_mdbook_version.major, + minor: Some(compiled_mdbook_version.minor), + patch: None, + pre: Default::default(), + }], + } + }); let mdbook_server_version = semver::Version::parse(&ctx.version).unwrap(); - if !compiled_mdbook_version.matches(&mdbook_server_version) { + if !MDBOOK_VERSION_REQ.matches(&mdbook_server_version) { log::warn!( - "{} is semver-incompatible with mdbook {} (compiled against {})", + "{} is semver-incompatible with mdbook {} (requires {})", env!("CARGO_PKG_NAME"), mdbook_server_version, - compiled_mdbook_version, + *MDBOOK_VERSION_REQ, ); }