minor changes + re-add custom error type

This commit is contained in:
Akshat Deshpande 2022-07-28 14:22:10 -05:00
parent 9e21c0a02a
commit 65d2207a5f

View file

@ -45,6 +45,7 @@ pub enum XcrabError {
Toml(toml::de::Error),
Var(std::env::VarError),
ClientDoesntExist,
Custom(String),
}
impl From<BreadError> for XcrabError {
@ -71,8 +72,18 @@ impl From<std::env::VarError> for XcrabError {
}
}
impl From<String> for XcrabError {
fn from(v: String) -> Self {
Self::Custom(v)
}
}
lazy_static! {
pub static ref CONFIG: config::XcrabConfig = config::load_file().unwrap_or_default();
pub static ref CONFIG: config::XcrabConfig = config::load_file().unwrap_or_else(|e| {
println!("[CONFIG] Error parsing config: {e}");
println!("[CONFIG] Falling back to default config");
config::XcrabConfig::default()
});
}
impl Display for XcrabError {
@ -83,6 +94,7 @@ impl Display for XcrabError {
Self::Toml(te) => Display::fmt(&te, f)?,
Self::Var(ve) => Display::fmt(&ve, f)?,
Self::ClientDoesntExist => Display::fmt("client didn't exist", f)?,
Self::Custom(fe) => Display::fmt(fe, f)?,
};
Ok(())
@ -139,9 +151,8 @@ async fn main() -> Result<()> {
};
let keymap = keymap(&mut conn).await.unwrap_or_default();
let key_u32 = u32::from_str_radix("0078", 16).unwrap_or_default();
let key = keymap
.get(&key_u32)
.expect("yeah sorry that keysym didn't map out");
let key: &u8 = keymap
.get(&key_u32).ok_or(XcrabError::Custom("the keysym requested was not found in the keymap".to_string()))?;
conn.exchange_request_async(GrabKeyRequest {
req_type: 33,