Introduce a new config struct the lives above the raw disk implementation.
This should let us include values that would not be written to disk like the folder containing the config.
This commit is contained in:
32
src/config/full.rs
Normal file
32
src/config/full.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
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,
|
||||
root_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub(crate) fn new<P: Into<PathBuf>>(root_dir: P) -> Result<Config, Box<dyn std::error::Error>> {
|
||||
Ok(Config {
|
||||
raw: RawConfig::default(),
|
||||
root_dir: root_dir.into(),
|
||||
})
|
||||
}
|
||||
|
||||
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
|
||||
.write_all(toml::to_string(&self.raw)?.as_bytes())
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn get_path_to_config_file(&self) -> PathBuf {
|
||||
self.root_dir.join("writer.toml")
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
mod definition;
|
||||
pub(crate) use definition::Config;
|
||||
mod full;
|
||||
mod raw;
|
||||
pub(crate) use full::Config;
|
||||
|
||||
@@ -3,15 +3,15 @@ use serde::Serialize;
|
||||
|
||||
/// This is the struct for the writer.toml config file that ends up in each site's root directory.
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub(crate) struct Config {
|
||||
pub(crate) struct RawConfig {
|
||||
site_title: String,
|
||||
author: Option<String>,
|
||||
email: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
impl Default for RawConfig {
|
||||
fn default() -> Self {
|
||||
Config {
|
||||
RawConfig {
|
||||
site_title: "My super awesome website".to_owned(),
|
||||
author: None,
|
||||
email: None,
|
||||
Reference in New Issue
Block a user