mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 13:00:37 +00:00
Make parsing of _VERSION global more robust
This commit is contained in:
parent
a52dfc1483
commit
9f58414e99
1 changed files with 28 additions and 13 deletions
|
@ -1,24 +1,39 @@
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
|
|
||||||
pub fn create(lua: &Lua) -> LuaResult<impl IntoLua<'_>> {
|
pub fn create(lua: &Lua) -> LuaResult<impl IntoLua<'_>> {
|
||||||
|
let lune_version = format!("Lune {}", env!("CARGO_PKG_VERSION"));
|
||||||
|
|
||||||
let luau_version_full = lua
|
let luau_version_full = lua
|
||||||
.globals()
|
.globals()
|
||||||
.get::<_, LuaString>("_VERSION")
|
.get::<_, LuaString>("_VERSION")
|
||||||
.expect("Missing _VERSION global");
|
.expect("Missing _VERSION global");
|
||||||
|
let luau_version_str = luau_version_full
|
||||||
|
.to_str()
|
||||||
|
.context("Invalid utf8 found in _VERSION global")?;
|
||||||
|
|
||||||
let luau_version = luau_version_full
|
// If this function runs more than once, we
|
||||||
.to_str()?
|
// may get an already formatted lune version.
|
||||||
.strip_prefix("Luau 0.")
|
if luau_version_str.starts_with(&lune_version) {
|
||||||
.expect("_VERSION global is formatted incorrectly")
|
return Ok(luau_version_full);
|
||||||
.trim();
|
|
||||||
|
|
||||||
if luau_version.is_empty() {
|
|
||||||
panic!("_VERSION global is missing version number")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lua.create_string(format!(
|
// Luau version is expected to be in the format "Luau 0.x" and sometimes "Luau 0.x.y"
|
||||||
"Lune {lune}+{luau}",
|
if !luau_version_str.starts_with("Luau 0.") {
|
||||||
lune = env!("CARGO_PKG_VERSION"),
|
panic!("_VERSION global is formatted incorrectly\nGot: '{luau_version_str}'")
|
||||||
luau = luau_version,
|
}
|
||||||
))
|
let luau_version = luau_version_str.strip_prefix("Luau 0.").unwrap().trim();
|
||||||
|
|
||||||
|
// We make some guarantees about the format of the _VERSION global,
|
||||||
|
// so make sure that the luau version also follows those rules.
|
||||||
|
if luau_version.is_empty() {
|
||||||
|
panic!("_VERSION global is missing version number\nGot: '{luau_version_str}'")
|
||||||
|
} else if !luau_version.chars().all(is_valid_version_char) {
|
||||||
|
panic!("_VERSION global contains invalid characters\nGot: '{luau_version_str}'")
|
||||||
|
}
|
||||||
|
|
||||||
|
lua.create_string(format!("{lune_version}+{luau_version}"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_valid_version_char(c: char) -> bool {
|
||||||
|
matches!(c, '0'..='9' | '.')
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue