use std::fs::File;
use std::io::{Write, Read};
use std::path::Path;
use directories::BaseDirs;
pub mod installation;
pub fn read(file: &str) -> Result<String, String> {
if let Ok(mut file) = File::open(file) {
let mut contents = String::new();
if let Ok(_) = file.read_to_string(&mut contents) {
return Ok(contents)
}
}
Err(format!("Could not read file {}.", file))
}
pub fn write(file: &str, data: &str) -> Result<(), String> {
if let Ok(mut open) = File::create(file) {
if let Ok(_) = open.write_all(data.as_bytes()) {
return Ok(())
}
}
Err(format!("Could not write file {}.", file))
}
fn appdata() -> Result<String, String> {
if let Some(base_dirs) = BaseDirs::new() {
let path = base_dirs.config_dir();
if path.exists() {
let formatted = path_as_string(path)?;
return Ok(formatted)
}
}
Err(String::from("os appdata path not found."))
}
pub fn path_as_string(path: &Path) -> Result<String, String> {
if let Some(formatted) = path.to_str() {
return Ok(String::from(formatted))
}
Err(String::from("could not get path as string."))
}