100 lines
3.0 KiB
Rust
100 lines
3.0 KiB
Rust
use std::path::Path;
|
|
use std::path::PathBuf;
|
|
use tokio::fs::File;
|
|
use tokio::io::AsyncWriteExt;
|
|
|
|
use crate::error::CustomError;
|
|
|
|
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.
|
|
#[derive(Debug)]
|
|
pub(crate) struct Config {
|
|
raw: RawConfig,
|
|
config_path: PathBuf,
|
|
}
|
|
|
|
impl Config {
|
|
pub(crate) fn new<P: AsRef<Path>>(root_dir: P) -> Result<Config, CustomError> {
|
|
fn inner(root_dir: &Path) -> Result<Config, CustomError> {
|
|
let file_path = root_dir.join("natter.toml");
|
|
Ok(Config {
|
|
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, CustomError> {
|
|
async fn inner(path: PathBuf) -> Result<Config, CustomError> {
|
|
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<(), CustomError> {
|
|
let mut config_file = File::create(&self.config_path).await?;
|
|
config_file
|
|
.write_all(toml::to_string(&self.raw)?.as_bytes())
|
|
.await?;
|
|
Ok(())
|
|
}
|
|
|
|
pub(crate) fn get_root_directory(&self) -> &Path {
|
|
self.config_path
|
|
.parent()
|
|
.expect("Config file must exist inside a directory.")
|
|
}
|
|
|
|
pub(crate) fn get_posts_directory(&self) -> PathBuf {
|
|
self.get_root_directory().join("posts")
|
|
}
|
|
|
|
/// Get the relative path to the folder containing a blog post.
|
|
///
|
|
/// This could be appended to the output root directory to get the
|
|
/// blog post output folder or it could be used to generate a link
|
|
/// to the blog post.
|
|
pub(crate) fn get_relative_path_to_post<P: AsRef<Path>>(&self, post_id: P) -> PathBuf {
|
|
Path::new("posts").join(post_id)
|
|
}
|
|
|
|
pub(crate) fn get_output_directory(&self) -> PathBuf {
|
|
self.get_root_directory().join("output")
|
|
}
|
|
|
|
pub(crate) fn use_relative_paths(&self) -> bool {
|
|
self.raw.use_relative_paths.unwrap_or(true)
|
|
}
|
|
|
|
pub(crate) fn get_web_root(&self) -> Option<&str> {
|
|
self.raw.web_root.as_deref()
|
|
}
|
|
|
|
pub(crate) fn get_site_title(&self) -> Option<&str> {
|
|
self.raw.site_title.as_deref()
|
|
}
|
|
|
|
pub(crate) fn get_stream_entries_per_page(&self) -> usize {
|
|
self.raw
|
|
.stream
|
|
.as_ref()
|
|
.and_then(|stream| stream.entries_per_page)
|
|
.unwrap_or(5)
|
|
}
|
|
|
|
pub(crate) fn get_relative_path_to_static_files(&self) -> PathBuf {
|
|
Path::new("static").into()
|
|
}
|
|
|
|
pub(crate) fn get_relative_path_to_pages(&self) -> PathBuf {
|
|
Path::new("pages").into()
|
|
}
|
|
}
|