replace anyhow bails with typed X11Error variants
Browse files
Add DisplayOpenFailed, FontLoadFailed, and DrawCreateFailed variants to X11Error to cover X11/Xft failures
+4 −9 src/bar/bar.rs
| @@ -2,7 +2,6 @@ use super::blocks::Block; | ||
| 2 | 2 | use super::font::{Font, FontDraw}; |
| 3 | 3 | use crate::Config; |
| 4 | 4 | use crate::window_manager::X11Error; |
| 5 | -use anyhow::Result; | |
| 6 | 5 | use std::time::Instant; |
| 7 | 6 | use x11rb::COPY_DEPTH_FROM_PARENT; |
| 8 | 7 | use x11rb::connection::Connection; |
| @@ -33,16 +32,13 @@ pub struct Bar { | ||
| 33 | 32 | scheme_selected: crate::ColorScheme, |
| 34 | 33 | } |
| 35 | 34 | |
| 36 | -// Potential Errors: | |
| 37 | -// ::new -> x11rb::errors::ReplyOrIdError | |
| 38 | -// ::new -> x11rb::errors::ConnectionError | |
| 39 | 35 | impl Bar { |
| 40 | 36 | pub fn new( |
| 41 | 37 | connection: &RustConnection, |
| 42 | 38 | screen: &Screen, |
| 43 | 39 | screen_num: usize, |
| 44 | 40 | config: &Config, |
| 45 | - ) -> Result<Self> { | |
| 41 | + ) -> Result<Self, X11Error> { | |
| 46 | 42 | let window = connection.generate_id()?; |
| 47 | 43 | let graphics_context = connection.generate_id()?; |
| 48 | 44 | |
| @@ -50,9 +46,9 @@ impl Bar { | ||
| 50 | 46 | |
| 51 | 47 | let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) }; |
| 52 | 48 | if display.is_null() { |
| 53 | - anyhow::bail!("Failed to open X11 display for XFT"); | |
| 49 | + return Err(X11Error::DisplayOpenFailed.into()); | |
| 54 | 50 | } |
| 55 | - // TODO: Identify errors | |
| 51 | + | |
| 56 | 52 | let font = Font::new(display, screen_num as i32, &config.font)?; |
| 57 | 53 | |
| 58 | 54 | let height = (font.height() as f32 * 1.4) as u16; |
| @@ -88,7 +84,6 @@ impl Bar { | ||
| 88 | 84 | let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) }; |
| 89 | 85 | let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) }; |
| 90 | 86 | |
| 91 | - // TODO: Identify errors | |
| 92 | 87 | let font_draw = FontDraw::new(display, window as x11::xlib::Drawable, visual, colormap)?; |
| 93 | 88 | |
| 94 | 89 | let horizontal_padding = (font.height() as f32 * 0.4) as u16; |
| @@ -181,7 +176,7 @@ impl Bar { | ||
| 181 | 176 | connection: &RustConnection, |
| 182 | 177 | current_tags: u32, |
| 183 | 178 | occupied_tags: u32, |
| 184 | - ) -> std::result::Result<(), X11Error> { | |
| 179 | + ) -> Result<(), X11Error> { | |
| 185 | 180 | if !self.needs_redraw { |
| 186 | 181 | return Ok(()); |
| 187 | 182 | } |
+8 −6 src/bar/font.rs
| @@ -1,22 +1,24 @@ | ||
| 1 | -use anyhow::Result; | |
| 2 | 1 | use std::ffi::CString; |
| 3 | 2 | use x11::xft::{XftColor, XftDraw, XftDrawStringUtf8, XftFont, XftFontOpenName}; |
| 4 | 3 | use x11::xlib::{Colormap, Display, Drawable, Visual}; |
| 5 | 4 | use x11::xrender::XRenderColor; |
| 6 | 5 | |
| 6 | +use crate::window_manager::X11Error; | |
| 7 | + | |
| 7 | 8 | pub struct Font { |
| 8 | 9 | xft_font: *mut XftFont, |
| 9 | 10 | display: *mut Display, |
| 10 | 11 | } |
| 11 | 12 | |
| 12 | 13 | impl Font { |
| 13 | - pub fn new(display: *mut Display, screen: i32, font_name: &str) -> Result<Self> { | |
| 14 | - let font_name_cstr = CString::new(font_name)?; | |
| 14 | + pub fn new(display: *mut Display, screen: i32, font_name: &str) -> Result<Self, X11Error> { | |
| 15 | + let font_name_cstr = | |
| 16 | + CString::new(font_name).map_err(|_| X11Error::FontLoadFailed(font_name.to_string()))?; | |
| 15 | 17 | |
| 16 | 18 | let xft_font = unsafe { XftFontOpenName(display, screen, font_name_cstr.as_ptr()) }; |
| 17 | 19 | |
| 18 | 20 | if xft_font.is_null() { |
| 19 | - anyhow::bail!("Failed to load font: {}", font_name); | |
| 21 | + return Err(X11Error::FontLoadFailed(font_name.to_string())); | |
| 20 | 22 | } |
| 21 | 23 | |
| 22 | 24 | Ok(Font { xft_font, display }) |
| @@ -71,11 +73,11 @@ impl FontDraw { | ||
| 71 | 73 | drawable: Drawable, |
| 72 | 74 | visual: *mut Visual, |
| 73 | 75 | colormap: Colormap, |
| 74 | - ) -> Result<Self> { | |
| 76 | + ) -> Result<Self, X11Error> { | |
| 75 | 77 | let xft_draw = unsafe { x11::xft::XftDrawCreate(display, drawable, visual, colormap) }; |
| 76 | 78 | |
| 77 | 79 | if xft_draw.is_null() { |
| 78 | - anyhow::bail!("Failed to create XftDraw"); | |
| 80 | + return Err(X11Error::DrawCreateFailed); | |
| 79 | 81 | } |
| 80 | 82 | |
| 81 | 83 | Ok(FontDraw { xft_draw }) |
+10 −5 src/window_manager.rs
| @@ -48,6 +48,9 @@ pub enum X11Error { | ||
| 48 | 48 | ConnectionError(x11rb::errors::ConnectionError), |
| 49 | 49 | ReplyError(x11rb::errors::ReplyError), |
| 50 | 50 | ReplyOrIdError(x11rb::errors::ReplyOrIdError), |
| 51 | + DisplayOpenFailed, | |
| 52 | + FontLoadFailed(String), | |
| 53 | + DrawCreateFailed, | |
| 51 | 54 | } |
| 52 | 55 | |
| 53 | 56 | type WmResult<T> = Result<T, WmError>; |
| @@ -55,7 +58,6 @@ type WmResult<T> = Result<T, WmError>; | ||
| 55 | 58 | impl std::fmt::Display for WmError { |
| 56 | 59 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 57 | 60 | match self { |
| 58 | - // Self::X11(x11_error) => write!(f, "{:?}", x11_error), | |
| 59 | 61 | Self::X11(error) => write!(f, "{}", error), |
| 60 | 62 | Self::Io(error) => write!(f, "{}", error), |
| 61 | 63 | Self::Anyhow(error) => write!(f, "{}", error), |
| @@ -68,10 +70,13 @@ impl std::error::Error for WmError {} | ||
| 68 | 70 | impl std::fmt::Display for X11Error { |
| 69 | 71 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 70 | 72 | match self { |
| 71 | - X11Error::ConnectError(err) => write!(f, "{}", err), | |
| 72 | - X11Error::ConnectionError(err) => write!(f, "{}", err), | |
| 73 | - X11Error::ReplyError(err) => write!(f, "{}", err), | |
| 74 | - X11Error::ReplyOrIdError(err) => write!(f, "{}", err), | |
| 73 | + Self::ConnectError(err) => write!(f, "{}", err), | |
| 74 | + Self::ConnectionError(err) => write!(f, "{}", err), | |
| 75 | + Self::ReplyError(err) => write!(f, "{}", err), | |
| 76 | + Self::ReplyOrIdError(err) => write!(f, "{}", err), | |
| 77 | + Self::DisplayOpenFailed => write!(f, "failed to open X11 display"), | |
| 78 | + Self::FontLoadFailed(font_name) => write!(f, "failed to load Xft font: {}", font_name), | |
| 79 | + Self::DrawCreateFailed => write!(f, "failed to create XftDraw"), | |
| 75 | 80 | } |
| 76 | 81 | } |
| 77 | 82 | } |