oxwm

https://git.tonybtw.com/oxwm.git git://git.tonybtw.com/oxwm.git
6,875 bytes raw
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
    NoConfigPathSet,
33
    NoConfigAtPath,
34
    CouldNotReadConfig(std::io::Error),
35
}
36
37
#[derive(Debug)]
38
pub enum BlockError {
39
    Io(io::Error),
40
    ParseInt(std::num::ParseIntError),
41
    MissingFile(String),
42
    InvalidData(String),
43
    CommandFailed(String),
44
}
45
46
pub enum MainError {
47
    CouldNotCreateConfigDir(std::io::Error),
48
    CouldNotWriteConfig(std::io::Error),
49
    FailedCheckExist(std::io::Error),
50
    FailedReadConfig(std::io::Error),
51
    FailedReadConfigTemplate(ConfigError),
52
    CouldNotStartWm(WmError),
53
    WmError(WmError),
54
    BadConfigPath,
55
    NoConfigPath,
56
    InvalidArguments,
57
    NoProgramName,
58
    NoConfigDir,
59
}
60
61
impl std::fmt::Display for WmError {
62
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63
        match self {
64
            Self::X11(error) => write!(f, "{}", error),
65
            Self::Io(error) => write!(f, "{}", error),
66
            Self::Config(error) => write!(f, "{}", error),
67
            Self::Block(error) => write!(f, "{}", error),
68
            Self::Autostart(command, error) => write!(
69
                f,
70
                "Failed to spawn autostart command '{}': {}",
71
                command, error
72
            ),
73
        }
74
    }
75
}
76
77
impl std::fmt::Display for X11Error {
78
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79
        match self {
80
            Self::ConnectError(err) => write!(f, "{}", err),
81
            Self::ConnectionError(err) => write!(f, "{}", err),
82
            Self::ReplyError(err) => write!(f, "{}", err),
83
            Self::ReplyOrIdError(err) => write!(f, "{}", err),
84
            Self::DisplayOpenFailed => write!(f, "failed to open X11 display"),
85
            Self::FontLoadFailed(font_name) => write!(f, "failed to load Xft font: {}", font_name),
86
            Self::DrawCreateFailed => write!(f, "failed to create XftDraw"),
87
        }
88
    }
89
}
90
91
impl std::fmt::Display for ConfigError {
92
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93
        match self {
94
            Self::LuaError(msg) => write!(f, "{}", msg),
95
            Self::InvalidModkey(msg) => write!(f, "{}", msg),
96
            Self::UnknownKey(msg) => write!(f, "{}", msg),
97
            Self::UnknownAction(msg) => write!(f, "{}", msg),
98
            Self::UnknownBlockCommand(msg) => write!(f, "{}", msg),
99
            Self::MissingCommandArg { command, field } => {
100
                write!(f, "{} command requires {}", command, field)
101
            }
102
            Self::ValidationError(msg) => write!(f, "{}", msg),
103
            Self::NoConfigPathSet => write!(
104
                f,
105
                "Could not find config file. Config path should've been set while loading"
106
            ),
107
            Self::NoConfigAtPath => write!(f, "Could not find config file, has it been moved?"),
108
            Self::CouldNotReadConfig(e) => write!(f, "Could not read config: {e}"),
109
        }
110
    }
111
}
112
113
impl std::fmt::Display for BlockError {
114
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
115
        match self {
116
            Self::Io(err) => write!(f, "Block I/O error: {}", err),
117
            Self::ParseInt(err) => write!(f, "Block parse error: {}", err),
118
            Self::MissingFile(path) => write!(f, "Block missing file: {}", path),
119
            Self::InvalidData(msg) => write!(f, "Block invalid data: {}", msg),
120
            Self::CommandFailed(msg) => write!(f, "Block command failed: {}", msg),
121
        }
122
    }
123
}
124
125
impl<T: Into<X11Error>> From<T> for WmError {
126
    fn from(value: T) -> Self {
127
        Self::X11(value.into())
128
    }
129
}
130
131
impl From<io::Error> for WmError {
132
    fn from(value: io::Error) -> Self {
133
        Self::Io(value)
134
    }
135
}
136
137
impl From<ConfigError> for WmError {
138
    fn from(value: ConfigError) -> Self {
139
        Self::Config(value)
140
    }
141
}
142
143
impl From<BlockError> for WmError {
144
    fn from(value: BlockError) -> Self {
145
        Self::Block(value)
146
    }
147
}
148
149
impl From<io::Error> for BlockError {
150
    fn from(value: io::Error) -> Self {
151
        BlockError::Io(value)
152
    }
153
}
154
155
impl From<std::num::ParseIntError> for BlockError {
156
    fn from(value: std::num::ParseIntError) -> Self {
157
        BlockError::ParseInt(value)
158
    }
159
}
160
161
impl From<x11rb::errors::ConnectError> for X11Error {
162
    fn from(value: x11rb::errors::ConnectError) -> Self {
163
        X11Error::ConnectError(value)
164
    }
165
}
166
167
impl From<x11rb::errors::ConnectionError> for X11Error {
168
    fn from(value: x11rb::errors::ConnectionError) -> Self {
169
        X11Error::ConnectionError(value)
170
    }
171
}
172
173
impl From<x11rb::errors::ReplyError> for X11Error {
174
    fn from(value: x11rb::errors::ReplyError) -> Self {
175
        X11Error::ReplyError(value)
176
    }
177
}
178
179
impl From<x11rb::errors::ReplyOrIdError> for X11Error {
180
    fn from(value: x11rb::errors::ReplyOrIdError) -> Self {
181
        X11Error::ReplyOrIdError(value)
182
    }
183
}
184
185
impl From<mlua::Error> for ConfigError {
186
    fn from(err: mlua::Error) -> Self {
187
        ConfigError::LuaError(err.to_string())
188
    }
189
}
190
191
pub trait LuaResultExt<T> {
192
    fn lua_context(self, context: &str) -> Result<T, ConfigError>;
193
}
194
195
impl<T> LuaResultExt<T> for Result<T, mlua::Error> {
196
    fn lua_context(self, context: &str) -> Result<T, ConfigError> {
197
        self.map_err(|e| ConfigError::LuaError(format!("{}: {}", context, e)))
198
    }
199
}
200
201
impl std::fmt::Debug for MainError {
202
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
203
        use MainError::*;
204
205
        match self {
206
            CouldNotCreateConfigDir(e)
207
            | CouldNotWriteConfig(e)
208
            | FailedCheckExist(e)
209
            | FailedReadConfig(e) => {
210
                write!(f, "{e}")
211
            }
212
            FailedReadConfigTemplate(e) => write!(f, "{e}"),
213
            CouldNotStartWm(e) | WmError(e) => write!(f, "{e}"),
214
            BadConfigPath => write!(f, "Given config path does not exist"),
215
            NoConfigPath => write!(f, "The --config switch requires a path value"),
216
            InvalidArguments => write!(f, "The arguments given are invalid try --help"),
217
            NoProgramName => write!(f, "Could not get the program name from the environment"),
218
            NoConfigDir => write!(f, "Could not get the config dir"),
219
        }
220
    }
221
}