-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodinfo.rs
49 lines (45 loc) · 1.45 KB
/
modinfo.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use crate::download_mod;
use mlua::prelude::{LuaResult, LuaValue};
use mlua::{FromLua, IntoLua, Lua};
#[derive(Debug, Clone)]
pub struct ModInfo {
pub url: String,
pub id: String,
pub name: String,
pub description: Vec<String>,
pub version: String,
pub authors: Vec<String>,
}
impl IntoLua<'_> for ModInfo {
fn into_lua(self, lua: &Lua) -> LuaResult<LuaValue> {
let table = lua.create_table()?;
let download_mod = self.clone();
let download_func = lua.create_function(move |lua, ()| download_mod.download(lua))?;
table.set("url", self.url)?;
table.set("id", self.id)?;
table.set("name", self.name)?;
table.set("description", self.description)?;
table.set("version", self.version)?;
table.set("authors", self.authors)?;
table.set("download", download_func)?;
Ok(LuaValue::Table(table))
}
}
impl FromLua<'_> for ModInfo {
fn from_lua(value: LuaValue, _: &'_ Lua) -> LuaResult<Self> {
let table = value.as_table().expect("Expected table");
Ok(ModInfo {
url: table.get("url")?,
id: table.get("id")?,
name: table.get("name")?,
description: table.get("description")?,
version: table.get("version")?,
authors: table.get("authors")?,
})
}
}
impl ModInfo {
pub fn download(&self, lua: &Lua) -> LuaResult<()> {
download_mod(lua, self.clone())
}
}