dotune 0.2.0

A cross-platform dotfile manager configured with Lua
use std::sync::Arc;

use mlua::prelude::*;

use super::path::Path;
use crate::lua::lua_app_data::LuaAppData;
use crate::model::{AbsolutePath, Instruction, InstructionKind};

#[derive(LuaUserData)]
pub struct Dot {
    #[lua(get)]
    link: LuaFunction,
    #[lua(get)]
    copy: LuaFunction,
}

impl Dot {
    pub fn new(lua: &Lua, script_path: Arc<AbsolutePath>) -> LuaResult<Self> {
        let link_script_path = script_path.clone();
        let link =
            lua.create_function(move |lua, (source_path, destination_path): (Path, Path)| {
                LuaAppData::push_instruction(
                    lua,
                    Instruction {
                        defined_in: link_script_path.clone(),
                        kind: InstructionKind::Link {
                            source_path: source_path.0,
                            destination_path: destination_path.0,
                        },
                    },
                );
                Ok(())
            })?;

        let copy =
            lua.create_function(move |lua, (source_path, destination_path): (Path, Path)| {
                LuaAppData::push_instruction(
                    lua,
                    Instruction {
                        defined_in: script_path.clone(),
                        kind: InstructionKind::Copy {
                            source_path: source_path.0,
                            destination_path: destination_path.0,
                        },
                    },
                );
                Ok(())
            })?;

        Ok(Self { link, copy })
    }
}