mod kconfig;
mod lua;
use crate::bridge::Bridge;
use anyhow::{bail, Ok, Result};
use std::path::Path;
use colored::Colorize;
pub use kconfig::KConfig;
pub use lua::LuaScript;
pub trait Script {
fn apply(&self, bridge: &Bridge) -> Result<()>;
}
pub fn load(path: impl AsRef<Path>) -> Result<Box<dyn Script>> {
let ext = path
.as_ref()
.extension()
.or_else(|| path.as_ref().file_name())
.expect("Missing file extension")
.to_str()
.unwrap();
Ok(match ext {
"lua" => Box::new(LuaScript::new(path)?),
"txt" | "config" | ".config" => Box::new(KConfig::new(path)?),
_ => bail!(format!("Unknown script type {ext}")),
})
}
pub fn apply(path: impl AsRef<Path>, bridge: &Bridge) -> Result<()> {
println!("{:>12} script ({})", "Applying".green(), path.as_ref().display());
load(path)?.apply(bridge)
}