make SiteBuilder creation fallible

This commit is contained in:
zyl 2024-11-07 17:20:43 -08:00
parent f4a2708794
commit 190cba1f3a
Signed by: zyl
SSH key fingerprint: SHA256:uxxbSXbdroP/OnKBGnEDk5q7EKB2razvstC/KmzdXXs
3 changed files with 6 additions and 7 deletions

View file

@ -54,7 +54,7 @@ pub struct SiteBuilder {
impl SiteBuilder { impl SiteBuilder {
/// Creates a new site builder. /// Creates a new site builder.
pub fn new(site: Site, serving: bool) -> Self { pub fn new(site: Site, serving: bool) -> eyre::Result<Self> {
let mut build_path = match &site.config.build { let mut build_path = match &site.config.build {
Some(build) => site.site_path.join(build), Some(build) => site.site_path.join(build),
_ => site.site_path.join("build"), _ => site.site_path.join("build"),
@ -68,11 +68,10 @@ impl SiteBuilder {
.join(format!("{}/**/*.tera", crate::TEMPLATES_PATH)) .join(format!("{}/**/*.tera", crate::TEMPLATES_PATH))
.to_str() .to_str()
.expect("failed to convert path to string"), .expect("failed to convert path to string"),
) )?;
.expect("failed to create tera instance");
tera.autoescape_on(vec![".tera"]); tera.autoescape_on(vec![".tera"]);
Self { Ok(Self {
tera, tera,
syntax_set: SyntaxSet::load_defaults_newlines(), syntax_set: SyntaxSet::load_defaults_newlines(),
theme_set: ThemeSet::load_defaults(), theme_set: ThemeSet::load_defaults(),
@ -80,7 +79,7 @@ impl SiteBuilder {
site, site,
build_path, build_path,
serving, serving,
} })
} }
/// Prepares the site builder for use and sets up the build directory. /// Prepares the site builder for use and sets up the build directory.

View file

@ -159,7 +159,7 @@ impl Site {
/// Builds the site once. /// Builds the site once.
pub fn build_once(self) -> eyre::Result<()> { pub fn build_once(self) -> eyre::Result<()> {
let builder = SiteBuilder::new(self, false).prepare()?; let builder = SiteBuilder::new(self, false)?.prepare()?;
builder.site.build_all_pages(&builder)?; builder.site.build_all_pages(&builder)?;
builder.build_sass()?; builder.build_sass()?;

View file

@ -150,7 +150,7 @@ impl Site {
pub async fn serve(self) -> eyre::Result<()> { pub async fn serve(self) -> eyre::Result<()> {
let addr = SocketAddr::from(([127, 0, 0, 1], 8080)); let addr = SocketAddr::from(([127, 0, 0, 1], 8080));
let mut builder = SiteBuilder::new(self, true).prepare()?; let mut builder = SiteBuilder::new(self, true)?.prepare()?;
let site = &builder.site; let site = &builder.site;
let build_path = builder.build_path.clone(); let build_path = builder.build_path.clone();