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 14, 2024
1 parent 7bf8d12 commit b14b19b
Showing 1 changed file with 97 additions and 81 deletions.
178 changes: 97 additions & 81 deletions src/types/semver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,65 +15,65 @@ impl Semver {
}
}

// TODO: cfg gate this so it only ends up in Lua module?
//// TODO: cfg gate this so it only ends up in Lua module?
pub fn semver(version: String) -> crate::Result<Semver> {
Ok(Semver::new(&version)?)
}

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()?;
metatable.set(
"__tostring",
lua.create_function(|_, arg: LuaTable| {
let major: u16 = arg.get("major")?;
let minor: u16 = arg.get("minor")?;
let patch: u16 = arg.get("patch")?;
Ok(format!("{}.{}.{}", major, minor, patch))
})?,
)?;
metatable.set(
"__eq",
lua.create_function(|_, _args: (LuaTable, LuaTable)| {
//dbg!(args);
//let major: u16 = args.0.get("major")?;
//let minor: u16 = args.0.get("minor")?;
//let patch: u16 = args.0.get("patch")?;
Ok(false)
})?,
)?;
metatable.set(
"__le",
lua.create_function(|_, _args: (LuaTable, LuaTable)| {
//dbg!(args);
Ok(false)
})?,
)?;
metatable.set(
"__lt",
lua.create_function(|_, (a, b): (Semver, Semver)| {
//let major_is_less: bool =
// args.0.get::<u16>("major")? < args.1.get::<u16>("major")?;
//let m: Semver = args.0;
dbg!(a);
//let _minor_is_less: bool =
// args.0.get::<u16>("minor")? < args.1.get::<u16>("minor")?;
//let _patch_is_less: bool =
// args.0.get::<u16>("patch")? < args.1.get::<u16>("patch")?;
//Ok(major_is_less)
Ok(false)
})?,
)?;
lua.set_named_registry_value(key, &metatable)?;
metatable
}
_ => unreachable!(),
};
Ok(metatable)
}
//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()?;
// metatable.set(
// "__tostring",
// lua.create_function(|_, arg: LuaTable| {
// let major: u16 = arg.get("major")?;
// let minor: u16 = arg.get("minor")?;
// let patch: u16 = arg.get("patch")?;
// Ok(format!("{}.{}.{}", major, minor, patch))
// })?,
// )?;
// metatable.set(
// "__eq",
// lua.create_function(|_, _args: (LuaTable, LuaTable)| {
// //dbg!(args);
// //let major: u16 = args.0.get("major")?;
// //let minor: u16 = args.0.get("minor")?;
// //let patch: u16 = args.0.get("patch")?;
// Ok(false)
// })?,
// )?;
// metatable.set(
// "__le",
// lua.create_function(|_, _args: (LuaTable, LuaTable)| {
// //dbg!(args);
// Ok(false)
// })?,
// )?;
// metatable.set(
// "__lt",
// lua.create_function(|_, (a, b): (LuaTable, LuaTable)| {
// //let major_is_less: bool =
// // args.0.get::<u16>("major")? < args.1.get::<u16>("major")?;
// //let m: Semver = args.0;
// dbg!(a);
// //let _minor_is_less: bool =
// // args.0.get::<u16>("minor")? < args.1.get::<u16>("minor")?;
// //let _patch_is_less: bool =
// // args.0.get::<u16>("patch")? < args.1.get::<u16>("patch")?;
// //Ok(major_is_less)
// Ok(false)
// })?,
// )?;
// lua.set_named_registry_value(key, &metatable)?;
// metatable
// }
// _ => unreachable!(),
// };
// Ok(metatable)
//}

impl Deref for Semver {
type Target = Version;
Expand All @@ -82,33 +82,49 @@ impl Deref for Semver {
}
}

impl IntoLua for Semver {
#[inline]
fn into_lua(self, lua: &Lua) -> LuaResult<LuaValue> {
let semver = lua.create_table()?;
semver.set("major", self.version.major)?;
semver.set("minor", self.version.minor)?;
semver.set("patch", self.version.patch)?;
let metatable: mlua::Table = use_registered_metatable(&lua)?;
semver.set_metatable(Some(metatable));
Ok(LuaValue::Table(semver))
impl LuaUserData for Semver {
fn add_fields<F: LuaUserDataFields<Self>>(fields: &mut F) {
fields.add_field_method_get("major", |_, this| Ok(this.version.major));
fields.add_field_method_get("minor", |_, this| Ok(this.version.minor));
fields.add_field_method_get("patch", |_, this| Ok(this.version.patch));
}
}

impl FromLua for Semver {
#[inline]
fn from_lua(value: LuaValue, _: &Lua) -> LuaResult<Self> {
dbg!(value);
match value {
//LuaValue::UserData(ud) => {
// dbg!(&ud);
//}
LuaValue::Table(t) => {
dbg!(t);
}
_ => unreachable!(),
};
//let major = value.get("major")?;
Ok(Semver::new("4.6.8")?)
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
methods.add_meta_method("__tostring", |_, this, ()| Ok(this.to_string()));

//methods.add_meta_method(MetaMethod::Add, |_, this, value: i32| {
// Ok(this.0 + value)
//});
}
}

//impl IntoLua for Semver {
// #[inline]
// fn into_lua(self, lua: &Lua) -> LuaResult<LuaValue> {
// let semver = lua.create_table()?;
// semver.set("major", self.version.major)?;
// semver.set("minor", self.version.minor)?;
// semver.set("patch", self.version.patch)?;
// //let metatable: mlua::Table = use_registered_metatable(&lua)?;
// //semver.set_metatable(Some(metatable));
// Ok(LuaValue::Table(semver))
// }
//}

//impl FromLua for Semver {
// #[inline]
// fn from_lua(value: LuaValue, _: &Lua) -> LuaResult<Self> {
// dbg!(value);
// match value {
// //LuaValue::UserData(ud) => {
// // dbg!(&ud);
// //}
// LuaValue::Table(t) => {
// dbg!(t);
// }
// _ => unreachable!(),
// };
// //let major = value.get("major")?;
// Ok(Semver::new("4.6.8")?)
// }
//}

0 comments on commit b14b19b

Please sign in to comment.