From c67f0ddf34b630c538caf6b6d4f6e801a6c0bcb6 Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Fri, 7 Jul 2017 10:35:33 -0700 Subject: [PATCH] Move config loading logic to config.rs Config loading was complexity that shouldn't be in main. --- src/config.rs | 38 +++++++++++++++++++++++++++++++++++++- src/main.rs | 46 ++++++++++------------------------------------ 2 files changed, 47 insertions(+), 37 deletions(-) diff --git a/src/config.rs b/src/config.rs index 7734e7af..957557f9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1123,7 +1123,43 @@ impl Config { self.hide_cursor_when_typing } - pub fn load_from>(path: P) -> Result { + /// Load configuration + /// + /// If a configuration file is given as a command line argument we don't + /// generate a default file. If an empty configuration file is given, i.e. + /// /dev/null, we load the compiled-in defaults. + pub fn load(options: &::cli::Options) -> Config { + let config_path = options.config_path() + .or_else(|| Config::installed_config()) + .unwrap_or_else(|| { + Config::write_defaults() + .unwrap_or_else(|err| die!("Write defaults config failure: {}", err)) + }); + + Config::load_from(&*config_path) + .map(|config| { + if let Some(path) = config.path().as_ref() { + info!("Config loaded from {}", path.display()); + } + + config + }) + .unwrap_or_else(|err| { + use self::Error::*; + match err { + NotFound => { + die!("Config file not found at: {}", config_path.display()); + }, + Empty => { + err_println!("Empty config; Loading defaults"); + Config::default() + }, + _ => die!("{}", err), + } + }) + } + + fn load_from>(path: P) -> Result { let path = path.into(); let raw = Config::read_file(path.as_path())?; let mut config: Config = serde_yaml::from_str(&raw)?; diff --git a/src/main.rs b/src/main.rs index f2a5109b..fcff9239 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,55 +37,29 @@ use alacritty::tty::{self, process_should_exit}; use alacritty::util::fmt::Red; fn main() { - // Load command line options and config - let options = cli::Options::load(); - let config = load_config(&options); - // Run alacritty - if let Err(err) = run(config, options) { - die!("Alacritty encountered an unrecoverable error:\n\n\t{}\n", Red(err)); + if let Err(err) = run() { + die!("Encountered an unrecoverable error:\n\n\t{}\n", Red(err)); } info!("Goodbye."); } -/// Load configuration -/// -/// If a configuration file is given as a command line argument we don't -/// generate a default file. If an empty configuration file is given, i.e. -/// /dev/null, we load the compiled-in defaults. -fn load_config(options: &cli::Options) -> Config { - let config_path = options.config_path() - .or_else(|| Config::installed_config()) - .unwrap_or_else(|| { - Config::write_defaults() - .unwrap_or_else(|err| die!("Write defaults config failure: {}", err)) - }); - - Config::load_from(&*config_path).unwrap_or_else(|err| { - match err { - config::Error::NotFound => { - die!("Config file not found at: {}", config_path.display()); - }, - config::Error::Empty => { - err_println!("Empty config; Loading defaults"); - Config::default() - }, - _ => die!("{}", err), - } - }) -} - /// Run Alacritty /// /// Creates a window, the terminal state, pty, I/O event loop, input processor, /// config change monitor, and runs the main display loop. -fn run(mut config: Config, options: cli::Options) -> Result<(), Box> { - // Initialize the logger first as to capture output from other subsystems - logging::initialize(&options)?; +fn run() -> Result<(), Box> { + // Load command line options + let options = cli::Options::load(); + // Initialize the logger ASAP + logging::initialize(&options)?; info!("Welcome to Alacritty."); + // Load config + let mut config = Config::load(&options); + // Create a display. // // The display manages a window and can draw the terminal