bulb/src/lib.rs
2022-12-01 15:25:08 +01:00

106 lines
2.4 KiB
Rust

mod actions;
mod config;
mod state;
mod manifest;
mod globals;
mod errors;
use mlua::prelude::*;
use std::{
fs::{
self,
File
},
// path::{
// Path,
// PathBuf
// }
};
use crate::{
config::{
ConfigLua,
Config
},
state::State,
globals::*
};
fn init(lua: &Lua, config_table: LuaTable) -> LuaResult<()> {
let bulb_state_path = BULB_STATE_PATH.get_mut().unwrap();
let config_lua = ConfigLua {
table: config_table,
lua
};
// let manifest = Manifest::parse(
// "/home/delta/.config/lite-xl/plugins/bulb/manifest.json",
// Url::parse("https://github.com/lite-xl/lite-xl-plugins").map_err(ErrorWrapper)?
// )?;
let state = State::parse(bulb_state_path.to_owned())?;
let mut config = Config::try_from(config_lua)?;
config.parse(state);
Ok(())
// Ok(format!("{:#?}", config))
}
#[mlua::lua_module]
fn libbulb(lua: &Lua) -> LuaResult<LuaTable> {
println!("1");
let lua_globals = lua.globals();
let user_dir: String = lua_globals.get("USERDIR")?;
let mut bulb_path = BULB_PATH.lock().unwrap();
let mut bulb_cache_path = BULB_CACHE_PATH.lock().unwrap();
let mut bulb_state_path = BULB_STATE_PATH.lock().unwrap();
let mut mod_version = MOD_VERSION.lock().unwrap();
// Jank:tm:
// Taken from https://stackoverflow.com/a/30527289
fn string_to_static_str(s: String) -> &'static str {
Box::leak(s.into_boxed_str())
}
bulb_path.push(user_dir);
bulb_path.push("bulb");
bulb_cache_path.push(bulb_path.clone());
bulb_cache_path.push(".cache");
bulb_state_path.push(bulb_path.clone());
bulb_state_path.push(".state.ron");
*mod_version = string_to_static_str(lua_globals.get("MOD_VERSION")?);
println!("2");
//*state = State::parse(bulb_state_path.to_owned())?;
println!("3");
if !bulb_path.exists() {
fs::create_dir(bulb_path.to_owned())?;
}
if !bulb_state_path.exists() {
File::create(bulb_state_path.to_owned())?;
fs::write(bulb_state_path.to_owned(), "(plugins:[],repos:[])")?;
}
if !bulb_cache_path.exists() {
fs::create_dir(bulb_cache_path.to_owned())?;
}
let bulb = lua.create_table()?;
bulb.set("init", lua.create_function(init)?)?;
// bulb.set("state", lua.create_userdata(*state)?)?;
println!("4");
Ok(bulb)
}