Compare commits
No commits in common. "273734c9ffc2cbecc8ea236e27147e25a421efe2" and "672ca07a0e2e11572e345564f588e1653f94c019" have entirely different histories.
273734c9ff
...
672ca07a0e
@ -1,4 +1,3 @@
|
|||||||
use std::path::Path;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use tokio::fs::File;
|
use tokio::fs::File;
|
||||||
use tokio::io::AsyncWriteExt;
|
use tokio::io::AsyncWriteExt;
|
||||||
@ -8,40 +7,26 @@ 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.
|
/// 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 {
|
pub(crate) struct Config {
|
||||||
raw: RawConfig,
|
raw: RawConfig,
|
||||||
config_path: PathBuf,
|
root_dir: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub(crate) fn new<P: AsRef<Path>>(root_dir: P) -> Result<Config, Box<dyn std::error::Error>> {
|
pub(crate) fn new<P: Into<PathBuf>>(root_dir: P) -> Result<Config, Box<dyn std::error::Error>> {
|
||||||
fn inner(root_dir: &Path) -> Result<Config, Box<dyn std::error::Error>> {
|
Ok(Config {
|
||||||
let file_path = root_dir.join("writer.toml");
|
raw: RawConfig::default(),
|
||||||
Ok(Config {
|
root_dir: root_dir.into(),
|
||||||
raw: RawConfig::default(),
|
})
|
||||||
config_path: file_path,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
inner(root_dir.as_ref())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) async fn load_from_file<P: Into<PathBuf>>(
|
|
||||||
path: P,
|
|
||||||
) -> Result<Config, Box<dyn std::error::Error>> {
|
|
||||||
async fn inner(path: PathBuf) -> 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())?;
|
|
||||||
Ok(Config {
|
|
||||||
raw: parsed_contents,
|
|
||||||
config_path: path,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
inner(path.into()).await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn write_to_disk(&self) -> Result<(), Box<dyn std::error::Error>> {
|
pub(crate) async fn write_to_disk(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let mut config_file = File::create(&self.config_path).await?;
|
let mut config_file = File::create(self.get_path_to_config_file()).await?;
|
||||||
config_file
|
config_file
|
||||||
.write_all(toml::to_string(&self.raw)?.as_bytes())
|
.write_all(toml::to_string(&self.raw)?.as_bytes())
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_path_to_config_file(&self) -> PathBuf {
|
||||||
|
self.root_dir.join("writer.toml")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user