diff options
author | a-kenji <aks.kenji@protonmail.com> | 2021-11-03 19:58:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-03 19:58:40 +0100 |
commit | f62ccd2a5ce281e710fd315f48d783efef9ea417 (patch) | |
tree | 621d806079c99cb9b09b3f2f25b156377c88c271 /zellij-utils/src | |
parent | 55c5b640ed80b025d65787e4c0fd83e54fe99891 (diff) |
add: `try_from` for `ConfigFromYaml` -> `Config` (#835)
Diffstat (limited to 'zellij-utils/src')
-rw-r--r-- | zellij-utils/src/input/config.rs | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/zellij-utils/src/input/config.rs b/zellij-utils/src/input/config.rs index ecfe64c7c..66104016c 100644 --- a/zellij-utils/src/input/config.rs +++ b/zellij-utils/src/input/config.rs @@ -5,7 +5,7 @@ use std::fs::File; use std::io::{self, Read}; use std::path::{Path, PathBuf}; -use serde::{Deserialize, Serialize}; +use serde::Deserialize; use std::convert::{TryFrom, TryInto}; use super::keybinds::{Keybinds, KeybindsFromYaml}; @@ -20,7 +20,7 @@ const DEFAULT_CONFIG_FILE_NAME: &str = "config.yaml"; type ConfigResult = Result<Config, ConfigError>; /// Intermediate deserialization config struct -#[derive(Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize)] pub struct ConfigFromYaml { #[serde(flatten)] pub options: Option<Options>, @@ -31,7 +31,7 @@ pub struct ConfigFromYaml { } /// Main configuration. -#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +#[derive(Debug, Clone, PartialEq, Deserialize)] pub struct Config { pub keybinds: Keybinds, pub options: Options, @@ -106,7 +106,8 @@ impl TryFrom<&CliArgs> for Config { impl Config { /// Uses defaults, but lets config override them. pub fn from_yaml(yaml_config: &str) -> ConfigResult { - let config_from_yaml: Option<ConfigFromYaml> = match serde_yaml::from_str(yaml_config) { + let maybe_config_from_yaml: Option<ConfigFromYaml> = match serde_yaml::from_str(yaml_config) + { Err(e) => { // needs direct check, as `[ErrorImpl]` is private // https://github.com/dtolnay/serde-yaml/issues/121 @@ -118,20 +119,9 @@ impl Config { Ok(config) => config, }; - match config_from_yaml { + match maybe_config_from_yaml { None => Ok(Config::default()), - Some(config) => { - let keybinds = Keybinds::get_default_keybinds_with_config(config.keybinds); - let options = Options::from_yaml(config.options); - let themes = config.themes; - let plugins = PluginsConfig::get_plugins_with_default(config.plugins.try_into()?); - Ok(Config { - keybinds, - options, - plugins, - themes, - }) - } + Some(config) => config.try_into(), } } @@ -157,6 +147,23 @@ impl Config { } } +impl TryFrom<ConfigFromYaml> for Config { + type Error = ConfigError; + + fn try_from(config_from_yaml: ConfigFromYaml) -> ConfigResult { + let keybinds = Keybinds::get_default_keybinds_with_config(config_from_yaml.keybinds); + let options = Options::from_yaml(config_from_yaml.options); + let themes = config_from_yaml.themes; + let plugins = PluginsConfig::get_plugins_with_default(config_from_yaml.plugins.try_into()?); + Ok(Self { + keybinds, + options, + plugins, + themes, + }) + } +} + // TODO: Split errors up into separate modules #[derive(Debug, Clone)] pub struct LayoutNameInTabError; |