| 1 |
use std::io;
|
| 2 |
|
| 3 |
#[derive(Debug)]
|
| 4 |
pub enum WmError {
|
| 5 |
X11(X11Error),
|
| 6 |
Io(io::Error),
|
| 7 |
Config(ConfigError),
|
| 8 |
Block(BlockError),
|
| 9 |
Autostart(String, io::Error),
|
| 10 |
}
|
| 11 |
|
| 12 |
#[derive(Debug)]
|
| 13 |
pub enum X11Error {
|
| 14 |
ConnectError(x11rb::errors::ConnectError),
|
| 15 |
ConnectionError(x11rb::errors::ConnectionError),
|
| 16 |
ReplyError(x11rb::errors::ReplyError),
|
| 17 |
ReplyOrIdError(x11rb::errors::ReplyOrIdError),
|
| 18 |
DisplayOpenFailed,
|
| 19 |
FontLoadFailed(String),
|
| 20 |
DrawCreateFailed,
|
| 21 |
}
|
| 22 |
|
| 23 |
#[derive(Debug)]
|
| 24 |
pub enum ConfigError {
|
| 25 |
LuaError(String),
|
| 26 |
InvalidModkey(String),
|
| 27 |
UnknownKey(String),
|
| 28 |
UnknownAction(String),
|
| 29 |
UnknownBlockCommand(String),
|
| 30 |
MissingCommandArg { command: String, field: String },
|
| 31 |
ValidationError(String),
|
| 32 |
}
|
| 33 |
|
| 34 |
#[derive(Debug)]
|
| 35 |
pub enum BlockError {
|
| 36 |
Io(io::Error),
|
| 37 |
ParseInt(std::num::ParseIntError),
|
| 38 |
MissingFile(String),
|
| 39 |
InvalidData(String),
|
| 40 |
CommandFailed(String),
|
| 41 |
}
|
| 42 |
|
| 43 |
impl std::fmt::Display for WmError {
|
| 44 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
| 45 |
match self {
|
| 46 |
Self::X11(error) => write!(f, "{}", error),
|
| 47 |
Self::Io(error) => write!(f, "{}", error),
|
| 48 |
Self::Config(error) => write!(f, "{}", error),
|
| 49 |
Self::Block(error) => write!(f, "{}", error),
|
| 50 |
Self::Autostart(command, error) => write!(f, "Failed to spawn autostart command '{}': {}", command, error),
|
| 51 |
}
|
| 52 |
}
|
| 53 |
}
|
| 54 |
|
| 55 |
impl std::error::Error for WmError {}
|
| 56 |
|
| 57 |
impl std::fmt::Display for X11Error {
|
| 58 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
| 59 |
match self {
|
| 60 |
Self::ConnectError(err) => write!(f, "{}", err),
|
| 61 |
Self::ConnectionError(err) => write!(f, "{}", err),
|
| 62 |
Self::ReplyError(err) => write!(f, "{}", err),
|
| 63 |
Self::ReplyOrIdError(err) => write!(f, "{}", err),
|
| 64 |
Self::DisplayOpenFailed => write!(f, "failed to open X11 display"),
|
| 65 |
Self::FontLoadFailed(font_name) => write!(f, "failed to load Xft font: {}", font_name),
|
| 66 |
Self::DrawCreateFailed => write!(f, "failed to create XftDraw"),
|
| 67 |
}
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
impl std::error::Error for X11Error {}
|
| 72 |
|
| 73 |
impl std::fmt::Display for ConfigError {
|
| 74 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
| 75 |
match self {
|
| 76 |
Self::LuaError(msg) => write!(f, "{}", msg),
|
| 77 |
Self::InvalidModkey(msg) => write!(f, "{}", msg),
|
| 78 |
Self::UnknownKey(msg) => write!(f, "{}", msg),
|
| 79 |
Self::UnknownAction(msg) => write!(f, "{}", msg),
|
| 80 |
Self::UnknownBlockCommand(msg) => write!(f, "{}", msg),
|
| 81 |
Self::MissingCommandArg { command, field } => {
|
| 82 |
write!(f, "{} command requires {}", command, field)
|
| 83 |
}
|
| 84 |
Self::ValidationError(msg) => write!(f, "{}", msg),
|
| 85 |
}
|
| 86 |
}
|
| 87 |
}
|
| 88 |
|
| 89 |
impl std::error::Error for ConfigError {}
|
| 90 |
|
| 91 |
impl std::fmt::Display for BlockError {
|
| 92 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
| 93 |
match self {
|
| 94 |
Self::Io(err) => write!(f, "Block I/O error: {}", err),
|
| 95 |
Self::ParseInt(err) => write!(f, "Block parse error: {}", err),
|
| 96 |
Self::MissingFile(path) => write!(f, "Block missing file: {}", path),
|
| 97 |
Self::InvalidData(msg) => write!(f, "Block invalid data: {}", msg),
|
| 98 |
Self::CommandFailed(msg) => write!(f, "Block command failed: {}", msg),
|
| 99 |
}
|
| 100 |
}
|
| 101 |
}
|
| 102 |
|
| 103 |
impl std::error::Error for BlockError {}
|
| 104 |
|
| 105 |
impl<T: Into<X11Error>> From<T> for WmError {
|
| 106 |
fn from(value: T) -> Self {
|
| 107 |
Self::X11(value.into())
|
| 108 |
}
|
| 109 |
}
|
| 110 |
|
| 111 |
impl From<io::Error> for WmError {
|
| 112 |
fn from(value: io::Error) -> Self {
|
| 113 |
Self::Io(value)
|
| 114 |
}
|
| 115 |
}
|
| 116 |
|
| 117 |
impl From<ConfigError> for WmError {
|
| 118 |
fn from(value: ConfigError) -> Self {
|
| 119 |
Self::Config(value)
|
| 120 |
}
|
| 121 |
}
|
| 122 |
|
| 123 |
impl From<BlockError> for WmError {
|
| 124 |
fn from(value: BlockError) -> Self {
|
| 125 |
Self::Block(value)
|
| 126 |
}
|
| 127 |
}
|
| 128 |
|
| 129 |
impl From<io::Error> for BlockError {
|
| 130 |
fn from(value: io::Error) -> Self {
|
| 131 |
BlockError::Io(value)
|
| 132 |
}
|
| 133 |
}
|
| 134 |
|
| 135 |
impl From<std::num::ParseIntError> for BlockError {
|
| 136 |
fn from(value: std::num::ParseIntError) -> Self {
|
| 137 |
BlockError::ParseInt(value)
|
| 138 |
}
|
| 139 |
}
|
| 140 |
|
| 141 |
impl From<x11rb::errors::ConnectError> for X11Error {
|
| 142 |
fn from(value: x11rb::errors::ConnectError) -> Self {
|
| 143 |
X11Error::ConnectError(value)
|
| 144 |
}
|
| 145 |
}
|
| 146 |
|
| 147 |
impl From<x11rb::errors::ConnectionError> for X11Error {
|
| 148 |
fn from(value: x11rb::errors::ConnectionError) -> Self {
|
| 149 |
X11Error::ConnectionError(value)
|
| 150 |
}
|
| 151 |
}
|
| 152 |
|
| 153 |
impl From<x11rb::errors::ReplyError> for X11Error {
|
| 154 |
fn from(value: x11rb::errors::ReplyError) -> Self {
|
| 155 |
X11Error::ReplyError(value)
|
| 156 |
}
|
| 157 |
}
|
| 158 |
|
| 159 |
impl From<x11rb::errors::ReplyOrIdError> for X11Error {
|
| 160 |
fn from(value: x11rb::errors::ReplyOrIdError) -> Self {
|
| 161 |
X11Error::ReplyOrIdError(value)
|
| 162 |
}
|
| 163 |
}
|
| 164 |
|
| 165 |
impl From<mlua::Error> for ConfigError {
|
| 166 |
fn from(err: mlua::Error) -> Self {
|
| 167 |
ConfigError::LuaError(err.to_string())
|
| 168 |
}
|
| 169 |
}
|
| 170 |
|
| 171 |
pub trait LuaResultExt<T> {
|
| 172 |
fn lua_context(self, context: &str) -> Result<T, ConfigError>;
|
| 173 |
}
|
| 174 |
|
| 175 |
impl<T> LuaResultExt<T> for Result<T, mlua::Error> {
|
| 176 |
fn lua_context(self, context: &str) -> Result<T, ConfigError> {
|
| 177 |
self.map_err(|e| ConfigError::LuaError(format!("{}: {}", context, e)))
|
| 178 |
}
|
| 179 |
}
|