pub mod structs;
use once_cell::sync::Lazy;
use std::{
fs::{create_dir_all, File, OpenOptions},
io::{Result, Seek, Write},
path::{Path, PathBuf},
};
pub static DEFAULT_CONFIG_PATH: Lazy<PathBuf> = Lazy::new(|| {
crate::HOME
.join(".config")
.join("ferium")
.join("config.json")
});
fn open_config_file(path: &Path) -> Result<File> {
OpenOptions::new()
.read(true)
.write(true)
.truncate(false)
.create(true)
.open(path)
}
pub fn get_file(path: &Path) -> Result<File> {
if path.exists() {
open_config_file(path)
} else {
create_dir_all(path.parent().unwrap())?;
let mut file = open_config_file(path)?;
write_file(&mut file, &structs::Config::default())?;
Ok(file)
}
}
pub fn deserialise(input: &str) -> serde_json::error::Result<structs::Config> {
serde_json::from_str(input)
}
pub fn write_file(config_file: &mut File, config: &structs::Config) -> Result<()> {
let serialised = serde_json::to_string_pretty(config)?;
config_file.set_len(0)?; config_file.rewind()?; config_file.write_all(serialised.as_bytes())?;
config_file.rewind()?; Ok(())
}