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:
Tom Alexander 2023-10-18 21:25:37 -04:00
parent 6668af2025
commit 672ca07a0e
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
6 changed files with 52 additions and 15 deletions

7
Cargo.lock generated
View File

@ -80,6 +80,12 @@ dependencies = [
"rustc-demangle",
]
[[package]]
name = "bytes"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223"
[[package]]
name = "cc"
version = "1.0.83"
@ -332,6 +338,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f38200e3ef7995e5ef13baec2f432a6da0aa9ac495b2c0e8f3b7eec2c92d653"
dependencies = [
"backtrace",
"bytes",
"num_cpus",
"pin-project-lite",
]

View File

@ -11,6 +11,6 @@ clap = { version = "4.4.6", default-features = false, features = ["std", "color"
organic = "0.1.12"
# | alloc, rc, serde_derive, unstable
serde = { version = "1.0.189", default-features = false, features = ["std", "derive"] }
tokio = { version = "1.30.0", default-features = false, features = ["rt", "rt-multi-thread"] }
tokio = { version = "1.30.0", default-features = false, features = ["rt", "rt-multi-thread", "fs", "io-util"] }
# display, parse | indexmap, preserve_order
toml = "0.8.2"

View File

@ -1,25 +1,22 @@
use std::fs::File;
use std::io::Write;
use crate::cli::parameters::InitArgs;
use crate::config::Config;
pub(crate) async fn init_writer_folder(args: &InitArgs) -> Result<(), Box<dyn std::error::Error>> {
let new_config = Config::default();
if args.path.exists() && !args.path.is_dir() {
return Err("The supplied path exists but is not a directory. Aborting.".into());
}
if !args.path.exists() {
std::fs::create_dir_all(&args.path)?;
tokio::fs::create_dir_all(&args.path).await?;
}
let existing_entries = std::fs::read_dir(&args.path)?;
if existing_entries.count() != 0 {
let mut existing_entries = tokio::fs::read_dir(&args.path).await?;
let first_entry = existing_entries.next_entry().await?;
if let Some(_) = first_entry {
return Err("The directory is not empty. Aborting.".into());
}
let mut config_file = File::create(args.path.join("writer.toml"))?;
config_file.write_all(toml::to_string(&new_config)?.as_bytes())?;
let new_config = Config::new(&args.path)?;
new_config.write_to_disk().await?;
Ok(())
}

32
src/config/full.rs Normal file
View 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")
}
}

View File

@ -1,2 +1,3 @@
mod definition;
pub(crate) use definition::Config;
mod full;
mod raw;
pub(crate) use full::Config;

View File

@ -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,