use std::path::Path; use std::path::PathBuf; use tokio::fs::File; use tokio::io::AsyncWriteExt; use super::raw::RawConfig; /// This is the config struct used by most of the code, which is an interpreted version of the RawConfig struct which is the raw disk-representation of the config. pub(crate) struct Config { raw: RawConfig, config_path: PathBuf, } impl Config { pub(crate) fn new>(root_dir: P) -> Result> { fn inner(root_dir: &Path) -> Result> { let file_path = root_dir.join("writer.toml"); Ok(Config { raw: RawConfig::default(), config_path: file_path, }) } inner(root_dir.as_ref()) } pub(crate) async fn load_from_file>( path: P, ) -> Result> { async fn inner(path: PathBuf) -> Result> { let contents = tokio::fs::read_to_string(&path).await?; let parsed_contents: RawConfig = toml::from_str(contents.as_str())?; Ok(Config { raw: parsed_contents, config_path: path, }) } inner(path.into()).await } pub(crate) async fn write_to_disk(&self) -> Result<(), Box> { let mut config_file = File::create(&self.config_path).await?; config_file .write_all(toml::to_string(&self.raw)?.as_bytes()) .await?; Ok(()) } pub(crate) fn get_root_directory(&self) -> &Path { &self .config_path .parent() .expect("Config file must exist inside a directory.") } }