oxwm

https://git.tonybtw.com/oxwm.git git://git.tonybtw.com/oxwm.git

replace anyhow bails with typed X11Error variants

Commit
e7ef3f4eda524ce78b5f2170851cfb46c97c3d1a
Parent
34487bf
Author
emzywastaken <amiamemetoo@gmail.com>
Date
2025-10-14 11:48:31
Add DisplayOpenFailed, FontLoadFailed, and DrawCreateFailed variants to
X11Error to cover X11/Xft failures

Diff

diff --git a/src/bar/bar.rs b/src/bar/bar.rs
index 23d6af5..36eae4a 100644
--- a/src/bar/bar.rs
+++ b/src/bar/bar.rs
@@ -2,7 +2,6 @@ use super::blocks::Block;
 use super::font::{Font, FontDraw};
 use crate::Config;
 use crate::window_manager::X11Error;
-use anyhow::Result;
 use std::time::Instant;
 use x11rb::COPY_DEPTH_FROM_PARENT;
 use x11rb::connection::Connection;
@@ -33,16 +32,13 @@ pub struct Bar {
     scheme_selected: crate::ColorScheme,
 }
 
-// Potential Errors:
-//  ::new -> x11rb::errors::ReplyOrIdError
-//  ::new -> x11rb::errors::ConnectionError
 impl Bar {
     pub fn new(
         connection: &RustConnection,
         screen: &Screen,
         screen_num: usize,
         config: &Config,
-    ) -> Result<Self> {
+    ) -> Result<Self, X11Error> {
         let window = connection.generate_id()?;
         let graphics_context = connection.generate_id()?;
 
@@ -50,9 +46,9 @@ impl Bar {
 
         let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) };
         if display.is_null() {
-            anyhow::bail!("Failed to open X11 display for XFT");
+            return Err(X11Error::DisplayOpenFailed.into());
         }
-        // TODO: Identify errors
+
         let font = Font::new(display, screen_num as i32, &config.font)?;
 
         let height = (font.height() as f32 * 1.4) as u16;
@@ -88,7 +84,6 @@ impl Bar {
         let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) };
         let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) };
 
-        // TODO: Identify errors
         let font_draw = FontDraw::new(display, window as x11::xlib::Drawable, visual, colormap)?;
 
         let horizontal_padding = (font.height() as f32 * 0.4) as u16;
@@ -181,7 +176,7 @@ impl Bar {
         connection: &RustConnection,
         current_tags: u32,
         occupied_tags: u32,
-    ) -> std::result::Result<(), X11Error> {
+    ) -> Result<(), X11Error> {
         if !self.needs_redraw {
             return Ok(());
         }
diff --git a/src/bar/font.rs b/src/bar/font.rs
index f93ffe8..a382f81 100644
--- a/src/bar/font.rs
+++ b/src/bar/font.rs
@@ -1,22 +1,24 @@
-use anyhow::Result;
 use std::ffi::CString;
 use x11::xft::{XftColor, XftDraw, XftDrawStringUtf8, XftFont, XftFontOpenName};
 use x11::xlib::{Colormap, Display, Drawable, Visual};
 use x11::xrender::XRenderColor;
 
+use crate::window_manager::X11Error;
+
 pub struct Font {
     xft_font: *mut XftFont,
     display: *mut Display,
 }
 
 impl Font {
-    pub fn new(display: *mut Display, screen: i32, font_name: &str) -> Result<Self> {
-        let font_name_cstr = CString::new(font_name)?;
+    pub fn new(display: *mut Display, screen: i32, font_name: &str) -> Result<Self, X11Error> {
+        let font_name_cstr =
+            CString::new(font_name).map_err(|_| X11Error::FontLoadFailed(font_name.to_string()))?;
 
         let xft_font = unsafe { XftFontOpenName(display, screen, font_name_cstr.as_ptr()) };
 
         if xft_font.is_null() {
-            anyhow::bail!("Failed to load font: {}", font_name);
+            return Err(X11Error::FontLoadFailed(font_name.to_string()));
         }
 
         Ok(Font { xft_font, display })
@@ -71,11 +73,11 @@ impl FontDraw {
         drawable: Drawable,
         visual: *mut Visual,
         colormap: Colormap,
-    ) -> Result<Self> {
+    ) -> Result<Self, X11Error> {
         let xft_draw = unsafe { x11::xft::XftDrawCreate(display, drawable, visual, colormap) };
 
         if xft_draw.is_null() {
-            anyhow::bail!("Failed to create XftDraw");
+            return Err(X11Error::DrawCreateFailed);
         }
 
         Ok(FontDraw { xft_draw })
diff --git a/src/window_manager.rs b/src/window_manager.rs
index 9d809f3..66ba794 100644
--- a/src/window_manager.rs
+++ b/src/window_manager.rs
@@ -48,6 +48,9 @@ pub enum X11Error {
     ConnectionError(x11rb::errors::ConnectionError),
     ReplyError(x11rb::errors::ReplyError),
     ReplyOrIdError(x11rb::errors::ReplyOrIdError),
+    DisplayOpenFailed,
+    FontLoadFailed(String),
+    DrawCreateFailed,
 }
 
 type WmResult<T> = Result<T, WmError>;
@@ -55,7 +58,6 @@ type WmResult<T> = Result<T, WmError>;
 impl std::fmt::Display for WmError {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         match self {
-            // Self::X11(x11_error) => write!(f, "{:?}", x11_error),
             Self::X11(error) => write!(f, "{}", error),
             Self::Io(error) => write!(f, "{}", error),
             Self::Anyhow(error) => write!(f, "{}", error),
@@ -68,10 +70,13 @@ impl std::error::Error for WmError {}
 impl std::fmt::Display for X11Error {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         match self {
-            X11Error::ConnectError(err) => write!(f, "{}", err),
-            X11Error::ConnectionError(err) => write!(f, "{}", err),
-            X11Error::ReplyError(err) => write!(f, "{}", err),
-            X11Error::ReplyOrIdError(err) => write!(f, "{}", err),
+            Self::ConnectError(err) => write!(f, "{}", err),
+            Self::ConnectionError(err) => write!(f, "{}", err),
+            Self::ReplyError(err) => write!(f, "{}", err),
+            Self::ReplyOrIdError(err) => write!(f, "{}", err),
+            Self::DisplayOpenFailed => write!(f, "failed to open X11 display"),
+            Self::FontLoadFailed(font_name) => write!(f, "failed to load Xft font: {}", font_name),
+            Self::DrawCreateFailed => write!(f, "failed to create XftDraw"),
         }
     }
 }