Modify config error handling

pull/1803/head
Joseph Harrison-Lim 3 years ago
parent 3676e2e5d9
commit 7b74c43ebb
No known key found for this signature in database
GPG Key ID: 5DAB214D5E62987D

@ -269,7 +269,7 @@ impl Application {
} }
fn refresh_config(&mut self) { fn refresh_config(&mut self) {
let config = Config::load(helix_loader::config_file()).unwrap(); let config = Config::load(helix_loader::config_file()).unwrap_or_default();
// Just an example to start; Some config properties like "theme" are a bit more involved and require a reload // Just an example to start; Some config properties like "theme" are a bit more involved and require a reload
if let Some(theme) = config.theme.clone() { if let Some(theme) = config.theme.clone() {
let true_color = self.true_color(); let true_color = self.true_color();

@ -1,7 +1,8 @@
use crate::keymap::{merge_keys, Keymaps}; use crate::keymap::{merge_keys, Keymaps};
use anyhow::{Error, Result};
use serde::Deserialize; use serde::Deserialize;
use std::io::Error as IOError;
use std::path::PathBuf; use std::path::PathBuf;
use toml::de::Error as TomlError;
#[derive(Debug, Default, Clone, PartialEq, Deserialize)] #[derive(Debug, Default, Clone, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
@ -15,6 +16,12 @@ pub struct Config {
pub editor: helix_view::editor::Config, pub editor: helix_view::editor::Config,
} }
#[derive(Debug)]
pub enum ConfigLoadError {
BadConfig(TomlError),
Error(IOError),
}
#[derive(Debug, Default, Clone, PartialEq, Deserialize)] #[derive(Debug, Default, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)] #[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct LspConfig { pub struct LspConfig {
@ -22,22 +29,18 @@ pub struct LspConfig {
} }
impl Config { impl Config {
pub fn load(config_path: PathBuf) -> Result<Config, Error> { pub fn load(config_path: PathBuf) -> Result<Config, ConfigLoadError> {
match std::fs::read_to_string(config_path) { match std::fs::read_to_string(config_path) {
Ok(config) => Ok(toml::from_str(&config) Ok(config) => toml::from_str(&config)
.map(merge_keys) .map(merge_keys)
.unwrap_or_else(|err| { .map_err(ConfigLoadError::BadConfig),
eprintln!("Bad config: {}", err); Err(err) => Err(ConfigLoadError::Error(err)),
eprintln!("Press <ENTER> to continue with default config");
use std::io::Read;
// This waits for an enter press.
let _ = std::io::stdin().read(&mut []);
Config::default()
})),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Result::Ok(Config::default()),
Err(err) => Err(err.into()),
} }
} }
pub fn load_default() -> Result<Config, ConfigLoadError> {
Config::load(helix_loader::config_file())
}
} }
#[cfg(test)] #[cfg(test)]

@ -1,7 +1,7 @@
use anyhow::{Context, Result}; use anyhow::{Context, Result, Error};
use helix_term::application::Application; use helix_term::application::Application;
use helix_term::args::Args; use helix_term::args::Args;
use helix_term::config::Config; use helix_term::config::{Config, ConfigLoadError};
use std::path::PathBuf; use std::path::PathBuf;
fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> { fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> {
@ -117,7 +117,25 @@ FLAGS:
std::fs::create_dir_all(&conf_dir).ok(); std::fs::create_dir_all(&conf_dir).ok();
} }
let config = Config::load(helix_loader::config_file())?; let config = match Config::load_default() {
Ok(config) => config,
Err(err) => {
match err {
ConfigLoadError::BadConfig(err) => {
eprintln!("Bad config: {}", err);
eprintln!("Press <ENTER> to continue with default config");
use std::io::Read;
// This waits for an enter press.
let _ = std::io::stdin().read(&mut []);
Config::default()
}
ConfigLoadError::Error(err) if err.kind() == std::io::ErrorKind::NotFound => {
Config::default()
}
ConfigLoadError::Error(err) => return Err(Error::new(err)),
}
}
};
setup_logging(logpath, args.verbosity).context("failed to initialize logging")?; setup_logging(logpath, args.verbosity).context("failed to initialize logging")?;

Loading…
Cancel
Save