diff options
| author | a-kenji <aks.kenji@protonmail.com> | 2021-04-27 09:23:52 +0200 |
|---|---|---|
| committer | a-kenji <aks.kenji@protonmail.com> | 2021-04-27 21:38:26 +0200 |
| commit | 77e0b5e0d7a44e9a23751eda8a61aed845974686 (patch) | |
| tree | 077f56665a33c519493d242e245a91caa3bcced1 /src/common/input/config.rs | |
| parent | fix(ci): formatting tweak (diff) | |
| download | sl0ppij-77e0b5e0d7a44e9a23751eda8a61aed845974686.tar.gz sl0ppij-77e0b5e0d7a44e9a23751eda8a61aed845974686.tar.xz sl0ppij-77e0b5e0d7a44e9a23751eda8a61aed845974686.zip | |
Config Loading
* Configuration is now handled in this order:
--config flag
ZELLIJ_CONFIG_FILE env
get_default_config_dir() (directories-next)
HOME/.config/zellij (for mac convenience)
SYSTEM_DEFAULT_CONFIG_DIR (for distributions to specify sensible
defaults ontop of zellij)
from assets
* Fix default.yaml file
* Move default.yaml file to assets directory
Diffstat (limited to 'src/common/input/config.rs')
| -rw-r--r-- | src/common/input/config.rs | 58 |
1 files changed, 39 insertions, 19 deletions
diff --git a/src/common/input/config.rs b/src/common/input/config.rs index 542116d9..62ec3dfe 100644 --- a/src/common/input/config.rs +++ b/src/common/input/config.rs @@ -7,8 +7,8 @@ use std::path::{Path, PathBuf}; use super::keybinds::{Keybinds, KeybindsFromYaml}; use crate::cli::ConfigCli; +use crate::common::install; -use directories_next::ProjectDirs; use serde::Deserialize; type ConfigResult = Result<Config, ConfigError>; @@ -33,6 +33,8 @@ pub enum ConfigError { Io(io::Error), // Io error with path context IoPath(io::Error, PathBuf), + // Internal Deserialisation Error + FromUtf8(std::string::FromUtf8Error), } impl Default for Config { @@ -63,18 +65,11 @@ impl Config { } } - /// Deserializes the config from a default platform specific path, - /// merges the default configuration - options take precedence. - fn from_default_path() -> ConfigResult { - let project_dirs = ProjectDirs::from("org", "Zellij Contributors", "Zellij").unwrap(); - let mut config_path: PathBuf = project_dirs.config_dir().to_owned(); - config_path.push("config.yaml"); - - match Config::new(&config_path) { - Ok(config) => Ok(config), - Err(ConfigError::IoPath(_, _)) => Ok(Config::default()), - Err(e) => Err(e), - } + /// Gets default configuration from assets + // TODO Deserialize the Configuration from bytes &[u8], + // once serde-yaml supports zero-copy + pub fn from_default_assets() -> ConfigResult { + Self::from_yaml(String::from_utf8(install::DEFAULT_CONFIG.to_vec())?.as_str()) } /// Entry point of the configuration @@ -82,20 +77,37 @@ impl Config { pub fn from_cli_config( location: Option<PathBuf>, cli_config: Option<ConfigCli>, + config_dir: Option<PathBuf>, ) -> ConfigResult { if let Some(path) = location { return Config::new(&path); } - match cli_config { - Some(ConfigCli::Config { clean, .. }) if clean => Ok(Config::default()), - Some(_) | None => Ok(Config::from_default_path()?), + if let Some(ConfigCli::Config { clean, .. }) = cli_config { + if clean { + return Config::from_default_assets(); + } + } + + if let Some(config) = config_dir { + let path = config.join("config.yaml"); + if path.exists() { + Config::new(&path) + } else { + Config::from_default_assets() + } + } else { + Config::from_default_assets() } } /// In order not to mess up tests from changing configurations #[cfg(test)] - pub fn from_cli_config(_: Option<PathBuf>, _: Option<ConfigCli>) -> ConfigResult { + pub fn from_cli_config( + _: Option<PathBuf>, + _: Option<ConfigCli>, + _: Option<PathBuf>, + ) -> ConfigResult { Ok(Config::default()) } } @@ -108,6 +120,7 @@ impl Display for ConfigError { write!(formatter, "IoError: {}, File: {}", err, path.display(),) } ConfigError::Serde(ref err) => write!(formatter, "Deserialisation error: {}", err), + ConfigError::FromUtf8(ref err) => write!(formatter, "FromUtf8Error: {}", err), } } } @@ -118,6 +131,7 @@ impl std::error::Error for ConfigError { ConfigError::Io(ref err) => Some(err), ConfigError::IoPath(ref err, _) => Some(err), ConfigError::Serde(ref err) => Some(err), + ConfigError::FromUtf8(ref err) => Some(err), } } } @@ -134,6 +148,12 @@ impl From<serde_yaml::Error> for ConfigError { } } +impl From<std::string::FromUtf8Error> for ConfigError { + fn from(err: std::string::FromUtf8Error) -> ConfigError { + ConfigError::FromUtf8(err) + } +} + // The unit test location. #[cfg(test)] mod config_test { @@ -142,14 +162,14 @@ mod config_test { #[test] fn clean_option_equals_default_config() { let cli_config = ConfigCli::Config { clean: true }; - let config = Config::from_cli_config(None, Some(cli_config)).unwrap(); + let config = Config::from_cli_config(None, Some(cli_config), None).unwrap(); let default = Config::default(); assert_eq!(config, default); } #[test] fn no_config_option_file_equals_default_config() { - let config = Config::from_cli_config(None, None).unwrap(); + let config = Config::from_cli_config(None, None, None).unwrap(); let default = Config::default(); assert_eq!(config, default); } |
