From 781d89e9bf5c7a4786a014cf823e35dd3818bb3d Mon Sep 17 00:00:00 2001 From: "Alexander Koz." <888526+boozook@users.noreply.github.com> Date: Wed, 5 Feb 2025 22:11:29 +0400 Subject: [PATCH] Pd-bindgen various fixes (#484) * move a test from bin to the bindgen lib * add min-max compatible (as "tested") version bounds (#483) --- Cargo.lock | 2 +- support/bindgen/Cargo.toml | 2 +- support/bindgen/src/lib.rs | 44 ++++++++++++++++++++++++++++++++----- support/bindgen/src/main.rs | 5 ----- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 81951cdd..5bc06710 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4138,7 +4138,7 @@ dependencies = [ [[package]] name = "playdate-bindgen" -version = "0.2.0" +version = "0.2.1" dependencies = [ "bindgen", "clap", diff --git a/support/bindgen/Cargo.toml b/support/bindgen/Cargo.toml index 1ccac54a..3abfda81 100644 --- a/support/bindgen/Cargo.toml +++ b/support/bindgen/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "playdate-bindgen" -version = "0.2.0" +version = "0.2.1" readme = "README.md" description = "Bindgen configuration for Playdate API and utils." keywords = ["playdate", "bindings", "ffi", "code-generation"] diff --git a/support/bindgen/src/lib.rs b/support/bindgen/src/lib.rs index 21ad9ef4..25409c97 100644 --- a/support/bindgen/src/lib.rs +++ b/support/bindgen/src/lib.rs @@ -19,8 +19,7 @@ pub mod gen; type Result = std::result::Result; -// TODO: We have to min-max bounds instead of just one semver-req. -pub const SDK_VER_SUPPORTED: &str = "^2.0.0"; // used for version validation. +pub const SDK_VER_SUPPORTED: &str = ">=2.0.0, <=3.0.0"; /// Generated Rust bindings. @@ -154,13 +153,21 @@ fn create_generator(cfg: cfg::Cfg) -> Result { fn check_sdk_version(version: &str) -> Result { + is_version_matches(version) + .map(|(ver, res, req)| { + if res { + println!("cargo:warning=Playdate SDK version not tested. Supported version '{req}' does not matches current '{ver}'.") + } + ver + }) +} + +fn is_version_matches(version: &str) -> Result<(semver::Version, bool, semver::VersionReq), error::Error> { let requirement = semver::VersionReq::parse(SDK_VER_SUPPORTED).expect("Builtin supported version requirement is invalid."); let version = semver::Version::parse(version.trim())?; - if !requirement.matches(&version) { - println!("cargo:warning=Playdate SDK version not tested. Supported version '{requirement}' does not matches current '{version}'."); - } - Ok(version) + let matches = requirement.matches(&version); + Ok((version, matches, requirement)) } @@ -437,3 +444,28 @@ pub fn rustfmt<'out>(mut rustfmt_path: Option, _ => Ok(source), } } + + +#[cfg(test)] +mod tests { + #[test] + fn same_env_var() { + assert_eq!(utils::consts::SDK_ENV_VAR, bindgen_cfg::Cfg::ENV_SDK_PATH); + } + + #[test] + fn is_version_matches() { + use super::is_version_matches as check; + + let map = |(_, res, _)| res; + + assert!(check("0.0").map(map).is_err()); + assert!(!check("0.0.0").map(map).unwrap()); + assert!(check("2.0.0").map(map).unwrap()); + assert!(check("2.6.0").map(map).unwrap()); + assert!(check("2.7.0").map(map).unwrap()); + assert!(!check("2.7.0-beta.3").map(map).unwrap()); + assert!(check("3.0.0").map(map).unwrap()); + assert!(!check("3.1.0").map(map).unwrap()); + } +} diff --git a/support/bindgen/src/main.rs b/support/bindgen/src/main.rs index 6574c41e..2d2240c2 100644 --- a/support/bindgen/src/main.rs +++ b/support/bindgen/src/main.rs @@ -81,9 +81,4 @@ mod tests { fn same_bin_name() { assert_eq!(env!("CARGO_BIN_NAME"), bindgen_cfg::BIN_NAME); } - - #[test] - fn same_env_var() { - assert_eq!(utils::consts::SDK_ENV_VAR, bindgen_cfg::Cfg::ENV_SDK_PATH); - } }