From 0c86a97ab44906221613bb13014f574936808349 Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Sun, 1 Jun 2025 10:12:44 +0200 Subject: [PATCH] remove redundant args error --- worf/src/lib/config.rs | 23 ++++++++--------------- worf/src/lib/gui.rs | 22 ++++++++++++++-------- worf/src/mod.rs | 9 ++++++--- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/worf/src/lib/config.rs b/worf/src/lib/config.rs index 696bdc2..eee8c05 100644 --- a/worf/src/lib/config.rs +++ b/worf/src/lib/config.rs @@ -3,7 +3,6 @@ use std::{env, fs, path::PathBuf, str::FromStr}; use clap::{Parser, ValueEnum}; use serde::{Deserialize, Serialize}; use serde_json::Value; -use thiserror::Error; use crate::Error; @@ -111,12 +110,6 @@ impl FromStr for Layer { } } -#[derive(Debug, Error)] -pub enum ArgsError { - #[error("input is not valid {0}")] - InvalidParameter(String), -} - impl FromStr for Anchor { type Err = String; @@ -132,7 +125,7 @@ impl FromStr for Anchor { } impl FromStr for Mode { - type Err = ArgsError; + type Err = Error; fn from_str(s: &str) -> Result { match s { @@ -144,7 +137,7 @@ impl FromStr for Mode { "ssh" => Ok(Mode::Ssh), "emoji" => Ok(Mode::Emoji), "auto" => Ok(Mode::Auto), - _ => Err(ArgsError::InvalidParameter( + _ => Err(Error::InvalidArgument( format!("{s} is not a valid argument, see help for details").to_owned(), )), } @@ -152,14 +145,14 @@ impl FromStr for Mode { } impl FromStr for WrapMode { - type Err = ArgsError; + type Err = Error; fn from_str(s: &str) -> Result { match s { "none" => Ok(WrapMode::None), "word" => Ok(WrapMode::Word), "inherit" => Ok(WrapMode::Inherit), - _ => Err(ArgsError::InvalidParameter( + _ => Err(Error::InvalidArgument( format!("{s} is not a valid argument, see help for details").to_owned(), )), } @@ -167,13 +160,13 @@ impl FromStr for WrapMode { } impl FromStr for SortOrder { - type Err = ArgsError; + type Err = Error; fn from_str(s: &str) -> Result { match s { "alphabetical" => Ok(SortOrder::Alphabetical), "default" => Ok(SortOrder::Default), - _ => Err(ArgsError::InvalidParameter( + _ => Err(Error::InvalidArgument( format!("{s} is not a valid argument, see help for details").to_owned(), )), } @@ -181,13 +174,13 @@ impl FromStr for SortOrder { } impl FromStr for KeyDetectionType { - type Err = ArgsError; + type Err = Error; fn from_str(s: &str) -> Result { match s { "value" => Ok(KeyDetectionType::Value), "code" => Ok(KeyDetectionType::Code), - _ => Err(ArgsError::InvalidParameter( + _ => Err(Error::InvalidArgument( format!("{s} is not a valid argument, see help for details").to_owned(), )), } diff --git a/worf/src/lib/gui.rs b/worf/src/lib/gui.rs index 5ef1f3f..a79eefd 100644 --- a/worf/src/lib/gui.rs +++ b/worf/src/lib/gui.rs @@ -1160,7 +1160,9 @@ fn sort_menu_items_by_score( } fn window_show_resize(config: &Config, ui: &Rc>) { - let Some(geometry) = get_monitor_geometry(ui) else { return }; + let Some(geometry) = get_monitor_geometry(ui) else { + return; + }; // Calculate target width from config, return early if not set let Some(target_width) = percent_or_absolute(&config.width(), geometry.width()) else { @@ -1188,7 +1190,11 @@ fn window_show_resize(config: &Config, ui: &Rc } } -fn calculate_dynamic_lines_window_height(config: &Config, ui: &UiElements, geometry: Rectangle) -> i32 { +fn calculate_dynamic_lines_window_height( + config: &Config, + ui: &UiElements, + geometry: Rectangle, +) -> i32 { if config.dynamic_lines_limit() { calculate_row_height(ui, visible_row_count(ui), config) .min(percent_or_absolute(&config.height(), geometry.height()).unwrap_or(0)) @@ -1231,7 +1237,7 @@ fn calculate_row_height( if baseline > 0 { let factor = if lines > 1 { 1.4 // todo find a better way to do this - // most likely it will not work with all styles + // most likely it will not work with all styles } else { 1.0 }; @@ -1277,7 +1283,7 @@ fn visible_row_count(ui: &UiElements) -> i32 { .filter(|(_, menu)| menu.visible) .count(), ) - .unwrap_or(i32::MAX) + .unwrap_or(i32::MAX) } fn handle_selected_item( @@ -1416,10 +1422,10 @@ fn create_menu_row( element_to_add.icon_path.as_ref().map(AsRef::as_ref), &meta.config, ) - .or(lookup_icon( - label_img.as_ref().map(AsRef::as_ref), - &meta.config, - )); + .or(lookup_icon( + label_img.as_ref().map(AsRef::as_ref), + &meta.config, + )); if let Some(image) = img { image.set_widget_name("img"); diff --git a/worf/src/mod.rs b/worf/src/mod.rs index 34d1812..e46b58a 100644 --- a/worf/src/mod.rs +++ b/worf/src/mod.rs @@ -1,4 +1,5 @@ use std::fmt; +use thiserror::Error; /// Configuration and command line parsing #[path = "lib/config.rs"] @@ -13,9 +14,8 @@ pub mod gui; #[path = "lib/modes/mod.rs"] pub mod modes; - /// Defines error the lib can encounter -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Error)] pub enum Error { /// Failed to update a cache file with the given reason. UpdateCacheError(String), @@ -42,6 +42,8 @@ pub enum Error { Graphics(String), /// Nothing selected NoSelection, + /// Invalid argument + InvalidArgument(String), } impl fmt::Display for Error { @@ -75,6 +77,7 @@ impl fmt::Display for Error { Error::NoSelection => { write!(f, "NoSelection") } + Error::InvalidArgument(s) => write!(f, "Invalid argument {s}"), } } -} \ No newline at end of file +}