use std::fs;
use std::path::PathBuf;
use serde::Deserialize;
pub mod general;
use general::GeneralConfig;
pub mod github;
use github::GithubConfig;
pub mod tmuxinator;
use tmuxinator::TmuxinatorConfig;
pub mod fzf;
use fzf::FzfConfig;
pub mod git;
use git::GitConfig;
pub mod templates;
use templates::WorkspaceTemplate;
pub fn get_config() -> Option<WorkflowsConfig> {
let home_config_file = dirs::home_dir()?.join(".workflows.toml");
if home_config_file.is_file() {
return WorkflowsConfig::try_from(home_config_file).ok();
}
let config_dir_file = dirs::config_dir()?.join("workflows/").join("config.toml");
WorkflowsConfig::try_from(config_dir_file).ok()
}
#[derive(Debug, Deserialize, Default, Clone, PartialEq, Eq)]
#[serde(default)]
pub struct WorkflowsConfig {
general: Option<GeneralConfig>,
template: Option<Vec<WorkspaceTemplate>>,
github: Option<GithubConfig>,
git: Option<GitConfig>,
tmuxinator: Option<TmuxinatorConfig>,
fzf: Option<FzfConfig>,
}
impl WorkflowsConfig {
pub fn general(&self) -> GeneralConfig {
self.general.clone().unwrap_or_default()
}
pub fn templates(&self) -> Vec<WorkspaceTemplate> {
self.template.clone().unwrap_or_default()
}
pub fn fzf(&self) -> FzfConfig {
self.fzf.clone().unwrap_or_default()
}
pub fn git(&self) -> GitConfig {
self.git.clone().unwrap_or_default()
}
pub fn github(&self) -> GithubConfig {
self.github.clone().unwrap_or_default()
}
pub fn tmuxinator(&self) -> TmuxinatorConfig {
self.tmuxinator.clone().unwrap_or_default()
}
}
impl TryFrom<PathBuf> for WorkflowsConfig {
type Error = &'static str;
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
let toml_string = match fs::read_to_string(value) {
Ok(toml) => toml,
Err(_) => return Err("Couldn't read string"),
};
match toml::from_str(&toml_string) {
Ok(config) => Ok(config),
Err(_) => Err("Couldn't parse config from toml"),
}
}
}