feat: Page::write method

This commit is contained in:
Nathan McCarty 2023-03-16 15:42:05 -04:00
parent 2697989bcc
commit 71f8f71b36
Signed by: thatonelutenist
GPG Key ID: D70DA3DD4D1E9F96
2 changed files with 57 additions and 4 deletions

View File

@ -17,6 +17,7 @@ pub mod page;
/// Error encountered interacting with a [`Site`]
#[derive(Snafu, Debug)]
#[snafu(visibility(pub(crate)))]
pub enum SiteError {
/// Error checking to see if a path exists
#[snafu(display("Error checking if path exists: {:?}", path))]
@ -53,6 +54,13 @@ pub enum SiteError {
/// The underlying error
source: std::io::Error,
},
/// Failed to write out a page stub
WritePageStub {
/// The path we tried to write the page stub to
path: PathBuf,
/// The underlying error
source: std::io::Error,
},
/// Error writing out a page
#[snafu(display("Error writing out page {:?}", page))]
PageWrite {

View File

@ -1,10 +1,19 @@
//! Management of a page
use std::path::{Path, PathBuf};
use std::{
fs::{create_dir_all, File},
io::Write,
path::{Path, PathBuf},
};
use serde::{Deserialize, Serialize};
use snafu::{ensure, ResultExt};
use tracing::{debug, instrument};
use super::SiteError;
use super::{
ConfigReifySnafu, CreateDirectorySnafu, ExistanceCheckSnafu, NotADirectorySnafu, SiteError,
WriteConfigSnafu, WritePageStubSnafu,
};
/// Representation of the configuration for a page
#[derive(Serialize, Deserialize, Debug)]
@ -57,11 +66,47 @@ impl Page {
///
/// Will return an error if serialization fails, the user does not have write permissions for
/// the directory, or any other IO errors occur while writing out the config.
#[instrument(skip(site_path, page_dir))]
pub fn write(
&self,
site_path: impl AsRef<Path>,
page_path: impl AsRef<Path>,
page_dir: impl AsRef<Path>,
) -> Result<(), SiteError> {
todo!()
// build the page_dir
let page_dir = site_path.as_ref().join("pages").join(page_dir.as_ref());
debug!(?page_dir);
// Make sure the page path is a dir if it exists, otherwise create it
if page_dir
.try_exists()
.context(ExistanceCheckSnafu { path: &page_dir })?
{
ensure!(page_dir.is_dir(), NotADirectorySnafu { path: &page_dir });
} else {
create_dir_all(&page_dir).context(CreateDirectorySnafu { path: &page_dir })?;
}
// Serialize the config
let config_path = page_dir.join("page.dhall");
let serialized_config = serde_dhall::serialize(&self.config)
.to_string()
.context(ConfigReifySnafu)?;
let mut config_file =
File::create(&config_path).context(WriteConfigSnafu { path: &config_path })?;
config_file
.write_all(serialized_config.as_bytes())
.context(WriteConfigSnafu { path: &config_path })?;
// Create the page file if it doesn't exist
let page_path = page_dir.join(&self.file);
if !page_path
.try_exists()
.context(ExistanceCheckSnafu { path: &page_dir })?
{
let contents = format!("# {}\n\n", self.config.title);
let mut page_file =
File::create(&page_path).context(WritePageStubSnafu { path: &page_path })?;
page_file
.write_all(contents.as_bytes())
.context(WritePageStubSnafu { path: &page_path })?;
}
Ok(())
}
}