From 672ca07a0e2e11572e345564f588e1653f94c019 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 18 Oct 2023 21:25:37 -0400 Subject: [PATCH] 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. --- Cargo.lock | 7 ++++++ Cargo.toml | 2 +- src/command/init/runner.rs | 15 ++++++------- src/config/full.rs | 32 ++++++++++++++++++++++++++++ src/config/mod.rs | 5 +++-- src/config/{definition.rs => raw.rs} | 6 +++--- 6 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 src/config/full.rs rename src/config/{definition.rs => raw.rs} (84%) diff --git a/Cargo.lock b/Cargo.lock index bd18f03..6983727 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", ] diff --git a/Cargo.toml b/Cargo.toml index edacbe1..44b8f2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/command/init/runner.rs b/src/command/init/runner.rs index 9c84b35..74cd176 100644 --- a/src/command/init/runner.rs +++ b/src/command/init/runner.rs @@ -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> { - 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(()) } diff --git a/src/config/full.rs b/src/config/full.rs new file mode 100644 index 0000000..e063959 --- /dev/null +++ b/src/config/full.rs @@ -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>(root_dir: P) -> Result> { + Ok(Config { + raw: RawConfig::default(), + root_dir: root_dir.into(), + }) + } + + pub(crate) async fn write_to_disk(&self) -> Result<(), Box> { + 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") + } +} diff --git a/src/config/mod.rs b/src/config/mod.rs index d6be996..6b51eb2 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,2 +1,3 @@ -mod definition; -pub(crate) use definition::Config; +mod full; +mod raw; +pub(crate) use full::Config; diff --git a/src/config/definition.rs b/src/config/raw.rs similarity index 84% rename from src/config/definition.rs rename to src/config/raw.rs index fb8c1bc..3d43bd5 100644 --- a/src/config/definition.rs +++ b/src/config/raw.rs @@ -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, email: Option, } -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,