Skip to content

Commit

Permalink
simplify error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxVerevkin committed Apr 26, 2024
1 parent 09fc0e0 commit a037dc1
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/themes/xresources.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -23,18 +25,15 @@ use tests::read_xresources;
static COLOR_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^\s*\*(?<name>[^: ]+)\s*:\s*(?<color>#[a-f0-9]{6,8}).*$").unwrap());

static COLORS: Lazy<Result<HashMap<String, String>, 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<Result<HashMap<String, String>, 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<Option<&String>, Error> {
COLORS
Expand Down

0 comments on commit a037dc1

Please sign in to comment.