use directories;
use std::env::current_dir;
use std::path::PathBuf;
use tokio::fs;
pub mod log {
pub use urs::log;
pub use urs::log::LogType::*;
}
pub enum ConfigDir {
RootDir,
KnifeDir,
LocalDir,
}
pub fn get_config_dir(d: ConfigDir) -> Option<PathBuf> {
let base_dir = directories::BaseDirs::new()?;
let root = base_dir.config_dir().join("autosway");
match d {
ConfigDir::RootDir => Some(root),
ConfigDir::KnifeDir => Some(root.join("knifes")),
ConfigDir::LocalDir => Some(if let Ok(p) = current_dir() {
p.join(".autosway")
} else {
log::log!(log::Error, "Failed to get local config directory");
std::process::exit(1)
}),
}
}
pub struct AutoswayLocalConfig {
pub knife_dir: PathBuf,
}
impl Default for AutoswayLocalConfig {
fn default() -> Self {
let local_dir = if let Some(p) = get_config_dir(ConfigDir::LocalDir) {
p
} else {
log::log!(log::Error, "Failed to local autosway directory");
std::process::exit(1)
};
let mut knife_dir = local_dir.join("knifes");
async {
if let Ok(f) = fs::File::open(local_dir.join("config")).await {
let mut f = if let Ok(f) = f.try_into_std() {
f
} else {
log::log!(log::Error, "Failed to convert async File to sync File");
std::process::exit(1)
};
if let Ok(c) = urs::misc::config::parse_config_file(&mut f) {
if let Some(dir) = c.get(&"knife_dir".to_string()) {
knife_dir = if let Ok(p) = current_dir() {
p.join(PathBuf::from(dir))
} else {
log::log!(log::Error, "");
std::process::exit(1)
}
}
}
}
};
Self { knife_dir }
}
}
pub struct Knife {
pub path: PathBuf,
pub name: String,
pub init: String,
}
pub async fn get_knifes() -> Vec<Knife> {
let mut knifes = vec![];
let push_knife = |path: PathBuf, knifes: &mut Vec<Knife>| {
let name = if let Some(name) = path.file_name() {
match name.to_owned().to_str() {
Some(s) => s.to_owned(),
None => return,
}
} else {
return;
};
let init = path.join("init.lua");
if init.exists() {
let init_str = match init.to_str() {
Some(s) => s.to_owned(),
None => return,
};
knifes.push(Knife {
path,
name,
init: init_str,
});
}
};
if let Some(knife_dir) = get_config_dir(ConfigDir::KnifeDir) {
let mut read = match fs::read_dir(knife_dir).await {
Ok(read_dir) => read_dir,
Err(_) => return knifes,
};
while let Ok(Some(entry)) = read.next_entry().await {
if entry.path().is_dir() {
push_knife(entry.path(), &mut knifes);
}
}
}
let local_dir = AutoswayLocalConfig::default().knife_dir;
let mut read = match fs::read_dir(local_dir).await {
Ok(read_dir) => read_dir,
Err(_) => return knifes,
};
while let Ok(Some(entry)) = read.next_entry().await {
if entry.path().is_dir() {
push_knife(entry.path(), &mut knifes);
}
}
knifes
}
#[derive(Default)]
pub struct StateOptions {
pub verbose: bool,
}
pub struct State {
pub lua: mlua::Lua,
pub knifes: Vec<Knife>,
pub options: StateOptions,
}
impl State {
pub async fn new() -> Self {
let mut state = Self {
lua: mlua::Lua::new(),
knifes: get_knifes().await,
options: StateOptions::default(),
};
match state.setup_lua().await {
Ok(()) => {}
Err(e) => {
log::log!(log::Error, "{e}");
std::process::exit(1);
}
}
state
}
}