fey 0.0.2

fey is a fast and reliable username scanner
Documentation
use dirs::config_dir;
use std::path::PathBuf;
use std::fs;
use crate::utils::error::Error;
use crate::utils::error::Error::FatalError;
use crate::utils::helper;
//use crate::utils::data::get_data_file;

/* 
pub struct ConfigFileData{
}
*/

pub struct ConfigFile {
    pub path: PathBuf,
}

impl ConfigFile {

    /// creates a new config file at ~/.config/fey/config.toml
    /// creates the config dir at ~/.config/ if it does not exist
    pub fn new() -> helper::Result<ConfigFile> {

        let config_dir = match config_dir(){
            Some(path) => path,
            None => {
                fs::DirBuilder::new().create("~/.config/").unwrap_or_else(|e| {
                    panic!("Fatal: {}", e);
                });
                PathBuf::from("~/.config/")
            },
        };

        let fey_config_dir_path = config_dir.join("fey");
        let fey_config_file_path = fey_config_dir_path.join("config.toml");

        // check if the config dir already exist as to avoid overwriting it,
        // or unnecessarily creating it again
        if !fey_config_dir_path.exists() {

            // create fey dir because write does not create a dir
            match fs::DirBuilder::new().create(config_dir.join("fey")) {
                Ok(()) => (),
                Err(error) => {
                    let errormsg = format!("could not create .config/fey/: {}", error);
                    return Err(FatalError(errormsg))
                }
            }

            match fs::write(&fey_config_file_path, "") {
                Ok(()) => (),
                Err(error) => {
                    let errormsg = format!("could not create config file: {}", error);
                    return Err(Error::FileError(errormsg))
                },
            };
        }

        Ok(ConfigFile {
            path: fey_config_file_path,
        })
    }

    /* 
    /// writes the data exclusively to the config file
    /// contents will have the type ConfigFileData which is a struct whose fields are the same as the .toml fields
    pub fn write(config: ConfigFile, contents: &str) -> Result<(), Error>{

        fs::write(config.path, contents).unwrap();

        Ok(())
    }
    */
}