Skip to content

Commit

Permalink
[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 a65dc01 commit 73f9cb9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
3 changes: 2 additions & 1 deletion rusile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use mlua::prelude::*;
fn rusile(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table().unwrap();
exports.set("demo", LuaFunction::wrap_raw(sile::rusile_demo))?;
exports.set("foo", LuaFunction::wrap_raw(sile::types::semver::foo))?;
let x = sile::types::semver::Semver::new("3.2.1-rc0+foo")?;
exports.set("x", x)?;
Ok(exports)
}
44 changes: 41 additions & 3 deletions src/types/semver.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
use mlua::prelude::*;
use semver::Version;
use std::ops::Deref;

pub fn foo() -> crate::Result<()> {
eprintln!("Run");
Ok(())
#[derive(Debug)]
pub struct Semver {
pub version: Option<Version>,
}

impl Semver {
pub fn new(version: &str) -> crate::Result<Self> {
Ok(Self {
version: Some(Version::parse(version)?),
})
}
}

pub fn semver(version: &str) -> crate::Result<Semver> {
Ok(Semver::new(version)?)

Check failure on line 19 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:19:5 | 19 | 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)]`
}

impl Deref for Semver {
type Target = Option<Version>;
fn deref(&self) -> &Option<Version> {
&self.version
}
}

impl IntoLua for Semver {
#[inline]
fn into_lua(self, lua: &Lua) -> LuaResult<LuaValue> {
let semver = lua.create_table()?;
let mt = lua.create_table()?;
let ts = lua.create_function(|_, luaself: LuaTable| {
dbg!(luaself);
Ok("apply of")
})?;
mt.set("__tostring", ts)?;
semver.set_metatable(Some(mt));
let dodad = lua.create_string("abra kad").unwrap();
semver.set("dodad", dodad).unwrap();
Ok(LuaValue::Table(semver))
}
}

0 comments on commit 73f9cb9

Please sign in to comment.