From 3c43c20c6ae53a9aa6b67770b0c489806784f4ac Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Sun, 16 Oct 2022 12:58:11 +0100 Subject: [PATCH] fix: weird behaviour when config does not exist --- src/config.rs | 17 +++++++++++++---- src/main.rs | 15 +++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/config.rs b/src/config.rs index 03ce274..d7b2460 100644 --- a/src/config.rs +++ b/src/config.rs @@ -15,6 +15,7 @@ use serde::Deserialize; use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::{env, fs}; +use tracing::instrument; #[derive(Debug, Deserialize, Clone)] #[serde(tag = "type", rename_all = "kebab-case")] @@ -69,7 +70,7 @@ impl BarPosition { } } -#[derive(Debug, Deserialize, Clone, Default)] +#[derive(Debug, Deserialize, Clone)] pub struct Config { #[serde(default = "default_bar_position")] pub position: BarPosition, @@ -96,14 +97,18 @@ const fn default_bar_height() -> i32 { impl Config { /// Attempts to load the config file from file, /// parse it and return a new instance of `Self`. + #[instrument] pub fn load() -> Result { let config_path = if let Ok(config_path) = env::var("IRONBAR_CONFIG") { let path = PathBuf::from(config_path); if path.exists() { Ok(path) } else { - Err(Report::msg("Specified config file does not exist") - .note("Config file was specified using `IRONBAR_CONFIG` environment variable")) + Err(Report::msg(format!( + "Specified config file does not exist: {}", + path.display() + )) + .note("Config file was specified using `IRONBAR_CONFIG` environment variable")) } } else { Self::try_find_config() @@ -116,6 +121,7 @@ impl Config { /// by checking each valid format's extension. /// /// Returns the path of the first valid match, if any. + #[instrument] fn try_find_config() -> Result { let config_dir = config_dir().wrap_err("Failed to locate user config dir")?; @@ -135,7 +141,10 @@ impl Config { match file { Some(file) => Ok(file), - None => Err(Report::msg("Could not find config file")), + None => Err(Report::msg("Could not find config file") + .suggestion("Ironbar does not include a configuration out of the box") + .suggestion("A guide on writing a config can be found on the wiki:") + .suggestion("https://github.com/JakeStanger/ironbar/wiki/configuration-guide")), } } diff --git a/src/main.rs b/src/main.rs index 4721282..8997859 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,13 @@ use wayland::WaylandClient; const VERSION: &str = env!("CARGO_PKG_VERSION"); +#[repr(i32)] +enum ExitCode { + ErrGtkDisplay = 1, + ErrCreateBars = 2, + ErrConfig = 3 +} + #[tokio::main] async fn main() -> Result<()> { // Disable backtraces by default @@ -58,7 +65,7 @@ async fn main() -> Result<()> { || { let report = Report::msg("Failed to get default GTK display"); error!("{:?}", report); - exit(1) + exit(ExitCode::ErrGtkDisplay as i32) }, |display| display, ); @@ -67,14 +74,14 @@ async fn main() -> Result<()> { Ok(config) => config, Err(err) => { error!("{:?}", err); - Config::default() + exit(ExitCode::ErrConfig as i32) } }; debug!("Loaded config file"); if let Err(err) = create_bars(app, &display, wayland_client, &config) { error!("{:?}", err); - exit(2); + exit(ExitCode::ErrCreateBars as i32); } debug!("Created bars"); @@ -83,7 +90,7 @@ async fn main() -> Result<()> { || { let report = Report::msg("Failed to locate user config dir"); error!("{:?}", report); - exit(3); + exit(ExitCode::ErrCreateBars as i32); }, |dir| dir.join("ironbar").join("style.css"), );