zoi/pkg/lua/
parser.rs

1use super::functions;
2use crate::{pkg::types, utils};
3use anyhow::{Result, anyhow};
4use mlua::{self, Lua, LuaSerdeExt, Table, Value};
5use std::fs;
6
7pub fn parse_lua_package_for_platform(
8    file_path: &str,
9    platform: &str,
10    version_override: Option<&str>,
11) -> Result<types::Package> {
12    let lua_code = fs::read_to_string(file_path)?;
13    let lua = Lua::new();
14
15    let pkg_meta_table = lua.create_table().map_err(|e| anyhow!(e.to_string()))?;
16    let pkg_deps_table = lua.create_table().map_err(|e| anyhow!(e.to_string()))?;
17    let pkg_updates_table = lua.create_table().map_err(|e| anyhow!(e.to_string()))?;
18    let pkg_hooks_table = lua.create_table().map_err(|e| anyhow!(e.to_string()))?;
19    lua.globals()
20        .set("__ZoiPackageMeta", pkg_meta_table)
21        .map_err(|e| anyhow!(e.to_string()))?;
22    lua.globals()
23        .set("__ZoiPackageDeps", pkg_deps_table)
24        .map_err(|e| anyhow!(e.to_string()))?;
25    lua.globals()
26        .set("__ZoiPackageUpdates", pkg_updates_table)
27        .map_err(|e| anyhow!(e.to_string()))?;
28    lua.globals()
29        .set("__ZoiPackageHooks", pkg_hooks_table)
30        .map_err(|e| anyhow!(e.to_string()))?;
31
32    let pkg_table = lua.create_table().map_err(|e| anyhow!(e.to_string()))?;
33    lua.globals()
34        .set("PKG", pkg_table)
35        .map_err(|e| anyhow!(e.to_string()))?;
36
37    functions::setup_lua_environment(&lua, platform, version_override, Some(file_path), None)
38        .map_err(|e| anyhow!(e.to_string()))?;
39
40    lua.load(&lua_code)
41        .exec()
42        .map_err(|e| anyhow!(e.to_string()))?;
43
44    let final_pkg_meta: Table = lua
45        .globals()
46        .get("__ZoiPackageMeta")
47        .map_err(|e| anyhow!(e.to_string()))?;
48    let final_pkg_deps: Table = lua
49        .globals()
50        .get("__ZoiPackageDeps")
51        .map_err(|e| anyhow!(e.to_string()))?;
52    let final_pkg_updates: Table = lua
53        .globals()
54        .get("__ZoiPackageUpdates")
55        .map_err(|e| anyhow!(e.to_string()))?;
56    let final_pkg_hooks: Table = lua
57        .globals()
58        .get("__ZoiPackageHooks")
59        .map_err(|e| anyhow!(e.to_string()))?;
60
61    let mut package: types::Package = lua
62        .from_value(Value::Table(final_pkg_meta))
63        .map_err(|e| anyhow!(e.to_string()))?;
64
65    package.dependencies = if final_pkg_deps.is_empty() {
66        None
67    } else {
68        Some(
69            lua.from_value(Value::Table(final_pkg_deps))
70                .map_err(|e| anyhow!(e.to_string()))?,
71        )
72    };
73
74    package.updates = if final_pkg_updates.is_empty() {
75        None
76    } else {
77        Some(
78            lua.from_value(Value::Table(final_pkg_updates))
79                .map_err(|e| anyhow!(e.to_string()))?,
80        )
81    };
82
83    package.hooks = if final_pkg_hooks.is_empty() {
84        None
85    } else {
86        Some(
87            lua.from_value(Value::Table(final_pkg_hooks))
88                .map_err(|e| anyhow!(e.to_string()))?,
89        )
90    };
91
92    Ok(package)
93}
94
95pub fn parse_lua_package(
96    file_path: &str,
97    version_override: Option<&str>,
98) -> Result<types::Package> {
99    let platform = utils::get_platform()?;
100    parse_lua_package_for_platform(file_path, &platform, version_override)
101}