Skip to content

Commit

Permalink
Allow version regex to match 4.0.stable.arch_linux
Browse files Browse the repository at this point in the history
  • Loading branch information
ttencate committed Mar 11, 2023
1 parent 54cb201 commit a2e9bce
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions godot-codegen/src/godot_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use regex::Regex;
use std::error::Error;

#[derive(Debug, Eq, PartialEq)]
pub struct GodotVersion {
/// the original string (trimmed, stripped of text around)
pub full_string: String,
Expand All @@ -27,26 +28,31 @@ pub struct GodotVersion {
}

pub fn parse_godot_version(version_str: &str) -> Result<GodotVersion, Box<dyn Error>> {
let regex = Regex::new(
// major minor [patch] official|custom_build|gentoo
// v v v v
r#"(\d+)\.(\d+)(?:\.(\d+))?\.(alpha|beta|dev|rc|stable)[0-9]*\.(?:mono\.)?(?:[a-z_]+\.([a-f0-9]+)|official)"#,
)?;
let regex = Regex::new(r#"(?x) # ignore whitespace and allow line comments (starting with `#`)
^
(?P<major>\d+)
\.(?P<minor>\d+)
(?:\.(?P<patch>\d+))?
\.(?P<stability>alpha|beta|dev|rc|stable)[0-9]*
(\.[^.]+)+? # mono|official|custom_build|gentoo|arch_linux|...
(?:\.(?P<custom_rev>[a-f0-9]+))?
$
"#)?;

let fail = || format!("Version substring cannot be parsed: `{version_str}`");
let caps = regex.captures(version_str).ok_or_else(fail)?;

Ok(GodotVersion {
full_string: caps.get(0).unwrap().as_str().to_string(),
major: caps.get(1).ok_or_else(fail)?.as_str().parse::<u8>()?,
minor: caps.get(2).ok_or_else(fail)?.as_str().parse::<u8>()?,
major: caps.name("major").ok_or_else(fail)?.as_str().parse::<u8>()?,
minor: caps.name("minor").ok_or_else(fail)?.as_str().parse::<u8>()?,
patch: caps
.get(3)
.name("patch")
.map(|m| m.as_str().parse::<u8>())
.transpose()?
.unwrap_or(0),
stability: caps.get(4).ok_or_else(fail)?.as_str().to_string(),
custom_rev: caps.get(5).map(|m| m.as_str().to_string()),
stability: caps.name("stability").ok_or_else(fail)?.as_str().to_string(),
custom_rev: caps.name("custom_rev").map(|m| m.as_str().to_string()),
})
}

Expand All @@ -72,23 +78,26 @@ fn test_godot_versions() {
("4.0.alpha.custom_build.faddbcfc0", 4, 0, 0, "alpha", s("faddbcfc0")),
("4.0.beta8.mono.custom_build.b28ddd918", 4, 0, 0, "beta", s("b28ddd918")),
("4.0.rc1.official.8843d9ad3", 4, 0, 0, "rc", s("8843d9ad3")),
("4.0.stable.arch_linux", 4, 0, 0, "stable", None),
];

let bad_versions = [
"4.0.unstable.custom_build.e7e9e663b", // 'unstable'
"4.0.3.custom_build.e7e9e663b", // no stability
"3.stable.official.206ba70f4", // no minor
"4.0.alpha.custom_build", // no rev after 'custom_build' (this is allowed for 'official' however)
];

for (full, major, minor, patch, stability, custom_rev) in good_versions {
let expected = GodotVersion {
full_string: full.to_owned(),
major,
minor,
patch,
stability: stability.to_owned(),
custom_rev,
};
let parsed: GodotVersion = parse_godot_version(full).unwrap();
assert_eq!(parsed.major, major);
assert_eq!(parsed.minor, minor);
assert_eq!(parsed.patch, patch);
assert_eq!(parsed.stability, stability);
assert_eq!(parsed.custom_rev, custom_rev);
assert_eq!(parsed.full_string, full);
assert_eq!(parsed, expected);
}

for full in bad_versions {
Expand Down

0 comments on commit a2e9bce

Please sign in to comment.