From a037dc109c81b1fcfec1486b5f3da17a0176fbaa Mon Sep 17 00:00:00 2001 From: MaxVerevkin Date: Fri, 26 Apr 2024 22:06:20 +0300 Subject: [PATCH] simplify error handling --- src/themes/xresources.rs | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/themes/xresources.rs b/src/themes/xresources.rs index 83280b3f63..c77c6cf2fb 100644 --- a/src/themes/xresources.rs +++ b/src/themes/xresources.rs @@ -1,8 +1,10 @@ -use crate::errors::Error; +use std::collections::HashMap; + use log::debug; use once_cell::sync::Lazy; use regex::Regex; -use std::collections::HashMap; + +use crate::errors::*; #[cfg(not(test))] use std::{env, path::PathBuf}; @@ -23,18 +25,15 @@ use tests::read_xresources; static COLOR_REGEX: Lazy = Lazy::new(|| Regex::new(r"^\s*\*(?[^: ]+)\s*:\s*(?#[a-f0-9]{6,8}).*$").unwrap()); -static COLORS: Lazy, Error>> = - Lazy::new(|| match read_xresources() { - Ok(content) => { - debug!(".Xresources content:\n{}", content); - Ok(HashMap::from_iter(content.lines().filter_map(|line| { - COLOR_REGEX - .captures(line) - .map(|caps| (caps["name"].to_string(), caps["color"].to_string())) - }))) - } - Err(e) => Err(Error::new(format!("could not read .Xresources: {}", e))), - }); +static COLORS: Lazy, Error>> = Lazy::new(|| { + let content = read_xresources().error("could not read .Xresources")?; + debug!(".Xresources content:\n{}", content); + Ok(HashMap::from_iter(content.lines().filter_map(|line| { + COLOR_REGEX + .captures(line) + .map(|caps| (caps["name"].to_string(), caps["color"].to_string())) + }))) +}); pub fn get_color(name: &str) -> Result, Error> { COLORS