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

113 lines
2.9 KiB
Rust

use serde::Deserialize;
use url::Url;
// use git2::Repository;
use std::{
// fs::read_dir,
// path::PathBuf,
collections::HashSet,
sync::{
Mutex,
Arc
}
};
use crate::{
globals::{
BULB_CACHE_PATH
},
state::State,
actions,
errors::ErrorWrapper
};
use mlua::{
prelude::{
LuaResult,
LuaError,
LuaTable
},
Lua,
LuaSerdeExt
};
#[allow(dead_code)]
#[derive(Deserialize, Debug, Clone, Hash, Eq, PartialEq)]
pub struct Repo {
url: Url,
plugins: Vec<String>
}
#[derive(Deserialize, Debug)]
// #[serde(transparent)]
pub struct Config<'c> {
pub repos: Vec<Repo>,
pub clean_repos: bool,
pub install_optionals: bool,
#[serde(skip)]
pub plugins: Vec<&'c String>
}
pub struct ConfigLua<'lua> {
pub table: LuaTable<'lua>,
pub lua: &'lua Lua
}
impl<'lua> TryFrom<ConfigLua<'lua>> for Config<'_> {
type Error = LuaError;
fn try_from(config_lua: ConfigLua) -> LuaResult<Self> {
let config: Config = Lua::from_value(config_lua.lua, mlua::Value::Table(config_lua.table))?;
Ok(config)
}
}
impl<'c> Config<'c> {
pub fn parse(&'c mut self, state: State) -> LuaResult<()> {
let bulb_cache_path = BULB_CACHE_PATH.get_mut().unwrap();
// let state = STATE.lock().unwrap();
let state_repos_hash: HashSet<_> = state.repos.iter().collect();
let state_plugins_hash: HashSet<_> = state.plugins.iter().collect();
let config_repos_hash: HashSet<String> = self.repos.iter().map(|repo| repo.url.to_string()).collect();
let config_plugins_hash: HashSet<_>;
// let mut repo_clone = self.repos.clone();
let config_plugins_vec: Vec<&'c String> = self.repos.iter()
.map(|repo| &repo.plugins)
.flatten()
.collect();
self.plugins = config_plugins_vec.clone();
config_plugins_hash = config_plugins_vec.iter().collect();
let config_mutex = Arc::new(Mutex::new(&self));
/*actions::install::install("plugin1".to_owned(),
Url::parse("https://github.com/lite-xl/lite-xl-plugins").map_err(ErrorWrapper)?,
config_mutex)?;*/
// Remove duplicate repos from state
// for repo_config in self.repos {
// state_diff.repos.remove(state_diff.repos.iter().position(|repo| repo == repo_config.url.as_str()).unwrap());
// for plugin_config in repo_config.plugins {
// }
// }
Ok(())
}
pub fn new() -> Self {
Self {
repos: Vec::new(),
clean_repos: false,
install_optionals: false,
plugins: Vec::new()
}
}
}