add: try_from for ConfigFromYaml -> Config (#835)
This commit is contained in:
parent
55c5b640ed
commit
f62ccd2a5c
1 changed files with 24 additions and 17 deletions
|
|
@ -5,7 +5,7 @@ use std::fs::File;
|
||||||
use std::io::{self, Read};
|
use std::io::{self, Read};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::Deserialize;
|
||||||
use std::convert::{TryFrom, TryInto};
|
use std::convert::{TryFrom, TryInto};
|
||||||
|
|
||||||
use super::keybinds::{Keybinds, KeybindsFromYaml};
|
use super::keybinds::{Keybinds, KeybindsFromYaml};
|
||||||
|
|
@ -20,7 +20,7 @@ const DEFAULT_CONFIG_FILE_NAME: &str = "config.yaml";
|
||||||
type ConfigResult = Result<Config, ConfigError>;
|
type ConfigResult = Result<Config, ConfigError>;
|
||||||
|
|
||||||
/// Intermediate deserialization config struct
|
/// Intermediate deserialization config struct
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
pub struct ConfigFromYaml {
|
pub struct ConfigFromYaml {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub options: Option<Options>,
|
pub options: Option<Options>,
|
||||||
|
|
@ -31,7 +31,7 @@ pub struct ConfigFromYaml {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Main configuration.
|
/// Main configuration.
|
||||||
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
|
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub keybinds: Keybinds,
|
pub keybinds: Keybinds,
|
||||||
pub options: Options,
|
pub options: Options,
|
||||||
|
|
@ -106,7 +106,8 @@ impl TryFrom<&CliArgs> for Config {
|
||||||
impl Config {
|
impl Config {
|
||||||
/// Uses defaults, but lets config override them.
|
/// Uses defaults, but lets config override them.
|
||||||
pub fn from_yaml(yaml_config: &str) -> ConfigResult {
|
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) => {
|
Err(e) => {
|
||||||
// needs direct check, as `[ErrorImpl]` is private
|
// needs direct check, as `[ErrorImpl]` is private
|
||||||
// https://github.com/dtolnay/serde-yaml/issues/121
|
// https://github.com/dtolnay/serde-yaml/issues/121
|
||||||
|
|
@ -118,20 +119,9 @@ impl Config {
|
||||||
Ok(config) => config,
|
Ok(config) => config,
|
||||||
};
|
};
|
||||||
|
|
||||||
match config_from_yaml {
|
match maybe_config_from_yaml {
|
||||||
None => Ok(Config::default()),
|
None => Ok(Config::default()),
|
||||||
Some(config) => {
|
Some(config) => config.try_into(),
|
||||||
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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -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
|
// TODO: Split errors up into separate modules
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct LayoutNameInTabError;
|
pub struct LayoutNameInTabError;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue