Add a function to load a config from file.

This commit is contained in:
Tom Alexander 2023-10-20 18:39:26 -04:00
parent 672ca07a0e
commit 4314937d26
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
1 changed files with 18 additions and 0 deletions

View File

@ -1,3 +1,4 @@
use std::path::Path;
use std::path::PathBuf;
use tokio::fs::File;
use tokio::io::AsyncWriteExt;
@ -18,6 +19,23 @@ impl Config {
})
}
pub(crate) async fn load_from_file<P: AsRef<Path>>(
path: P,
) -> Result<Config, Box<dyn std::error::Error>> {
async fn inner(path: &Path) -> Result<Config, Box<dyn std::error::Error>> {
let contents = tokio::fs::read_to_string(path).await?;
let parsed_contents: RawConfig = toml::from_str(contents.as_str())?;
let root_directory = path
.parent()
.expect("Writer config must exist inside a directory.");
Ok(Config {
raw: parsed_contents,
root_dir: root_directory.to_owned(),
})
}
inner(path.as_ref()).await
}
pub(crate) async fn write_to_disk(&self) -> Result<(), Box<dyn std::error::Error>> {
let mut config_file = File::create(self.get_path_to_config_file()).await?;
config_file