From f7eedaf77574cf04afb8d3891b9070d9d6bdef9c Mon Sep 17 00:00:00 2001 From: Matan Kushner Date: Mon, 15 Jul 2019 18:42:35 -0400 Subject: [PATCH] refactor: Improve logging where lacking --- src/config.rs | 59 +++++++++++++++++++++++++++++++++++---- src/modules/git_status.rs | 14 ++++++++-- 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/config.rs b/src/config.rs index 4b28d69c..f2045a68 100644 --- a/src/config.rs +++ b/src/config.rs @@ -24,7 +24,7 @@ impl Config { let file_path = match env::var("STARSHIP_CONFIG") { Ok(path) => { // Use $STARSHIP_CONFIG as the config path if available - log::debug!("STARSHIP_CONFIG is set: {}", &path); + log::debug!("STARSHIP_CONFIG is set: \n{}", &path); path } Err(_) => { @@ -50,7 +50,7 @@ impl Config { }?; let config = toml::from_str(&toml_content).ok()?; - log::debug!("Config found: \n{:?}", &config); + log::debug!("Config parsed: \n{:?}", &config); Some(config) } @@ -61,26 +61,75 @@ impl Config { .get(module_name) .map(toml::Value::as_table) .unwrap_or(None); - log::debug!("Config found for {}: {:?}", &module_name, &module_config); + + if module_config.is_some() { + log::debug!( + "Config found for \"{}\": \n{:?}", + &module_name, + &module_config + ); + } else { + log::trace!("No config found for \"{}\"", &module_name); + } + module_config } } /// Extends `toml::value::Table` with useful methods pub trait TableExt { + fn get_config(&self, key: &str) -> Option<&toml::value::Value>; fn get_as_bool(&self, key: &str) -> Option; fn get_as_str(&self, key: &str) -> Option<&str>; } impl TableExt for toml::value::Table { + /// Get the config value for a given key + fn get_config(&self, key: &str) -> Option<&toml::value::Value> { + log::trace!("Looking for config key \"{}\"", key); + let config_value = self.get(key); + + if config_value.is_some() { + log::trace!("Config found for \"{}\": {:?}", key, &config_value); + } else { + log::trace!("No value found for \"{}\"", key); + } + + config_value + } + /// Get a key from a module's configuration as a boolean fn get_as_bool(&self, key: &str) -> Option { - self.get(key).map(toml::Value::as_bool).unwrap_or(None) + let value = self.get_config(key)?; + let bool_value = value.as_bool(); + + if bool_value.is_none() { + log::debug!( + "Expected \"{}\" to be a boolean. Instead received {} of type {}.", + key, + value, + value.type_str() + ); + } + + bool_value } /// Get a key from a module's configuration as a string fn get_as_str(&self, key: &str) -> Option<&str> { - self.get(key).map(toml::Value::as_str).unwrap_or(None) + let value = self.get_config(key)?; + let str_value = value.as_str(); + + if str_value.is_none() { + log::debug!( + "Expected \"{}\" to be a string. Instead received {} of type {}.", + key, + value, + value.type_str() + ); + } + + str_value } } diff --git a/src/modules/git_status.rs b/src/modules/git_status.rs index 912ce7d0..b56de363 100644 --- a/src/modules/git_status.rs +++ b/src/modules/git_status.rs @@ -41,9 +41,19 @@ pub fn module<'a>(context: &'a Context) -> Option> { module.set_style(module_style); let ahead_behind = get_ahead_behind(&repository, &branch_name); - log::debug!("Repo ahead/behind: {:?}", ahead_behind); + if ahead_behind != Ok((0, 0)) { + log::debug!("Repo ahead/behind: {:?}", ahead_behind); + } else { + log::trace!("No ahead/behind found"); + } + let stash_object = repository.revparse_single("refs/stash"); - log::debug!("Stash object: {:?}", stash_object); + if stash_object.is_ok() { + log::debug!("Stash object: {:?}", stash_object); + } else { + log::trace!("No stash object found"); + } + let repo_status = get_repo_status(&repository); log::debug!("Repo status: {:?}", repo_status);