Skip to content

Commit

Permalink
refactor: cleanup config.rs (#6)
Browse files Browse the repository at this point in the history
- use rs.ratatui.crates-tui as the project dirs
  name.
- hard code the CRATES_TUI_ prefix for environment
  variables
- use chained function instead of if else for
  default_config_dir and default_data_dir
- rearranged the functions in the file to make it
  easier to read
  • Loading branch information
joshka committed Feb 6, 2024
1 parent a527923 commit 868769a
Showing 1 changed file with 29 additions and 47 deletions.
76 changes: 29 additions & 47 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::PathBuf, sync::OnceLock};
use std::{env, path::PathBuf, sync::OnceLock};

use color_eyre::eyre::{eyre, Result};
use directories::ProjectDirs;
Expand Down Expand Up @@ -101,51 +101,6 @@ impl Default for Config {
}
}

/// Returns the directory to use for storing data files.
fn default_data_dir() -> PathBuf {
if let Some(dir) = std::env::var(format!(
"{}_DATA_HOME",
env!("CARGO_CRATE_NAME").to_uppercase()
))
.ok()
.map(PathBuf::from)
{
dir
} else if let Ok(dir) = project_dirs().map(|dirs| dirs.data_local_dir().to_path_buf()) {
dir
} else {
PathBuf::from(".").join(".data")
}
}

/// Returns the directory to use for storing config files.
fn default_config_dir() -> PathBuf {
if let Some(dir) = std::env::var(format!(
"{}_CONFIG_HOME",
env!("CARGO_CRATE_NAME").to_uppercase()
))
.ok()
.map(PathBuf::from)
{
dir
} else if let Ok(dir) = project_dirs().map(|dirs| dirs.config_local_dir().to_path_buf()) {
dir
} else {
PathBuf::from(".").join(".config")
}
}

/// Returns the path to the default configuration file.
fn default_config_file() -> PathBuf {
default_config_dir().join("config.toml")
}

/// Returns the project directories.
fn project_dirs() -> Result<ProjectDirs> {
ProjectDirs::from("com", "kdheepak", env!("CARGO_PKG_NAME"))
.ok_or_else(|| eyre!("user home directory not found"))
}

/// Initialize the application configuration.
///
/// This function should be called before any other function in the application.
Expand All @@ -160,7 +115,7 @@ pub fn init(cli: &Cli) -> Result<()> {
.merge(Serialized::defaults(Config::default()))
.merge(Toml::string(CONFIG_DEFAULT))
.merge(Toml::file(config_file))
.merge(Env::prefixed(concat!(env!("CARGO_CRATE_NAME"), "_")))
.merge(Env::prefixed("CRATES_TUI_"))
.merge(Serialized::defaults(cli))
.extract::<Config>()?;
CONFIG
Expand All @@ -178,3 +133,30 @@ pub fn init(cli: &Cli) -> Result<()> {
pub fn get() -> &'static Config {
CONFIG.get().expect("config not initialized")
}

/// Returns the path to the default configuration file.
fn default_config_file() -> PathBuf {
default_config_dir().join("config.toml")
}

/// Returns the directory to use for storing config files.
fn default_config_dir() -> PathBuf {
env::var("CRATES_TUI_CONFIG_HOME")
.map(PathBuf::from)
.or_else(|_| project_dirs().map(|dirs| dirs.config_local_dir().to_path_buf()))
.unwrap_or(PathBuf::from(".").join(".config"))
}

/// Returns the directory to use for storing data files.
fn default_data_dir() -> PathBuf {
env::var("CRATES_TUI_DATA_HOME")
.map(PathBuf::from)
.or_else(|_| project_dirs().map(|dirs| dirs.data_local_dir().to_path_buf()))
.unwrap_or(PathBuf::from(".").join(".data"))
}

/// Returns the project directories.
fn project_dirs() -> Result<ProjectDirs> {
ProjectDirs::from("rs", "ratatui", "crates-tui")
.ok_or_else(|| eyre!("user home directory not found"))
}

0 comments on commit 868769a

Please sign in to comment.