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