Skip to content

Commit

Permalink
Pd-bindgen various fixes (#484)
Browse files Browse the repository at this point in the history
* move a test from bin to the bindgen lib

* add min-max compatible (as "tested") version bounds (#483)
  • Loading branch information
boozook authored Feb 5, 2025
1 parent 66da849 commit 781d89e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion support/bindgen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
44 changes: 38 additions & 6 deletions support/bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ pub mod gen;
type Result<T, E = error::Error> = std::result::Result<T, E>;


// 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.
Expand Down Expand Up @@ -154,13 +153,21 @@ fn create_generator(cfg: cfg::Cfg) -> Result<Generator, error::Error> {


fn check_sdk_version(version: &str) -> Result<semver::Version, error::Error> {
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))
}


Expand Down Expand Up @@ -437,3 +444,28 @@ pub fn rustfmt<'out>(mut rustfmt_path: Option<PathBuf>,
_ => 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());
}
}
5 changes: 0 additions & 5 deletions support/bindgen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 781d89e

Please sign in to comment.