Skip to content

Commit

Permalink
fixup! refactor(utilities): Rewrite semver in Rust and wrap as Lua type
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Oct 13, 2024
1 parent 56a063e commit 1aa3019
Showing 1 changed file with 43 additions and 27 deletions.
70 changes: 43 additions & 27 deletions src/types/semver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,48 @@ pub fn semver(version: String) -> crate::Result<Semver> {
Ok(Semver::new(&version)?)

Check failure on line 20 in src/types/semver.rs

View workflow job for this annotation

GitHub Actions / clippy

question mark operator is useless here

error: question mark operator is useless here --> src/types/semver.rs:20:5 | 20 | Ok(Semver::new(&version)?) | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `Semver::new(&version)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark = note: `-D clippy::needless-question-mark` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::needless_question_mark)]`
}

fn use_registered_metatable(lua: &Lua) -> LuaResult<LuaTable> {
let key = "semver_type_metatable";
let metatable: LuaTable = match lua.named_registry_value(key)? {
LuaValue::Table(metatable) => metatable,
LuaValue::Nil => {
let metatable = lua.create_table()?;
let to_string = lua.create_function(|_, luaself: LuaTable| {
let major: u8 = luaself.get("major")?;
let minor: u8 = luaself.get("minor")?;
let patch: u8 = luaself.get("patch")?;
Ok(format!("{}.{}.{}", major, minor, patch))
})?;
metatable.set("__tostring", to_string)?;
let equal = lua.create_function(|_, args: (LuaTable, LuaTable)| {
dbg!(args);
//let major: u8 = args.0.get("major")?;
//let minor: u8 = args.0.get("minor")?;
//let patch: u8 = args.0.get("patch")?;
Ok(false)
})?;
metatable.set("__eq", equal)?;
let less_equal = lua.create_function(|_, args: (LuaTable, LuaTable)| {
dbg!(args);
Ok(false)
})?;
metatable.set("__le", less_equal)?;
let less_than = lua.create_function(|_, args: (LuaTable, LuaTable)| {
dbg!(args);
Ok(false)
//let major1: u8 = args.0.get("major")?;
//let major2: u8 = args.1.get("major")?;
//Ok(major1 < major2)
})?;
metatable.set("__lt", less_than)?;
lua.set_named_registry_value(key, &metatable)?;
metatable
}
_ => panic!("Unexpected type return from registry lookup"),
};
Ok(metatable)
}

impl Deref for Semver {
type Target = Version;
fn deref(&self) -> &Version {
Expand All @@ -34,33 +76,7 @@ impl IntoLua for Semver {
semver.set("major", self.version.major)?;
semver.set("minor", self.version.minor)?;
semver.set("patch", self.version.patch)?;
let metatable = lua.create_table()?;
let to_string = lua.create_function(|_, luaself: LuaTable| {
let major: u8 = luaself.get("major")?;
let minor: u8 = luaself.get("minor")?;
let patch: u8 = luaself.get("patch")?;
Ok(format!("{}.{}.{}", major, minor, patch))
})?;
metatable.set("__tostring", to_string)?;
let equal = lua.create_function(|_, args: (LuaTable, LuaTable)| {
dbg!(args);
panic!("No passiy");
//let major: u8 = args.0.get("major")?;
//let minor: u8 = args.0.get("minor")?;
//let patch: u8 = args.0.get("patch")?;
Ok(false)
})?;
metatable.set("__eq", equal)?;
let less_equal = lua.create_function(|_, args: (LuaTable, LuaTable)| {
dbg!(args);
Ok(false)
})?;
metatable.set("__le", less_equal)?;
let less_than = lua.create_function(|_, args: (LuaTable, LuaTable)| {
dbg!(args);
Ok(false)
})?;
metatable.set("__lt", less_than)?;
let metatable: mlua::Table = use_registered_metatable(&lua)?;

Check failure on line 79 in src/types/semver.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> src/types/semver.rs:79:63 | 79 | let metatable: mlua::Table = use_registered_metatable(&lua)?; | ^^^^ help: change this to: `lua` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-D clippy::needless-borrow` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::needless_borrow)]`
semver.set_metatable(Some(metatable));
Ok(LuaValue::Table(semver))
}
Expand Down

0 comments on commit 1aa3019

Please sign in to comment.