<commit_before>extern crate toml;

use std::io::Read;
use std::env::var;
use std::path;
use std::fs::File;

pub struct Icons {
    pub occupied_focused: String,
    pub occupied_unfocused: String,
    pub free_focused: String,
    pub free_unfocused: String,
    pub urgent_focused: String,
    pub urgent_unfocused: String,
}

fn get_config_path() -> Option<String> {
    if let Ok(value) = var("XDG_CONFIG_HOME") {
        Some(value + &path::MAIN_SEPARATOR.to_string() + "rustlebar.toml")
    } else if let Ok(value) = var("HOME") {
        Some(value + &path::MAIN_SEPARATOR.to_string() + ".config" + &path::MAIN_SEPARATOR.to_string() + "rustlebar.toml")
    } else {
        None
    }
}

fn get_value(toml: &toml::Value, default: &str, value: &str) -> String {
    if let Some(item) = toml.lookup(value) {
        match item.as_str() {
            Some(string) => string.to_owned(),
            None => default.to_owned()
        }
    } else {
        default.to_owned()
    }
}

pub fn get_icons() -> Icons {
    let mut occupied_focused = "".to_owned();
    let mut occupied_unfocused = "".to_owned();
    let mut free_focused = "".to_owned();
    let mut free_unfocused = "".to_owned();
    let mut urgent_focused = "".to_owned();
    let mut urgent_unfocused = "".to_owned();

    let mut buffer = String::new();

    if let Some(path) = get_config_path() {
        if let Ok(mut file) = File::open(&path) {
            let _ = file.read_to_string(&mut buffer);
            let configuration: toml::Value = buffer.parse().unwrap();

            occupied_focused = get_value(&configuration, &occupied_focused, "icons.occupied_focused");
            occupied_unfocused = get_value(&configuration, &occupied_unfocused, "icons.occupied_unfocused");
            free_focused = get_value(&configuration, &free_focused, "icons.free_focused");
            free_unfocused = get_value(&configuration, &free_unfocused, "icons.free_unfocused");
            urgent_focused = get_value(&configuration, &urgent_focused, "icons.urgent_focused");
            urgent_unfocused = get_value(&configuration, &urgent_unfocused, "icons.urgent_unfocused");
        }
    }

    Icons {
        occupied_focused: occupied_focused,
        occupied_unfocused: occupied_unfocused,
        free_focused: free_focused,
        free_unfocused: free_unfocused,
        urgent_focused: urgent_focused,
        urgent_unfocused: urgent_unfocused,
    }
}<commit_msg>Opinionated cleanup with too few commits<commit_after>extern crate toml;

use std::io::Read;
use std::env;
use std::path::PathBuf;
use std::fs::File;

pub struct Icons {
    pub occupied_focused: String,
    pub occupied_unfocused: String,
    pub free_focused: String,
    pub free_unfocused: String,
    pub urgent_focused: String,
    pub urgent_unfocused: String,
}

fn if_readable(path: PathBuf) -> Option<PathBuf> { if path.exists() { Some(path) } else { None } }

fn get_config_path() -> Option<PathBuf> {
    let xdg_path = env::var("XDG_CONFIG_HOME").ok()
        .map(|v| PathBuf::from(v).join("rustlebar.toml"))
        .and_then(if_readable);

    let dot_home = env::var("HOME").ok()
        .map(|v| PathBuf::from(v).join(".config").join("rustlebar.toml"))
        .and_then(if_readable);

    xdg_path.or(dot_home)
}

fn get_value(toml: &toml::Value, default: &str, value: &str) -> String {
    toml.lookup(value)
        .and_then(toml::Value::as_str).map(str::to_owned)
        .unwrap_or_else(|| default.to_owned())
}

pub fn get_icons() -> Icons {
    let mut buffer = String::new();

    if let Some(mut f) = get_config_path().and_then(|p| File::open(p).ok()) {
        f.read_to_string(&mut buffer).expect("Can't read configuration file");
    }

    let configuration: toml::Value = buffer.parse().unwrap_or(toml::Value::Array(Vec::new()));

    Icons {
        occupied_focused: get_value(&configuration, "", "icons.occupied_focused"),
        occupied_unfocused: get_value(&configuration, "", "icons.occupied_unfocused"),
        free_focused: get_value(&configuration, "", "icons.free_focused"),
        free_unfocused: get_value(&configuration, "", "icons.free_unfocused"),
        urgent_focused: get_value(&configuration, "", "icons.urgent_focused"),
        urgent_unfocused: get_value(&configuration, "", "icons.urgent_unfocused"),
    }
}
{'fraction_non_alphanumeric': 0.14635838150289018, 'fraction_numerical': 0.0, 'mean_word_length': 3.577777777777778, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2057125', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1426, 'n_words': 281}