rwpspread 0.5.1

Multi-Monitor Wallpaper Spanning Utility
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;

pub struct Wpaperd;
impl Wpaperd {
    /// Build and save a new wpaperd config to disk
    pub fn new(
        path: &String,
        hash: &String,
        wallpapers: &HashMap<String, String>,
    ) -> Result<(), String> {
        // create a new config file
        let mut config_file = File::create(path).map_err(|_| "wpaperd: cant open config file")?;

        // write the hash first
        config_file
            .write(format!("# {}\n# DO NOT EDIT! AUTOGENERATED CONFIG!\n\n", hash).as_bytes())
            .map_err(|_| "wpaperd: TOML write error")?;

        // add default statement for image center
        config_file
            .write(format!("[default]\nmode = \"center\"\n\n").as_bytes())
            .map_err(|_| "wpaperd: TOML write error")?;

        // add monitor output sections
        for paper in wallpapers {
            config_file
                .write(format!("[{}]\npath = \"{}\"\n\n", paper.0, paper.1).as_bytes())
                .map_err(|_| "wpaperd: TOML write error")?;
        }

        // return
        Ok(())
    }
    /// Check and return if any existing config is found
    pub fn check_existing(path: &String, hash: &String) -> bool {
        // Open the file
        let read_file = std::fs::read_to_string(path).unwrap_or_default();

        // check if we find the correct hash
        if read_file.starts_with(&format!("# {}", hash)) {
            // hash matches, don't regenerate
            return true;
        }

        false
    }
}