Skip to content

Commit

Permalink
fixup! [NFY] Wrap semver::Version as Lua type
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Oct 11, 2024
1 parent 73f9cb9 commit 607fe4c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
3 changes: 2 additions & 1 deletion rusile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use mlua::prelude::*;

#[mlua::lua_module]
fn rusile(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table().unwrap();
let exports = lua.create_table()?;
exports.set("demo", LuaFunction::wrap_raw(sile::rusile_demo))?;
let x = sile::types::semver::Semver::new("3.2.1-rc0+foo")?;
exports.set("x", x)?;
//exports.set("semver", LuaFunction::wrap_raw(sile::types::semver::semver))?;
Ok(exports)
}
21 changes: 14 additions & 7 deletions src/types/semver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use std::ops::Deref;

#[derive(Debug)]
pub struct Semver {
pub version: Option<Version>,
pub version: Version,
}

impl Semver {
pub fn new(version: &str) -> crate::Result<Self> {
Ok(Self {
version: Some(Version::parse(version)?),
version: Version::parse(version)?,
})
}
}
Expand All @@ -20,8 +20,8 @@ pub fn semver(version: &str) -> crate::Result<Semver> {
}

impl Deref for Semver {
type Target = Option<Version>;
fn deref(&self) -> &Option<Version> {
type Target = Version;
fn deref(&self) -> &Version {
&self.version
}
}
Expand All @@ -33,12 +33,19 @@ impl IntoLua for Semver {
let mt = lua.create_table()?;
let ts = lua.create_function(|_, luaself: LuaTable| {
dbg!(luaself);
Ok("apply of")
Ok("stahoeu")
//Ok(format!(
// "{}.{}.{}",
// luaself.get("major")?,
// luaself.get("minor")?,
// luaself.get("patch")?
//))
})?;
mt.set("__tostring", ts)?;
semver.set_metatable(Some(mt));
let dodad = lua.create_string("abra kad").unwrap();
semver.set("dodad", dodad).unwrap();
semver.set("major", self.version.major)?;
semver.set("minor", self.version.minor)?;
semver.set("patch", self.version.patch)?;
Ok(LuaValue::Table(semver))
}
}

0 comments on commit 607fe4c

Please sign in to comment.