mirror of
https://github.com/lune-org/lune.git
synced 2025-04-10 21:40:54 +01:00
Add version string to lune-utils crate
This commit is contained in:
parent
3b3fa65bfe
commit
121afae726
4 changed files with 71 additions and 0 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1644,6 +1644,7 @@ name = "lune-utils"
|
||||||
version = "0.8.3"
|
version = "0.8.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"mlua",
|
"mlua",
|
||||||
|
"once_cell",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -12,3 +12,5 @@ workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
mlua = { version = "0.9.7", features = ["async"] }
|
mlua = { version = "0.9.7", features = ["async"] }
|
||||||
|
|
||||||
|
once_cell = "1.17"
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#![allow(clippy::cargo_common_metadata)]
|
#![allow(clippy::cargo_common_metadata)]
|
||||||
|
|
||||||
mod table_builder;
|
mod table_builder;
|
||||||
|
mod version_string;
|
||||||
|
|
||||||
pub use self::table_builder::TableBuilder;
|
pub use self::table_builder::TableBuilder;
|
||||||
|
pub use self::version_string::get_version_string;
|
||||||
|
|
66
crates/lune-utils/src/version_string.rs
Normal file
66
crates/lune-utils/src/version_string.rs
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use mlua::prelude::*;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
|
static VERSION_STRING: Lazy<Arc<String>> = Lazy::new(create_version_string);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the current Lune version string, in the format `Lune x.y.z+luau`.
|
||||||
|
|
||||||
|
This version string is strongly guaranteed to follow the above
|
||||||
|
format and may safely be used for parsing & version comparisons.
|
||||||
|
*/
|
||||||
|
#[must_use]
|
||||||
|
pub fn get_version_string() -> Arc<String> {
|
||||||
|
Arc::clone(&VERSION_STRING)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_version_string() -> Arc<String> {
|
||||||
|
// Extract the current Luau version from a fresh Lua state / VM that can't be accessed externally.
|
||||||
|
let luau_version_full = {
|
||||||
|
let temp_lua = Lua::new();
|
||||||
|
|
||||||
|
let luau_version_full = temp_lua
|
||||||
|
.globals()
|
||||||
|
.get::<_, LuaString>("_VERSION")
|
||||||
|
.expect("Missing _VERSION global");
|
||||||
|
|
||||||
|
luau_version_full
|
||||||
|
.to_str()
|
||||||
|
.context("Invalid utf8 found in _VERSION global")
|
||||||
|
.expect("Expected _VERSION global to be a string")
|
||||||
|
.to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Luau version is expected to be in the format "Luau 0.x" and sometimes "Luau 0.x.y"
|
||||||
|
assert!(
|
||||||
|
luau_version_full.starts_with("Luau 0."),
|
||||||
|
"_VERSION global is formatted incorrectly\
|
||||||
|
\nFound string '{luau_version_full}'"
|
||||||
|
);
|
||||||
|
let luau_version_noprefix = luau_version_full.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_noprefix.is_empty() {
|
||||||
|
panic!(
|
||||||
|
"_VERSION global is missing version number\
|
||||||
|
\nFound string '{luau_version_full}'"
|
||||||
|
)
|
||||||
|
} else if !luau_version_noprefix.chars().all(is_valid_version_char) {
|
||||||
|
panic!(
|
||||||
|
"_VERSION global contains invalid characters\
|
||||||
|
\nFound string '{luau_version_full}'"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(format!(
|
||||||
|
"Lune {}+{luau_version_noprefix}",
|
||||||
|
env!("CARGO_PKG_VERSION")
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_valid_version_char(c: char) -> bool {
|
||||||
|
matches!(c, '0'..='9' | '.')
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue