diff --git a/src/app/layout_manager.rs b/src/app/layout_manager.rs index e7904c9a..e125726f 100644 --- a/src/app/layout_manager.rs +++ b/src/app/layout_manager.rs @@ -1,9 +1,6 @@ use std::collections::BTreeMap; -use crate::{ - constants::DEFAULT_WIDGET_ID, - error::{BottomError, Result}, -}; +use crate::{constants::DEFAULT_WIDGET_ID, options::OptionError}; /// Represents a more usable representation of the layout, derived from the /// config. @@ -985,9 +982,9 @@ impl BottomWidgetType { } impl std::str::FromStr for BottomWidgetType { - type Err = BottomError; + type Err = OptionError; - fn from_str(s: &str) -> Result { + fn from_str(s: &str) -> Result { let lower_case = s.to_lowercase(); match lower_case.as_str() { "cpu" => Ok(BottomWidgetType::Cpu), @@ -1002,8 +999,8 @@ impl std::str::FromStr for BottomWidgetType { _ => { #[cfg(feature = "battery")] { - Err(BottomError::ConfigError(format!( - "\"{s}\" is an invalid widget name. + Err(OptionError::config(format!( + "'{s}' is an invalid widget name. Supported widget names: +--------------------------+ @@ -1028,8 +1025,8 @@ Supported widget names: } #[cfg(not(feature = "battery"))] { - Err(BottomError::ConfigError(format!( - "\"{s}\" is an invalid widget name. + Err(OptionError::config(format!( + "'{s}' is an invalid widget name. Supported widget names: +--------------------------+ diff --git a/src/canvas.rs b/src/canvas.rs index 9b587b32..547a3e13 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -22,7 +22,8 @@ use crate::{ App, }, constants::*, - utils::{error, error::BottomError}, + options::OptionError, + utils::error, }; #[derive(Debug)] @@ -37,9 +38,9 @@ pub enum ColourScheme { } impl FromStr for ColourScheme { - type Err = BottomError; + type Err = OptionError; - fn from_str(s: &str) -> error::Result { + fn from_str(s: &str) -> Result { let lower_case = s.to_lowercase(); match lower_case.as_str() { "default" => Ok(ColourScheme::Default), @@ -48,8 +49,8 @@ impl FromStr for ColourScheme { "gruvbox-light" => Ok(ColourScheme::GruvboxLight), "nord" => Ok(ColourScheme::Nord), "nord-light" => Ok(ColourScheme::NordLight), - _ => Err(BottomError::ConfigError(format!( - "`{s}` is an invalid built-in color scheme." + _ => Err(OptionError::other(format!( + "'{s}' is an invalid built-in color scheme." ))), } } diff --git a/src/canvas/dialogs/dd_dialog.rs b/src/canvas/dialogs/dd_dialog.rs index ff41df4a..225c95f8 100644 --- a/src/canvas/dialogs/dd_dialog.rs +++ b/src/canvas/dialogs/dd_dialog.rs @@ -182,19 +182,19 @@ impl Painter { { if to_kill_processes.1.len() != 1 { Line::from(format!( - "Kill {} processes with the name \"{}\"? Press ENTER to confirm.", + "Kill {} processes with the name '{}'? Press ENTER to confirm.", to_kill_processes.1.len(), to_kill_processes.0 )) } else { Line::from(format!( - "Kill 1 process with the name \"{}\"? Press ENTER to confirm.", + "Kill 1 process with the name '{}'? Press ENTER to confirm.", to_kill_processes.0 )) } } else { Line::from(format!( - "Kill process \"{}\" with PID {}? Press ENTER to confirm.", + "Kill process '{}' with PID {}? Press ENTER to confirm.", to_kill_processes.0, first_pid )) }, diff --git a/src/canvas/styling.rs b/src/canvas/styling.rs index 53ff120b..a77f88df 100644 --- a/src/canvas/styling.rs +++ b/src/canvas/styling.rs @@ -1,12 +1,15 @@ mod colour_utils; -use anyhow::Context; use colour_utils::*; use tui::style::{Color, Style}; use super::ColourScheme; pub use crate::options::ConfigV1; -use crate::{constants::*, options::colours::ColoursConfig, utils::error}; +use crate::{ + constants::*, + options::{colours::ColoursConfig, OptionError, OptionResult}, + utils::error, +}; pub struct CanvasStyling { pub currently_selected_text_colour: Color, @@ -98,11 +101,12 @@ impl Default for CanvasStyling { macro_rules! try_set_colour { ($field:expr, $colours:expr, $colour_field:ident) => { if let Some(colour_str) = &$colours.$colour_field { - $field = str_to_fg(colour_str).context(concat!( - "update '", - stringify!($colour_field), - "' in your config file" - ))?; + $field = str_to_fg(colour_str).map_err(|err| { + OptionError::config(format!( + "Please update 'colors.{}' in your config file. {err}", + stringify!($colour_field) + )) + })?; } }; } @@ -113,12 +117,13 @@ macro_rules! try_set_colour_list { $field = colour_list .iter() .map(|s| str_to_fg(s)) - .collect::>>() - .context(concat!( - "update '", - stringify!($colour_field), - "' in your config file" - ))?; + .collect::, String>>() + .map_err(|err| { + OptionError::config(format!( + "Please update 'colors.{}' in your config file. {err}", + stringify!($colour_field) + )) + })?; } }; } @@ -154,7 +159,7 @@ impl CanvasStyling { Ok(canvas_colours) } - pub fn set_colours_from_palette(&mut self, colours: &ColoursConfig) -> anyhow::Result<()> { + pub fn set_colours_from_palette(&mut self, colours: &ColoursConfig) -> OptionResult<()> { // CPU try_set_colour!(self.avg_colour_style, colours, avg_cpu_color); try_set_colour!(self.all_colour_style, colours, all_cpu_color); @@ -201,12 +206,12 @@ impl CanvasStyling { if let Some(scroll_entry_text_color) = &colours.selected_text_color { self.set_scroll_entry_text_color(scroll_entry_text_color) - .context("update 'selected_text_color' in your config file")?; + .map_err(|_| OptionError::invalid_config_value("selected_text_color"))? } if let Some(scroll_entry_bg_color) = &colours.selected_bg_color { self.set_scroll_entry_bg_color(scroll_entry_bg_color) - .context("update 'selected_bg_color' in your config file")?; + .map_err(|_| OptionError::invalid_config_value("selected_bg_color"))? } Ok(()) diff --git a/src/canvas/styling/colour_utils.rs b/src/canvas/styling/colour_utils.rs index 1c1def3e..367fb6d8 100644 --- a/src/canvas/styling/colour_utils.rs +++ b/src/canvas/styling/colour_utils.rs @@ -3,8 +3,6 @@ use itertools::Itertools; use tui::style::{Color, Style}; use unicode_segmentation::UnicodeSegmentation; -use crate::utils::error; - pub const FIRST_COLOUR: Color = Color::LightMagenta; pub const SECOND_COLOUR: Color = Color::LightYellow; pub const THIRD_COLOUR: Color = Color::LightCyan; @@ -16,19 +14,16 @@ pub const AVG_COLOUR: Color = Color::Red; pub const ALL_COLOUR: Color = Color::Green; /// Convert a hex string to a colour. -fn convert_hex_to_color(hex: &str) -> error::Result { - fn hex_component_to_int(hex: &str, first: &str, second: &str) -> error::Result { - u8::from_str_radix(&concat_string!(first, second), 16).map_err(|_| { - error::BottomError::ConfigError(format!( - "\"{hex}\" is an invalid hex color, could not decode." - )) - }) +fn convert_hex_to_color(hex: &str) -> Result { + fn hex_component_to_int(hex: &str, first: &str, second: &str) -> Result { + u8::from_str_radix(&concat_string!(first, second), 16) + .map_err(|_| format!("'{hex}' is an invalid hex color, could not decode.")) } - fn invalid_hex_format(hex: &str) -> error::BottomError { - error::BottomError::ConfigError(format!( - "\"{hex}\" is an invalid hex color. It must be either a 7 character hex string of the form \"#12ab3c\" or a 3 character hex string of the form \"#1a2\".", - )) + fn invalid_hex_format(hex: &str) -> String { + format!( + "'{hex}' is an invalid hex color. It must be either a 7 character hex string of the form '#12ab3c' or a 3 character hex string of the form '#1a2'.", + ) } if !hex.starts_with('#') { @@ -55,11 +50,11 @@ fn convert_hex_to_color(hex: &str) -> error::Result { } } -pub fn str_to_fg(input_val: &str) -> error::Result