From c4b8fbd579d1b19ac2683d76b06c615dc928911b Mon Sep 17 00:00:00 2001 From: galister <22305755+galister@users.noreply.github.com> Date: Wed, 10 Dec 2025 21:03:13 +0900 Subject: [PATCH] anyhow context is nice --- wlx-overlay-s/src/backend/openvr/manifest.rs | 4 ++-- wlx-overlay-s/src/backend/wayvr/egl_data.rs | 8 ++++---- wlx-overlay-s/src/backend/wayvr/mod.rs | 11 ++++++----- wlx-overlay-s/src/config_wayvr.rs | 3 ++- wlx-overlay-s/src/graphics/dmabuf.rs | 13 +++++++------ wlx-overlay-s/src/overlays/edit/lock.rs | 3 ++- wlx-overlay-s/src/overlays/wayvr.rs | 3 ++- wlx-overlay-s/src/subsystem/hid/wayland.rs | 9 ++++----- 8 files changed, 29 insertions(+), 25 deletions(-) diff --git a/wlx-overlay-s/src/backend/openvr/manifest.rs b/wlx-overlay-s/src/backend/openvr/manifest.rs index 669ee0d6..e3e5d1c3 100644 --- a/wlx-overlay-s/src/backend/openvr/manifest.rs +++ b/wlx-overlay-s/src/backend/openvr/manifest.rs @@ -1,6 +1,6 @@ use std::{fs::File, io::Read}; -use anyhow::bail; +use anyhow::{bail, Context}; use json::{array, object}; use ovr_overlay::applications::ApplicationsManager; @@ -18,7 +18,7 @@ pub(super) fn install_manifest(app_mgr: &mut ApplicationsManager) -> anyhow::Res Ok(ref path) => path, Err(_) => executable_pathbuf .to_str() - .ok_or_else(|| anyhow::anyhow!("Invalid executable path"))?, + .context("Invalid executable path")?, }; if app_mgr.is_application_installed(APP_KEY) == Ok(true) diff --git a/wlx-overlay-s/src/backend/wayvr/egl_data.rs b/wlx-overlay-s/src/backend/wayvr/egl_data.rs index c04ece20..2583bb40 100644 --- a/wlx-overlay-s/src/backend/wayvr/egl_data.rs +++ b/wlx-overlay-s/src/backend/wayvr/egl_data.rs @@ -6,7 +6,7 @@ use crate::backend::wayvr::egl_ex::{ }; use super::egl_ex; -use anyhow::anyhow; +use anyhow::{anyhow, Context}; #[derive(Debug)] pub struct EGLData { @@ -83,9 +83,9 @@ fn get_disp( egl .get_display(khronos_egl::DEFAULT_DISPLAY) - .ok_or_else(|| anyhow!( + .context( "Both eglGetPlatformDisplayEXT and eglGetDisplay failed. This shouldn't happen unless you don't have any display manager running. Cannot continue, check your EGL installation." - )) + ) } } @@ -113,7 +113,7 @@ impl EGLData { let config = egl .choose_first_config(display, &attrib_list)? - .ok_or_else(|| anyhow!("Failed to get EGL config"))?; + .context("Failed to get EGL config")?; egl.bind_api(khronos_egl::OPENGL_ES_API)?; diff --git a/wlx-overlay-s/src/backend/wayvr/mod.rs b/wlx-overlay-s/src/backend/wayvr/mod.rs index 1d9282cf..34a509d2 100644 --- a/wlx-overlay-s/src/backend/wayvr/mod.rs +++ b/wlx-overlay-s/src/backend/wayvr/mod.rs @@ -10,6 +10,7 @@ pub mod server_ipc; mod smithay_wrapper; mod time; mod window; +use anyhow::Context; use comp::Application; use display::{Display, DisplayInitParams, DisplayVec}; use event_queue::SyncEventQueue; @@ -20,9 +21,9 @@ use smallvec::SmallVec; use smithay::{ backend::{ egl, - renderer::{ImportDma, gles::GlesRenderer}, + renderer::{gles::GlesRenderer, ImportDma}, }, - input::{SeatState, keyboard::XkbConfig}, + input::{keyboard::XkbConfig, SeatState}, output::{Mode, Output}, reexports::wayland_server::{self, backend::ClientId}, wayland::{ @@ -44,7 +45,7 @@ use wayvr_ipc::{packet_client, packet_server}; use crate::{ state::AppState, - subsystem::hid::{MODS_TO_KEYS, WheelDelta}, + subsystem::hid::{WheelDelta, MODS_TO_KEYS}, }; const STR_INVALID_HANDLE_DISP: &str = "Invalid display handle"; @@ -283,7 +284,7 @@ impl WayVR { .state .displays .get_mut(&display) - .ok_or_else(|| anyhow::anyhow!(STR_INVALID_HANDLE_DISP))?; + .context(STR_INVALID_HANDLE_DISP)?; /* Buffer warm-up is required, always two first calls of this function are always rendered */ if !display.wants_redraw && display.rendered_frame_count >= 2 { @@ -695,7 +696,7 @@ impl WayVRState { let display = self .displays .get_mut(&display_handle) - .ok_or_else(|| anyhow::anyhow!(STR_INVALID_HANDLE_DISP))?; + .context(STR_INVALID_HANDLE_DISP)?; let res = display.spawn_process(exec_path, args, env, working_dir)?; diff --git a/wlx-overlay-s/src/config_wayvr.rs b/wlx-overlay-s/src/config_wayvr.rs index 5640260d..9ca0c5bf 100644 --- a/wlx-overlay-s/src/config_wayvr.rs +++ b/wlx-overlay-s/src/config_wayvr.rs @@ -8,6 +8,7 @@ use std::{ sync::Arc, }; +use anyhow::Context; use serde::{Deserialize, Serialize}; use wlx_common::{common::LeftRight, config::GeneralConfig, windowing::Positioning}; @@ -187,7 +188,7 @@ impl WayVRConfig { keyboard_repeat_delay_ms: config_wayvr.keyboard_repeat_delay, keyboard_repeat_rate: config_wayvr.keyboard_repeat_rate, blit_method: wayvr::BlitMethod::from_string(&config_wayvr.blit_method) - .ok_or_else(|| anyhow::anyhow!("Unknown blit method"))?, + .context("unknown blit method")?, auto_hide_delay: if config_wayvr.auto_hide { Some(config_wayvr.auto_hide_delay) } else { diff --git a/wlx-overlay-s/src/graphics/dmabuf.rs b/wlx-overlay-s/src/graphics/dmabuf.rs index 4b87f6b1..1239a24e 100644 --- a/wlx-overlay-s/src/graphics/dmabuf.rs +++ b/wlx-overlay-s/src/graphics/dmabuf.rs @@ -4,23 +4,24 @@ use std::{ sync::Arc, }; +use anyhow::Context; use smallvec::SmallVec; use vulkano::{ - VulkanError, VulkanObject, device::Device, format::Format, - image::{Image, ImageCreateInfo, ImageTiling, ImageUsage, SubresourceLayout, sys::RawImage}, + image::{sys::RawImage, Image, ImageCreateInfo, ImageTiling, ImageUsage, SubresourceLayout}, memory::{ + allocator::{MemoryAllocator, MemoryTypeFilter}, DedicatedAllocation, DeviceMemory, ExternalMemoryHandleType, ExternalMemoryHandleTypes, MemoryAllocateInfo, MemoryImportInfo, MemoryPropertyFlags, ResourceMemory, - allocator::{MemoryAllocator, MemoryTypeFilter}, }, sync::Sharing, + VulkanError, VulkanObject, }; use wgui::gfx::WGfx; use wlx_capture::frame::{ - DRM_FORMAT_ABGR8888, DRM_FORMAT_ABGR2101010, DRM_FORMAT_ARGB8888, DRM_FORMAT_XBGR8888, - DRM_FORMAT_XBGR2101010, DRM_FORMAT_XRGB8888, DmabufFrame, DrmFormat, FourCC, + DmabufFrame, DrmFormat, FourCC, DRM_FORMAT_ABGR2101010, DRM_FORMAT_ABGR8888, + DRM_FORMAT_ARGB8888, DRM_FORMAT_XBGR2101010, DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB8888, }; pub const DRM_FORMAT_MOD_INVALID: u64 = 0xff_ffff_ffff_ffff; @@ -74,7 +75,7 @@ impl WGfxDmabuf for WGfx { ..Default::default() }, ) - .ok_or_else(|| anyhow::anyhow!("failed to get memory type index"))?; + .context("failed to get memory type index")?; debug_assert!(self.device.enabled_extensions().khr_external_memory_fd); debug_assert!(self.device.enabled_extensions().khr_external_memory); diff --git a/wlx-overlay-s/src/overlays/edit/lock.rs b/wlx-overlay-s/src/overlays/edit/lock.rs index c3412e79..4aa54ad0 100644 --- a/wlx-overlay-s/src/overlays/edit/lock.rs +++ b/wlx-overlay-s/src/overlays/edit/lock.rs @@ -1,3 +1,4 @@ +use anyhow::Context; use glam::FloatExt; use wgui::{ animation::{Animation, AnimationEasing}, @@ -24,7 +25,7 @@ impl InteractLockHandler { .state .widgets .get_as::(id) - .ok_or_else(|| anyhow::anyhow!("Element with id=\"shadow\" must be a "))?; + .context("Element with id=\"shadow\" must be a ")?; Ok(Self { id, diff --git a/wlx-overlay-s/src/overlays/wayvr.rs b/wlx-overlay-s/src/overlays/wayvr.rs index b372fd92..35974e6a 100644 --- a/wlx-overlay-s/src/overlays/wayvr.rs +++ b/wlx-overlay-s/src/overlays/wayvr.rs @@ -1,3 +1,4 @@ +use anyhow::Context; use glam::{vec3, Affine2, Affine3A, Quat, Vec3}; use smallvec::smallvec; use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::Arc}; @@ -677,7 +678,7 @@ impl OverlayBackend for WayVRBackend { .data .state .get_render_data(ctx.display) - .ok_or_else(|| anyhow::anyhow!("Failed to fetch render data"))? + .context("Failed to fetch render data")? .clone(); drop(wayvr); diff --git a/wlx-overlay-s/src/subsystem/hid/wayland.rs b/wlx-overlay-s/src/subsystem/hid/wayland.rs index c374c01d..0e5e326e 100644 --- a/wlx-overlay-s/src/subsystem/hid/wayland.rs +++ b/wlx-overlay-s/src/subsystem/hid/wayland.rs @@ -1,11 +1,12 @@ +use anyhow::Context; use wlx_capture::wayland::wayland_client::{ - Connection, Dispatch, Proxy, QueueHandle, - globals::{GlobalListContents, registry_queue_init}, + globals::{registry_queue_init, GlobalListContents}, protocol::{ wl_keyboard::{self, WlKeyboard}, wl_registry::WlRegistry, wl_seat::{self, Capability, WlSeat}, }, + Connection, Dispatch, Proxy, QueueHandle, }; use xkbcommon::xkb; @@ -46,9 +47,7 @@ pub fn get_keymap_wl() -> anyhow::Result { // this gets us the wl_keyboard let _ = queue.blocking_dispatch(&mut me); - me.keymap - .take() - .ok_or_else(|| anyhow::anyhow!("Could not load keymap")) + me.keymap.take().context("could not load keymap") } impl Dispatch for WlKeymapHandler {