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