From b61b03b1c9a787bc39c3578c7c2ffcaf5d48e50f Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Mon, 23 Aug 2021 09:52:10 -0700 Subject: [PATCH] refactor(eyre): handle options with combinators This commit removes the unnecessary eyre dependency and instead uses the relevant imports from color-eyre. Additionally, after reading the eyre readme a little more closely, I have switched out .compat() for the ok_or() combinator function as suggested here: https://github.com/yaahc/eyre#compatibility-with-anyhow. --- Cargo.lock | 5 +- komorebi/Cargo.toml | 1 - komorebi/src/main.rs | 10 +-- komorebi/src/monitor.rs | 14 ++--- komorebi/src/process_command.rs | 7 ++- komorebi/src/process_event.rs | 11 ++-- komorebi/src/window.rs | 6 +- komorebi/src/window_manager.rs | 107 +++++++++++++++++--------------- komorebi/src/windows_api.rs | 8 +-- komorebi/src/workspace.rs | 57 ++++++++--------- 10 files changed, 116 insertions(+), 110 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 832f20bf..3d1fd7d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -514,7 +514,6 @@ dependencies = [ "crossbeam-utils", "ctrlc", "dirs", - "eyre", "getset", "hotwatch", "komorebi-core", @@ -1177,9 +1176,9 @@ dependencies = [ [[package]] name = "sysinfo" -version = "0.20.0" +version = "0.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0af066e6272f2175c1783cfc2ebf3e2d8dfe2c182b00677fdeccbf8291af83fb" +checksum = "4786f320388e6031aa78c68455553f4e3425deeeb40565fecba3d101c1faf21f" dependencies = [ "cfg-if 1.0.0", "core-foundation-sys", diff --git a/komorebi/Cargo.toml b/komorebi/Cargo.toml index 3f674fbe..a81109d2 100644 --- a/komorebi/Cargo.toml +++ b/komorebi/Cargo.toml @@ -15,7 +15,6 @@ crossbeam-channel = "0.5" crossbeam-utils = "0.8" ctrlc = "3" dirs = "3" -eyre = "0.6" getset = "0.1" hotwatch = "0.4" lazy_static = "1" diff --git a/komorebi/src/main.rs b/komorebi/src/main.rs index 17f112a3..1d8917a1 100644 --- a/komorebi/src/main.rs +++ b/komorebi/src/main.rs @@ -9,7 +9,7 @@ use std::thread; #[cfg(feature = "deadlock_detection")] use std::time::Duration; -use color_eyre::eyre::ContextCompat; +use color_eyre::eyre::anyhow; use color_eyre::Result; use crossbeam_channel::Receiver; use crossbeam_channel::Sender; @@ -82,7 +82,7 @@ fn setup() -> Result<(WorkerGuard, WorkerGuard)> { std::env::set_var("RUST_LOG", "info"); } - let home = dirs::home_dir().context("there is no home directory")?; + let home = dirs::home_dir().ok_or_else(|| anyhow!("there is no home directory"))?; let appender = tracing_appender::rolling::never(home, "komorebi.log"); let color_appender = tracing_appender::rolling::never(std::env::temp_dir(), "komorebi.log"); let (non_blocking, guard) = tracing_appender::non_blocking(appender); @@ -133,7 +133,7 @@ fn setup() -> Result<(WorkerGuard, WorkerGuard)> { } pub fn load_configuration() -> Result<()> { - let home = dirs::home_dir().context("there is no home directory")?; + let home = dirs::home_dir().ok_or_else(|| anyhow!("there is no home directory"))?; let mut config_v1 = home.clone(); config_v1.push("komorebi.ahk"); @@ -147,7 +147,7 @@ pub fn load_configuration() -> Result<()> { config_v1 .as_os_str() .to_str() - .context("cannot convert path to string")? + .ok_or_else(|| anyhow!("cannot convert path to string"))? ); Command::new("autohotkey.exe") @@ -159,7 +159,7 @@ pub fn load_configuration() -> Result<()> { config_v2 .as_os_str() .to_str() - .context("cannot convert path to string")? + .ok_or_else(|| anyhow!("cannot convert path to string"))? ); Command::new("AutoHotkey64.exe") diff --git a/komorebi/src/monitor.rs b/komorebi/src/monitor.rs index dd569975..f12fb29b 100644 --- a/komorebi/src/monitor.rs +++ b/komorebi/src/monitor.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::collections::VecDeque; -use color_eyre::eyre::ContextCompat; +use color_eyre::eyre::anyhow; use color_eyre::Result; use getset::CopyGetters; use getset::Getters; @@ -56,7 +56,7 @@ impl Monitor { pub fn add_container(&mut self, container: Container) -> Result<()> { let workspace = self .focused_workspace_mut() - .context("there is no workspace")?; + .ok_or_else(|| anyhow!("there is no workspace"))?; workspace.add_container(container); @@ -78,17 +78,17 @@ impl Monitor { ) -> Result<()> { let workspace = self .focused_workspace_mut() - .context("there is no workspace")?; + .ok_or_else(|| anyhow!("there is no workspace"))?; if workspace.maximized_window().is_some() { - return Err(eyre::anyhow!( + return Err(anyhow!( "cannot move native maximized window to another monitor or workspace" )); } let container = workspace .remove_focused_container() - .context("there is no container")?; + .ok_or_else(|| anyhow!("there is no container"))?; let workspaces = self.workspaces_mut(); @@ -129,7 +129,7 @@ impl Monitor { if name.is_some() { self.workspaces_mut() .get_mut(idx) - .context("there is no workspace")? + .ok_or_else(|| anyhow!("there is no workspace"))? .set_name(name); } } @@ -145,7 +145,7 @@ impl Monitor { let work_area = *self.work_area_size(); self.focused_workspace_mut() - .context("there is no workspace")? + .ok_or_else(|| anyhow!("there is no workspace"))? .update(&work_area)?; Ok(()) diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 0f1b8038..05637490 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -5,7 +5,7 @@ use std::str::FromStr; use std::sync::Arc; use std::thread; -use color_eyre::eyre::ContextCompat; +use color_eyre::eyre::anyhow; use color_eyre::Result; use parking_lot::Mutex; use uds_windows::UnixStream; @@ -122,7 +122,7 @@ impl WindowManager { let work_area = *monitor.work_area_size(); let workspace = monitor .focused_workspace_mut() - .context("there is no workspace")?; + .ok_or_else(|| anyhow!("there is no workspace"))?; // Reset any resize adjustments if we want to force a retile for resize in workspace.resize_dimensions_mut() { @@ -161,7 +161,8 @@ impl WindowManager { } SocketMessage::State => { let state = serde_json::to_string_pretty(&window_manager::State::from(self))?; - let mut socket = dirs::home_dir().context("there is no home directory")?; + let mut socket = + dirs::home_dir().ok_or_else(|| anyhow!("there is no home directory"))?; socket.push("komorebic.sock"); let socket = socket.as_path(); diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index afd992ca..b97eabfa 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -2,7 +2,7 @@ use std::fs::OpenOptions; use std::sync::Arc; use std::thread; -use color_eyre::eyre::ContextCompat; +use color_eyre::eyre::anyhow; use color_eyre::Result; use crossbeam_channel::select; use parking_lot::Mutex; @@ -57,7 +57,7 @@ impl WindowManager { | WindowManagerEvent::MoveResizeEnd(_, window) => { let monitor_idx = self .monitor_idx_from_window(*window) - .context("there is no monitor associated with this window, it may have already been destroyed")?; + .ok_or_else(|| anyhow!("there is no monitor associated with this window, it may have already been destroyed"))?; self.focus_monitor(monitor_idx)?; } @@ -158,7 +158,7 @@ impl WindowManager { if self.focused_monitor_idx() != known_monitor_idx || self .focused_monitor() - .context("there is no monitor")? + .ok_or_else(|| anyhow!("there is no monitor"))? .focused_workspace_idx() != known_workspace_idx { @@ -210,7 +210,7 @@ impl WindowManager { let old_position = *workspace .latest_layout() .get(focused_idx) - .context("there is no latest layout")?; + .ok_or_else(|| anyhow!("there is no latest layout"))?; let mut new_position = WindowsApi::window_rect(window.hwnd())?; // See Window.set_position() in window.rs for comments @@ -305,7 +305,8 @@ impl WindowManager { } } - let mut hwnd_json = dirs::home_dir().context("there is no home directory")?; + let mut hwnd_json = + dirs::home_dir().ok_or_else(|| anyhow!("there is no home directory"))?; hwnd_json.push("komorebi.hwnd.json"); let file = OpenOptions::new() .write(true) diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index 19e0757c..4e877991 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -2,7 +2,7 @@ use std::convert::TryFrom; use std::fmt::Display; use std::fmt::Formatter; -use color_eyre::eyre::ContextCompat; +use color_eyre::eyre::anyhow; use color_eyre::Result; use serde::ser::SerializeStruct; use serde::Serialize; @@ -187,12 +187,12 @@ impl Window { pub fn style(self) -> Result { let bits = u32::try_from(WindowsApi::gwl_style(self.hwnd())?)?; - GwlStyle::from_bits(bits).context("there is no gwl style") + GwlStyle::from_bits(bits).ok_or_else(|| anyhow!("there is no gwl style")) } pub fn ex_style(self) -> Result { let bits = u32::try_from(WindowsApi::gwl_ex_style(self.hwnd())?)?; - GwlExStyle::from_bits(bits).context("there is no gwl style") + GwlExStyle::from_bits(bits).ok_or_else(|| anyhow!("there is no gwl style")) } pub fn title(self) -> Result { diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 607315dd..2e0cd313 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -5,6 +5,7 @@ use std::path::PathBuf; use std::sync::Arc; use std::thread; +use color_eyre::eyre::anyhow; use color_eyre::eyre::ContextCompat; use color_eyre::Result; use crossbeam_channel::Receiver; @@ -102,7 +103,7 @@ impl EnforceWorkspaceRuleOp { impl WindowManager { #[tracing::instrument] pub fn new(incoming: Arc>>) -> Result { - let home = dirs::home_dir().context("there is no home directory")?; + let home = dirs::home_dir().ok_or_else(|| anyhow!("there is no home directory"))?; let mut socket = home; socket.push("komorebi.sock"); let socket = socket.as_path(); @@ -148,7 +149,7 @@ impl WindowManager { #[tracing::instrument(skip(self))] pub fn watch_configuration(&mut self, enable: bool) -> Result<()> { - let home = dirs::home_dir().context("there is no home directory")?; + let home = dirs::home_dir().ok_or_else(|| anyhow!("there is no home directory"))?; let mut config_v1 = home.clone(); config_v1.push("komorebi.ahk"); @@ -173,7 +174,7 @@ impl WindowManager { config .as_os_str() .to_str() - .context("cannot convert path to string")? + .ok_or_else(|| anyhow!("cannot convert path to string"))? ); // Always make absolutely sure that there isn't an already existing watch, because // hotwatch allows multiple watches to be registered for the same path @@ -204,7 +205,7 @@ impl WindowManager { config .as_os_str() .to_str() - .context("cannot convert path to string")? + .ok_or_else(|| anyhow!("cannot convert path to string"))? ); self.hotwatch.unwatch(config)?; @@ -222,7 +223,7 @@ impl WindowManager { let focused_workspace_idx = self .monitors() .get(focused_monitor_idx) - .context("there is no monitor with that index")? + .ok_or_else(|| anyhow!("there is no monitor with that index"))? .focused_workspace_idx(); let workspace_rules = WORKSPACE_RULES.lock(); @@ -283,10 +284,10 @@ impl WindowManager { let origin_workspace = self .monitors_mut() .get_mut(op.origin_monitor_idx) - .context("there is no monitor with that index")? + .ok_or_else(|| anyhow!("there is no monitor with that index"))? .workspaces_mut() .get_mut(op.origin_workspace_idx) - .context("there is no workspace with that index")?; + .ok_or_else(|| anyhow!("there is no workspace with that index"))?; // Hide the window we are about to remove if it is on the currently focused workspace if op.is_origin(focused_monitor_idx, focused_workspace_idx) { @@ -303,7 +304,7 @@ impl WindowManager { let target_monitor = self .monitors_mut() .get_mut(op.target_monitor_idx) - .context("there is no monitor with that index")?; + .ok_or_else(|| anyhow!("there is no monitor with that index"))?; // The very first time this fn is called, the workspace might not even exist yet if target_monitor @@ -318,7 +319,7 @@ impl WindowManager { let target_workspace = target_monitor .workspaces_mut() .get_mut(op.target_workspace_idx) - .context("there is no workspace with that index")?; + .ok_or_else(|| anyhow!("there is no workspace with that index"))?; target_workspace.new_container_for_window(Window { hwnd: op.hwnd }); } @@ -364,7 +365,7 @@ impl WindowManager { tracing::info!("updating"); self.focused_monitor_mut() - .context("there is no monitor")? + .ok_or_else(|| anyhow!("there is no monitor"))? .update_focused_workspace()?; if mouse_follows_focus { @@ -385,7 +386,7 @@ impl WindowManager { // attach to the thread of the desktop window always seems to result in "Access is // denied (os error 5)" WindowsApi::set_foreground_window(desktop_window.hwnd()) - .map_err(|error| eyre::anyhow!("{} {}:{}", error, file!(), line!()))?; + .map_err(|error| anyhow!("{} {}:{}", error, file!(), line!()))?; } } @@ -408,7 +409,7 @@ impl WindowManager { let focused_idx_resize = workspace .resize_dimensions() .get(focused_idx) - .context("there is no resize adjustment for this container")?; + .ok_or_else(|| anyhow!("there is no resize adjustment for this container"))?; if direction.is_valid( workspace.layout(), @@ -453,7 +454,7 @@ impl WindowManager { let resize = workspace.layout().resize( unaltered .get(focused_idx) - .context("there is no last layout")?, + .ok_or_else(|| anyhow!("there is no last layout"))?, focused_idx_resize, direction, sizing, @@ -487,25 +488,27 @@ impl WindowManager { pub fn move_container_to_monitor(&mut self, idx: usize, follow: bool) -> Result<()> { tracing::info!("moving container"); - let monitor = self.focused_monitor_mut().context("there is no monitor")?; + let monitor = self + .focused_monitor_mut() + .ok_or_else(|| anyhow!("there is no monitor"))?; let workspace = monitor .focused_workspace_mut() - .context("there is no workspace")?; + .ok_or_else(|| anyhow!("there is no workspace"))?; if workspace.maximized_window().is_some() { - return Err(eyre::anyhow!( + return Err(anyhow!( "cannot move native maximized window to another monitor or workspace" )); } let container = workspace .remove_focused_container() - .context("there is no container")?; + .ok_or_else(|| anyhow!("there is no container"))?; let target_monitor = self .monitors_mut() .get_mut(idx) - .context("there is no monitor")?; + .ok_or_else(|| anyhow!("there is no monitor"))?; target_monitor.add_container(container)?; target_monitor.load_focused_workspace()?; @@ -521,7 +524,9 @@ impl WindowManager { pub fn move_container_to_workspace(&mut self, idx: usize, follow: bool) -> Result<()> { tracing::info!("moving container"); - let monitor = self.focused_monitor_mut().context("there is no monitor")?; + let monitor = self + .focused_monitor_mut() + .ok_or_else(|| anyhow!("there is no monitor"))?; monitor.move_container_to_workspace(idx, follow)?; monitor.load_focused_workspace()?; self.update_focused_workspace(true) @@ -534,7 +539,7 @@ impl WindowManager { let new_idx = workspace .new_idx_for_direction(direction) - .context("this is not a valid direction from the current position")?; + .ok_or_else(|| anyhow!("this is not a valid direction from the current position"))?; workspace.focus_container(new_idx); self.focused_window_mut()?.focus()?; @@ -551,7 +556,7 @@ impl WindowManager { let current_idx = workspace.focused_container_idx(); let new_idx = workspace .new_idx_for_direction(direction) - .context("this is not a valid direction from the current position")?; + .ok_or_else(|| anyhow!("this is not a valid direction from the current position"))?; workspace.swap_containers(current_idx, new_idx); workspace.focus_container(new_idx); @@ -565,7 +570,7 @@ impl WindowManager { let container = self.focused_container_mut()?; if container.windows().len() == 1 { - return Err(eyre::anyhow!("there is only one window in this container")); + return Err(anyhow!("there is only one window in this container")); } let current_idx = container.focused_window_idx(); @@ -592,9 +597,9 @@ impl WindowManager { ); if is_valid { - let new_idx = workspace - .new_idx_for_direction(direction) - .context("this is not a valid direction from the current position")?; + let new_idx = workspace.new_idx_for_direction(direction).ok_or_else(|| { + anyhow!("this is not a valid direction from the current position") + })?; let adjusted_new_index = if new_idx > current_container_idx { new_idx - 1 @@ -623,7 +628,7 @@ impl WindowManager { tracing::info!("removing window"); if self.focused_container()?.windows().len() == 1 { - return Err(eyre::anyhow!("a container must have at least one window")); + return Err(anyhow!("a container must have at least one window")); } let workspace = self.focused_workspace_mut()?; @@ -673,7 +678,7 @@ impl WindowManager { let window = workspace .floating_windows_mut() .last_mut() - .context("there is no floating window")?; + .ok_or_else(|| anyhow!("there is no floating window"))?; window.center(&work_area)?; window.focus()?; @@ -805,7 +810,7 @@ impl WindowManager { let padding = workspace .workspace_padding() - .context("there is no workspace padding")?; + .ok_or_else(|| anyhow!("there is no workspace padding"))?; workspace.set_workspace_padding(Option::from(sizing.adjust_by(padding, adjustment))); @@ -820,7 +825,7 @@ impl WindowManager { let padding = workspace .container_padding() - .context("there is no container padding")?; + .ok_or_else(|| anyhow!("there is no container padding"))?; workspace.set_container_padding(Option::from(sizing.adjust_by(padding, adjustment))); @@ -837,12 +842,12 @@ impl WindowManager { let monitor = self .monitors_mut() .get_mut(monitor_idx) - .context("there is no monitor")?; + .ok_or_else(|| anyhow!("there is no monitor"))?; let workspace = monitor .workspaces_mut() .get_mut(workspace_idx) - .context("there is no monitor")?; + .ok_or_else(|| anyhow!("there is no monitor"))?; workspace.set_tile(tile); @@ -863,7 +868,7 @@ impl WindowManager { let monitor = self .monitors_mut() .get_mut(monitor_idx) - .context("there is no monitor")?; + .ok_or_else(|| anyhow!("there is no monitor"))?; let work_area = *monitor.work_area_size(); let focused_workspace_idx = monitor.focused_workspace_idx(); @@ -871,7 +876,7 @@ impl WindowManager { let workspace = monitor .workspaces_mut() .get_mut(workspace_idx) - .context("there is no monitor")?; + .ok_or_else(|| anyhow!("there is no monitor"))?; workspace.set_layout(layout); @@ -895,7 +900,7 @@ impl WindowManager { let monitor = self .monitors_mut() .get_mut(monitor_idx) - .context("there is no monitor")?; + .ok_or_else(|| anyhow!("there is no monitor"))?; monitor.ensure_workspace_count(workspace_count); @@ -914,12 +919,12 @@ impl WindowManager { let monitor = self .monitors_mut() .get_mut(monitor_idx) - .context("there is no monitor")?; + .ok_or_else(|| anyhow!("there is no monitor"))?; let workspace = monitor .workspaces_mut() .get_mut(workspace_idx) - .context("there is no monitor")?; + .ok_or_else(|| anyhow!("there is no monitor"))?; workspace.set_workspace_padding(Option::from(size)); @@ -938,12 +943,12 @@ impl WindowManager { let monitor = self .monitors_mut() .get_mut(monitor_idx) - .context("there is no monitor")?; + .ok_or_else(|| anyhow!("there is no monitor"))?; let workspace = monitor .workspaces_mut() .get_mut(workspace_idx) - .context("there is no monitor")?; + .ok_or_else(|| anyhow!("there is no monitor"))?; workspace.set_name(Option::from(name.clone())); monitor.workspace_names_mut().insert(workspace_idx, name); @@ -963,12 +968,12 @@ impl WindowManager { let monitor = self .monitors_mut() .get_mut(monitor_idx) - .context("there is no monitor")?; + .ok_or_else(|| anyhow!("there is no monitor"))?; let workspace = monitor .workspaces_mut() .get_mut(workspace_idx) - .context("there is no monitor")?; + .ok_or_else(|| anyhow!("there is no monitor"))?; workspace.set_container_padding(Option::from(size)); @@ -978,7 +983,7 @@ impl WindowManager { pub fn focused_monitor_work_area(&self) -> Result { Ok(*self .focused_monitor() - .context("there is no monitor")? + .ok_or_else(|| anyhow!("there is no monitor"))? .work_area_size()) } @@ -989,7 +994,7 @@ impl WindowManager { if self.monitors().get(idx).is_some() { self.monitors.focus(idx); } else { - return Err(eyre::anyhow!("this is not a valid monitor index")); + return Err(anyhow!("this is not a valid monitor index")); } Ok(()) @@ -1009,16 +1014,16 @@ impl WindowManager { pub fn focused_workspace(&self) -> Result<&Workspace> { self.focused_monitor() - .context("there is no monitor")? + .ok_or_else(|| anyhow!("there is no monitor"))? .focused_workspace() - .context("there is no workspace") + .ok_or_else(|| anyhow!("there is no workspace")) } pub fn focused_workspace_mut(&mut self) -> Result<&mut Workspace> { self.focused_monitor_mut() - .context("there is no monitor")? + .ok_or_else(|| anyhow!("there is no monitor"))? .focused_workspace_mut() - .context("there is no workspace") + .ok_or_else(|| anyhow!("there is no workspace")) } #[tracing::instrument(skip(self))] @@ -1027,7 +1032,7 @@ impl WindowManager { let monitor = self .focused_monitor_mut() - .context("there is no workspace")?; + .ok_or_else(|| anyhow!("there is no workspace"))?; monitor.focus_workspace(idx)?; monitor.load_focused_workspace()?; @@ -1041,7 +1046,7 @@ impl WindowManager { let monitor = self .focused_monitor_mut() - .context("there is no workspace")?; + .ok_or_else(|| anyhow!("there is no workspace"))?; monitor.focus_workspace(monitor.new_workspace_idx())?; monitor.load_focused_workspace()?; @@ -1052,18 +1057,18 @@ impl WindowManager { pub fn focused_container(&self) -> Result<&Container> { self.focused_workspace()? .focused_container() - .context("there is no container") + .ok_or_else(|| anyhow!("there is no container")) } pub fn focused_container_mut(&mut self) -> Result<&mut Container> { self.focused_workspace_mut()? .focused_container_mut() - .context("there is no container") + .ok_or_else(|| anyhow!("there is no container")) } fn focused_window_mut(&mut self) -> Result<&mut Window> { self.focused_container_mut()? .focused_window_mut() - .context("there is no window") + .ok_or_else(|| anyhow!("there is no window")) } } diff --git a/komorebi/src/windows_api.rs b/komorebi/src/windows_api.rs index 1966518d..4ba32713 100644 --- a/komorebi/src/windows_api.rs +++ b/komorebi/src/windows_api.rs @@ -3,9 +3,9 @@ use std::convert::TryFrom; use std::convert::TryInto; use std::ffi::c_void; -use color_eyre::eyre::ContextCompat; +use color_eyre::eyre::anyhow; +use color_eyre::eyre::Error; use color_eyre::Result; -use eyre::Error; use bindings::Windows::Win32::Foundation::BOOL; use bindings::Windows::Win32::Foundation::HANDLE; @@ -314,7 +314,7 @@ impl WindowsApi { next_hwnd = Self::next_window(HWND(next_hwnd))?; } - Err(eyre::anyhow!("could not find next window")) + Err(anyhow!("could not find next window")) } pub fn window_rect(hwnd: HWND) -> Result { @@ -463,7 +463,7 @@ impl WindowsApi { Ok(Self::exe_path(handle)? .split('\\') .last() - .context("there is no last element")? + .ok_or_else(|| anyhow!("there is no last element"))? .to_string()) } diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index a6355581..a88ab2fb 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -1,6 +1,7 @@ use std::collections::VecDeque; use std::num::NonZeroUsize; +use color_eyre::eyre::anyhow; use color_eyre::eyre::ContextCompat; use color_eyre::Result; use getset::CopyGetters; @@ -229,16 +230,16 @@ impl Workspace { pub fn focus_container_by_window(&mut self, hwnd: isize) -> Result<()> { let container_idx = self .container_idx_for_window(hwnd) - .context("there is no container/window")?; + .ok_or_else(|| anyhow!("there is no container/window"))?; let container = self .containers_mut() .get_mut(container_idx) - .context("there is no container")?; + .ok_or_else(|| anyhow!("there is no container"))?; let window_idx = container .idx_for_window(hwnd) - .context("there is no window")?; + .ok_or_else(|| anyhow!("there is no window"))?; container.focus_window(window_idx); self.focus_container(container_idx); @@ -293,7 +294,7 @@ impl Workspace { pub fn promote_container(&mut self) -> Result<()> { let container = self .remove_focused_container() - .context("there is no container")?; + .ok_or_else(|| anyhow!("there is no container"))?; self.containers_mut().push_front(container); self.resize_dimensions_mut().insert(0, None); self.focus_container(0); @@ -330,27 +331,27 @@ impl Workspace { let container_idx = self .container_idx_for_window(hwnd) - .context("there is no window")?; + .ok_or_else(|| anyhow!("there is no window"))?; let container = self .containers_mut() .get_mut(container_idx) - .context("there is no container")?; + .ok_or_else(|| anyhow!("there is no container"))?; let window_idx = container .windows() .iter() .position(|window| window.hwnd == hwnd) - .context("there is no window")?; + .ok_or_else(|| anyhow!("there is no window"))?; container .remove_window_by_idx(window_idx) - .context("there is no window")?; + .ok_or_else(|| anyhow!("there is no window"))?; if container.windows().is_empty() { self.containers_mut() .remove(container_idx) - .context("there is no container")?; + .ok_or_else(|| anyhow!("there is no container"))?; // Whenever a container is empty, we need to remove any resize dimensions for it too if self.resize_dimensions().get(container_idx).is_some() { @@ -393,11 +394,11 @@ impl Workspace { let container = self .focused_container_mut() - .context("there is no container")?; + .ok_or_else(|| anyhow!("there is no container"))?; let window = container .remove_focused_window() - .context("there is no window")?; + .ok_or_else(|| anyhow!("there is no window"))?; // This is a little messy let adjusted_target_container_index = if container.windows().is_empty() { @@ -417,13 +418,13 @@ impl Workspace { let target_container = self .containers_mut() .get_mut(adjusted_target_container_index) - .context("there is no container")?; + .ok_or_else(|| anyhow!("there is no container"))?; target_container.add_window(window); self.focus_container(adjusted_target_container_index); self.focused_container_mut() - .context("there is no container")? + .ok_or_else(|| anyhow!("there is no container"))? .load_focused_window(); Ok(()) @@ -434,11 +435,11 @@ impl Workspace { let container = self .focused_container_mut() - .context("there is no container")?; + .ok_or_else(|| anyhow!("there is no container"))?; let window = container .remove_focused_window() - .context("there is no window")?; + .ok_or_else(|| anyhow!("there is no window"))?; if container.windows().is_empty() { self.containers_mut().remove(focused_container_idx); @@ -458,7 +459,7 @@ impl Workspace { let focused_idx = self.focused_container_idx(); let window = self .remove_focused_floating_window() - .context("there is no floating window")?; + .ok_or_else(|| anyhow!("there is no floating window"))?; let mut container = Container::default(); container.add_window(window); @@ -498,11 +499,11 @@ impl Workspace { let container = self .focused_container_mut() - .context("there is no container")?; + .ok_or_else(|| anyhow!("there is no container"))?; let window = container .remove_focused_window() - .context("there is no window")?; + .ok_or_else(|| anyhow!("there is no window"))?; if container.windows().is_empty() { self.containers_mut().remove(focused_idx); @@ -547,7 +548,7 @@ impl Workspace { let container = self .containers_mut() .remove(focused_idx) - .context("there is not container")?; + .ok_or_else(|| anyhow!("there is no container"))?; // We don't remove any resize adjustments for a monocle, because when this container is // inevitably reintegrated, it would be weird if it doesn't go back to the dimensions @@ -559,7 +560,7 @@ impl Workspace { self.monocle_container_mut() .as_mut() - .context("there is no monocle container")? + .ok_or_else(|| anyhow!("there is no monocle container"))? .load_focused_window(); Ok(()) @@ -568,12 +569,12 @@ impl Workspace { pub fn reintegrate_monocle_container(&mut self) -> Result<()> { let restore_idx = self .monocle_container_restore_idx() - .context("there is no monocle restore index")?; + .ok_or_else(|| anyhow!("there is no monocle restore index"))?; let container = self .monocle_container_mut() .as_ref() - .context("there is no monocle container")?; + .ok_or_else(|| anyhow!("there is no monocle container"))?; let container = container.clone(); if restore_idx > self.containers().len() - 1 { @@ -584,7 +585,7 @@ impl Workspace { self.containers_mut().insert(restore_idx, container); self.focus_container(restore_idx); self.focused_container_mut() - .context("there is no container")? + .ok_or_else(|| anyhow!("there is no container"))? .load_focused_window(); self.set_monocle_container(None); @@ -598,11 +599,11 @@ impl Workspace { let container = self .focused_container_mut() - .context("there is no container")?; + .ok_or_else(|| anyhow!("there is no container"))?; let window = container .remove_focused_window() - .context("there is no window")?; + .ok_or_else(|| anyhow!("there is no window"))?; if container.windows().is_empty() { self.containers_mut().remove(focused_idx); @@ -626,12 +627,12 @@ impl Workspace { pub fn reintegrate_maximized_window(&mut self) -> Result<()> { let restore_idx = self .maximized_window_restore_idx() - .context("there is no monocle restore index")?; + .ok_or_else(|| anyhow!("there is no monocle restore index"))?; let window = self .maximized_window() .as_ref() - .context("there is no monocle container")?; + .ok_or_else(|| anyhow!("there is no monocle container"))?; let window = *window; if !self.containers().is_empty() && restore_idx > self.containers().len() - 1 { @@ -646,7 +647,7 @@ impl Workspace { self.focus_container(restore_idx); self.focused_container_mut() - .context("there is no container")? + .ok_or_else(|| anyhow!("there is no container"))? .load_focused_window(); self.set_maximized_window(None);