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

66 lines
1.3 KiB
Rust

use mlua::prelude::*;
use url::Url;
use ron::de::from_bytes;
use crate::{
globals::BULB_STATE_PATH,
errors::RonError
};
use serde::{
Serialize,
Deserialize
};
use std::{
fs,
path::Path
};
#[derive(Serialize, Deserialize, Clone)]
pub struct State {
pub plugins: Vec<String>,
pub repos: Vec<Url>
}
impl State {
pub fn parse<P>(path: P) -> LuaResult<Self>
where
P: AsRef<Path>
{
let data = fs::read(path)?;
// let mut manifest: Manifest = serde_json::from_slice(data.as_slice()).map_err(JsonError)?;
let state = from_bytes(data.as_slice()).map_err(RonError)?;
Ok(state)
}
pub fn new() -> Self {
Self {
plugins: Vec::new(),
repos: Vec::new()
}
}
pub fn clean(&self) {
fs::write(
BULB_STATE_PATH.lock().unwrap().to_owned(),
ron::to_string(self).unwrap()
).expect("Couldn't write state");
}
// pub fn clean_lua(_: &Lua, this: &Self, (): ()) -> LuaResult<()> {
// this.clean();
// Ok(())
// }
}
// impl LuaUserData for State {
// fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
// methods.add_method("clean", State::clean_lua)
// }
// }
impl Drop for State {
fn drop(&mut self) {
self.clean();
}
}