move errors to `error` module
Browse files
+1 −1 src/bar/bar.rs
| @@ -1,7 +1,7 @@ | |||
| 1 | use super::blocks::Block; | 1 | 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::errors::X11Error; |
| 5 | use std::time::Instant; | 5 | use std::time::Instant; |
| 6 | use x11rb::COPY_DEPTH_FROM_PARENT; | 6 | use x11rb::COPY_DEPTH_FROM_PARENT; |
| 7 | use x11rb::connection::Connection; | 7 | use x11rb::connection::Connection; |
+1 −1 src/bar/font.rs
| @@ -3,7 +3,7 @@ use x11::xft::{XftColor, XftDraw, XftDrawStringUtf8, XftFont, XftFontOpenName}; | |||
| 3 | use x11::xlib::{Colormap, Display, Drawable, Visual}; | 3 | use x11::xlib::{Colormap, Display, Drawable, Visual}; |
| 4 | use x11::xrender::XRenderColor; | 4 | use x11::xrender::XRenderColor; |
| 5 | 5 | ||
| 6 | -use crate::window_manager::X11Error; | 6 | +use crate::errors::X11Error; |
| 7 | 7 | ||
| 8 | pub struct Font { | 8 | pub struct Font { |
| 9 | xft_font: *mut XftFont, | 9 | xft_font: *mut XftFont, |
+87 −0 src/errors.rs new file mode 100644
| @@ -0,0 +1,87 @@ | |||
| 1 | +#[derive(Debug)] | ||
| 2 | +pub enum WmError { | ||
| 3 | + X11(X11Error), | ||
| 4 | + Io(std::io::Error), | ||
| 5 | + Anyhow(anyhow::Error), | ||
| 6 | +} | ||
| 7 | + | ||
| 8 | +#[derive(Debug)] | ||
| 9 | +pub enum X11Error { | ||
| 10 | + ConnectError(x11rb::errors::ConnectError), | ||
| 11 | + ConnectionError(x11rb::errors::ConnectionError), | ||
| 12 | + ReplyError(x11rb::errors::ReplyError), | ||
| 13 | + ReplyOrIdError(x11rb::errors::ReplyOrIdError), | ||
| 14 | + DisplayOpenFailed, | ||
| 15 | + FontLoadFailed(String), | ||
| 16 | + DrawCreateFailed, | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +impl std::fmt::Display for WmError { | ||
| 20 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| 21 | + match self { | ||
| 22 | + Self::X11(error) => write!(f, "{}", error), | ||
| 23 | + Self::Io(error) => write!(f, "{}", error), | ||
| 24 | + Self::Anyhow(error) => write!(f, "{}", error), | ||
| 25 | + } | ||
| 26 | + } | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +impl std::error::Error for WmError {} | ||
| 30 | + | ||
| 31 | +impl std::fmt::Display for X11Error { | ||
| 32 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| 33 | + match self { | ||
| 34 | + Self::ConnectError(err) => write!(f, "{}", err), | ||
| 35 | + Self::ConnectionError(err) => write!(f, "{}", err), | ||
| 36 | + Self::ReplyError(err) => write!(f, "{}", err), | ||
| 37 | + Self::ReplyOrIdError(err) => write!(f, "{}", err), | ||
| 38 | + Self::DisplayOpenFailed => write!(f, "failed to open X11 display"), | ||
| 39 | + Self::FontLoadFailed(font_name) => write!(f, "failed to load Xft font: {}", font_name), | ||
| 40 | + Self::DrawCreateFailed => write!(f, "failed to create XftDraw"), | ||
| 41 | + } | ||
| 42 | + } | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +impl std::error::Error for X11Error {} | ||
| 46 | + | ||
| 47 | +impl<T: Into<X11Error>> From<T> for WmError { | ||
| 48 | + fn from(value: T) -> Self { | ||
| 49 | + Self::X11(value.into()) | ||
| 50 | + } | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +impl From<std::io::Error> for WmError { | ||
| 54 | + fn from(value: std::io::Error) -> Self { | ||
| 55 | + Self::Io(value) | ||
| 56 | + } | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +impl From<anyhow::Error> for WmError { | ||
| 60 | + fn from(value: anyhow::Error) -> Self { | ||
| 61 | + Self::Anyhow(value) | ||
| 62 | + } | ||
| 63 | +} | ||
| 64 | + | ||
| 65 | +impl From<x11rb::errors::ConnectError> for X11Error { | ||
| 66 | + fn from(value: x11rb::errors::ConnectError) -> Self { | ||
| 67 | + X11Error::ConnectError(value) | ||
| 68 | + } | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +impl From<x11rb::errors::ConnectionError> for X11Error { | ||
| 72 | + fn from(value: x11rb::errors::ConnectionError) -> Self { | ||
| 73 | + X11Error::ConnectionError(value) | ||
| 74 | + } | ||
| 75 | +} | ||
| 76 | + | ||
| 77 | +impl From<x11rb::errors::ReplyError> for X11Error { | ||
| 78 | + fn from(value: x11rb::errors::ReplyError) -> Self { | ||
| 79 | + X11Error::ReplyError(value) | ||
| 80 | + } | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | +impl From<x11rb::errors::ReplyOrIdError> for X11Error { | ||
| 84 | + fn from(value: x11rb::errors::ReplyOrIdError) -> Self { | ||
| 85 | + X11Error::ReplyOrIdError(value) | ||
| 86 | + } | ||
| 87 | +} | ||
+1 −1 src/keyboard/handlers.rs
| @@ -4,7 +4,7 @@ use std::process::Command; | |||
| 4 | use x11rb::connection::Connection; | 4 | use x11rb::connection::Connection; |
| 5 | use x11rb::protocol::xproto::*; | 5 | use x11rb::protocol::xproto::*; |
| 6 | 6 | ||
| 7 | -use crate::window_manager::X11Error; | 7 | +use crate::errors::X11Error; |
| 8 | 8 | ||
| 9 | #[derive(Debug, Copy, Clone)] | 9 | #[derive(Debug, Copy, Clone)] |
| 10 | pub enum KeyAction { | 10 | pub enum KeyAction { |
+1 −0 src/lib.rs
| @@ -1,4 +1,5 @@ | |||
| 1 | pub mod bar; | 1 | pub mod bar; |
| 2 | +pub mod errors; | ||
| 2 | pub mod keyboard; | 3 | pub mod keyboard; |
| 3 | pub mod layout; | 4 | pub mod layout; |
| 4 | pub mod window_manager; | 5 | pub mod window_manager; |
+1 −86 src/window_manager.rs
| @@ -1,5 +1,6 @@ | |||
| 1 | use crate::Config; | 1 | use crate::Config; |
| 2 | use crate::bar::Bar; | 2 | use crate::bar::Bar; |
| 3 | +use crate::errors::WmError; | ||
| 3 | use crate::keyboard::{self, Arg, KeyAction, handlers}; | 4 | use crate::keyboard::{self, Arg, KeyAction, handlers}; |
| 4 | use crate::layout::GapConfig; | 5 | use crate::layout::GapConfig; |
| 5 | use crate::layout::Layout; | 6 | use crate::layout::Layout; |
| @@ -34,94 +35,8 @@ pub struct WindowManager { | |||
| 34 | bar: Bar, | 35 | bar: Bar, |
| 35 | } | 36 | } |
| 36 | 37 | ||
| 37 | -#[derive(Debug)] | ||
| 38 | -pub enum WmError { | ||
| 39 | - X11(X11Error), | ||
| 40 | - Io(std::io::Error), | ||
| 41 | - Anyhow(anyhow::Error), | ||
| 42 | -} | ||
| 43 | - | ||
| 44 | -#[derive(Debug)] | ||
| 45 | -pub enum X11Error { | ||
| 46 | - ConnectError(x11rb::errors::ConnectError), | ||
| 47 | - ConnectionError(x11rb::errors::ConnectionError), | ||
| 48 | - ReplyError(x11rb::errors::ReplyError), | ||
| 49 | - ReplyOrIdError(x11rb::errors::ReplyOrIdError), | ||
| 50 | - DisplayOpenFailed, | ||
| 51 | - FontLoadFailed(String), | ||
| 52 | - DrawCreateFailed, | ||
| 53 | -} | ||
| 54 | - | ||
| 55 | type WmResult<T> = Result<T, WmError>; | 38 | type WmResult<T> = Result<T, WmError>; |
| 56 | 39 | ||
| 57 | -impl std::fmt::Display for WmError { | ||
| 58 | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| 59 | - match self { | ||
| 60 | - Self::X11(error) => write!(f, "{}", error), | ||
| 61 | - Self::Io(error) => write!(f, "{}", error), | ||
| 62 | - Self::Anyhow(error) => write!(f, "{}", error), | ||
| 63 | - } | ||
| 64 | - } | ||
| 65 | -} | ||
| 66 | - | ||
| 67 | -impl std::error::Error for WmError {} | ||
| 68 | - | ||
| 69 | -impl std::fmt::Display for X11Error { | ||
| 70 | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| 71 | - match self { | ||
| 72 | - Self::ConnectError(err) => write!(f, "{}", err), | ||
| 73 | - Self::ConnectionError(err) => write!(f, "{}", err), | ||
| 74 | - Self::ReplyError(err) => write!(f, "{}", err), | ||
| 75 | - Self::ReplyOrIdError(err) => write!(f, "{}", err), | ||
| 76 | - Self::DisplayOpenFailed => write!(f, "failed to open X11 display"), | ||
| 77 | - Self::FontLoadFailed(font_name) => write!(f, "failed to load Xft font: {}", font_name), | ||
| 78 | - Self::DrawCreateFailed => write!(f, "failed to create XftDraw"), | ||
| 79 | - } | ||
| 80 | - } | ||
| 81 | -} | ||
| 82 | - | ||
| 83 | -impl<T: Into<X11Error>> From<T> for WmError { | ||
| 84 | - fn from(value: T) -> Self { | ||
| 85 | - Self::X11(value.into()) | ||
| 86 | - } | ||
| 87 | -} | ||
| 88 | - | ||
| 89 | -impl From<std::io::Error> for WmError { | ||
| 90 | - fn from(value: std::io::Error) -> Self { | ||
| 91 | - Self::Io(value) | ||
| 92 | - } | ||
| 93 | -} | ||
| 94 | - | ||
| 95 | -impl From<anyhow::Error> for WmError { | ||
| 96 | - fn from(value: anyhow::Error) -> Self { | ||
| 97 | - Self::Anyhow(value) | ||
| 98 | - } | ||
| 99 | -} | ||
| 100 | - | ||
| 101 | -impl From<x11rb::errors::ConnectError> for X11Error { | ||
| 102 | - fn from(value: x11rb::errors::ConnectError) -> Self { | ||
| 103 | - X11Error::ConnectError(value) | ||
| 104 | - } | ||
| 105 | -} | ||
| 106 | - | ||
| 107 | -impl From<x11rb::errors::ConnectionError> for X11Error { | ||
| 108 | - fn from(value: x11rb::errors::ConnectionError) -> Self { | ||
| 109 | - X11Error::ConnectionError(value) | ||
| 110 | - } | ||
| 111 | -} | ||
| 112 | - | ||
| 113 | -impl From<x11rb::errors::ReplyError> for X11Error { | ||
| 114 | - fn from(value: x11rb::errors::ReplyError) -> Self { | ||
| 115 | - X11Error::ReplyError(value) | ||
| 116 | - } | ||
| 117 | -} | ||
| 118 | - | ||
| 119 | -impl From<x11rb::errors::ReplyOrIdError> for X11Error { | ||
| 120 | - fn from(value: x11rb::errors::ReplyOrIdError) -> Self { | ||
| 121 | - X11Error::ReplyOrIdError(value) | ||
| 122 | - } | ||
| 123 | -} | ||
| 124 | - | ||
| 125 | impl WindowManager { | 40 | impl WindowManager { |
| 126 | pub fn new(config: Config) -> WmResult<Self> { | 41 | pub fn new(config: Config) -> WmResult<Self> { |
| 127 | let (connection, screen_number) = x11rb::connect(None)?; | 42 | let (connection, screen_number) = x11rb::connect(None)?; |