replace anyhow with eyre

This commit is contained in:
zyl 2024-06-14 13:36:41 -07:00
parent 00b5567769
commit 27bd18beea
No known key found for this signature in database
GPG key ID: FDAEE0976A45AAC3
12 changed files with 172 additions and 77 deletions

121
Cargo.lock generated
View file

@ -4,9 +4,9 @@ version = 3
[[package]] [[package]]
name = "addr2line" name = "addr2line"
version = "0.22.0" version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb"
dependencies = [ dependencies = [
"gimli", "gimli",
] ]
@ -44,12 +44,6 @@ version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f"
[[package]]
name = "anyhow"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
[[package]] [[package]]
name = "arraydeque" name = "arraydeque"
version = "0.5.1" version = "0.5.1"
@ -77,9 +71,9 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]] [[package]]
name = "backtrace" name = "backtrace"
version = "0.3.73" version = "0.3.71"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d"
dependencies = [ dependencies = [
"addr2line", "addr2line",
"cc", "cc",
@ -162,6 +156,33 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e769b5c8c8283982a987c6e948e540254f1058d5a74b8794914d4ef5fc2a24" checksum = "b9e769b5c8c8283982a987c6e948e540254f1058d5a74b8794914d4ef5fc2a24"
[[package]]
name = "color-eyre"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55146f5e46f237f7423d74111267d4597b59b0dad0ffaf7303bce9945d843ad5"
dependencies = [
"backtrace",
"color-spantrace",
"eyre",
"indenter",
"once_cell",
"owo-colors",
"tracing-error",
]
[[package]]
name = "color-spantrace"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd6be1b2a7e382e2b98b43b2adcca6bb0e465af0bdd38123873ae61eb17a72c2"
dependencies = [
"once_cell",
"owo-colors",
"tracing-core",
"tracing-error",
]
[[package]] [[package]]
name = "convert_case" name = "convert_case"
version = "0.4.0" version = "0.4.0"
@ -396,6 +417,16 @@ version = "4.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "426c23bd8fdd0cfde248e0481b4013cd186feb92fede97d6097d693abac6eeb2" checksum = "426c23bd8fdd0cfde248e0481b4013cd186feb92fede97d6097d693abac6eeb2"
[[package]]
name = "eyre"
version = "0.6.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec"
dependencies = [
"indenter",
"once_cell",
]
[[package]] [[package]]
name = "file-id" name = "file-id"
version = "0.1.0" version = "0.1.0"
@ -581,9 +612,9 @@ dependencies = [
[[package]] [[package]]
name = "gimli" name = "gimli"
version = "0.29.0" version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
[[package]] [[package]]
name = "grass" name = "grass"
@ -927,6 +958,12 @@ dependencies = [
"utf8_iter", "utf8_iter",
] ]
[[package]]
name = "indenter"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.2.6" version = "2.2.6"
@ -1222,9 +1259,9 @@ dependencies = [
[[package]] [[package]]
name = "object" name = "object"
version = "0.36.0" version = "0.32.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "576dfe1fc8f9df304abb159d767a29d0476f7750fbf8aa7ad07816004a207434" checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441"
dependencies = [ dependencies = [
"memchr", "memchr",
] ]
@ -1235,6 +1272,12 @@ version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "owo-colors"
version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.12.3" version = "0.12.3"
@ -1806,6 +1849,15 @@ dependencies = [
"digest", "digest",
] ]
[[package]]
name = "sharded-slab"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
dependencies = [
"lazy_static",
]
[[package]] [[package]]
name = "siphasher" name = "siphasher"
version = "0.3.11" version = "0.3.11"
@ -1914,6 +1966,16 @@ dependencies = [
"syn 2.0.66", "syn 2.0.66",
] ]
[[package]]
name = "thread_local"
version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c"
dependencies = [
"cfg-if",
"once_cell",
]
[[package]] [[package]]
name = "time" name = "time"
version = "0.3.36" version = "0.3.36"
@ -2041,6 +2103,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"valuable",
]
[[package]]
name = "tracing-error"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e"
dependencies = [
"tracing",
"tracing-subscriber",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b"
dependencies = [
"sharded-slab",
"thread_local",
"tracing-core",
] ]
[[package]] [[package]]
@ -2131,6 +2215,12 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"
@ -2513,8 +2603,9 @@ dependencies = [
name = "zyl-site" name = "zyl-site"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "color-eyre",
"extract-frontmatter", "extract-frontmatter",
"eyre",
"fs_extra", "fs_extra",
"futures", "futures",
"grass", "grass",

View file

@ -4,8 +4,9 @@ name = "zyl-site"
version = "0.1.0" version = "0.1.0"
[dependencies] [dependencies]
anyhow = "1" color-eyre = {version = "0.6", optional = true}
extract-frontmatter = "4" extract-frontmatter = "4"
eyre = "0.6"
fs_extra = "1.2" fs_extra = "1.2"
futures = {version = "0.3", optional = true} futures = {version = "0.3", optional = true}
grass = {version = "0.13", default-features = false} grass = {version = "0.13", default-features = false}
@ -33,5 +34,5 @@ walkdir = "2"
warp = {version = "0.3", optional = true} warp = {version = "0.3", optional = true}
[features] [features]
default = ["serve"] default = ["serve", "color-eyre"]
serve = ["futures", "hotwatch", "percent-encoding", "tokio", "warp"] serve = ["futures", "hotwatch", "percent-encoding", "tokio", "warp"]

View file

@ -42,7 +42,7 @@ pub struct BlogPostMetadata {
impl BlogPostMetadata { impl BlogPostMetadata {
/// Helper to get the CDN URL to the blog post's header image. /// Helper to get the CDN URL to the blog post's header image.
fn get_header_image(&self, site_config: &SiteConfig) -> anyhow::Result<String> { fn get_header_image(&self, site_config: &SiteConfig) -> eyre::Result<String> {
Ok(site_config.cdn_url(&self.header_image_file)?.to_string()) Ok(site_config.cdn_url(&self.header_image_file)?.to_string())
} }
} }
@ -66,7 +66,7 @@ impl ResourceMethods<BlogPostTemplateData> for ResourceMetadata<BlogPostMetadata
fn get_extra_resource_template_data( fn get_extra_resource_template_data(
&self, &self,
site_config: &SiteConfig, site_config: &SiteConfig,
) -> anyhow::Result<BlogPostTemplateData> { ) -> eyre::Result<BlogPostTemplateData> {
// TODO: render markdown // TODO: render markdown
Ok(BlogPostTemplateData { Ok(BlogPostTemplateData {
header_image: self.inner.get_header_image(site_config)?, header_image: self.inner.get_header_image(site_config)?,
@ -83,7 +83,7 @@ impl ResourceMethods<BlogPostTemplateData> for ResourceMetadata<BlogPostMetadata
}) })
} }
fn get_head_data(&self, site_config: &SiteConfig) -> anyhow::Result<String> { fn get_head_data(&self, site_config: &SiteConfig) -> eyre::Result<String> {
// TODO: update this so we're not just doing raw html injection lmao // TODO: update this so we're not just doing raw html injection lmao
Ok(format!( Ok(format!(
r#" r#"

View file

@ -2,7 +2,7 @@
use std::path::PathBuf; use std::path::PathBuf;
use anyhow::Context; use eyre::Context;
use gray_matter::{engine::YAML, Matter}; use gray_matter::{engine::YAML, Matter};
use handlebars::Handlebars; use handlebars::Handlebars;
use lol_html::{element, html_content::ContentType, HtmlRewriter, Settings}; use lol_html::{element, html_content::ContentType, HtmlRewriter, Settings};
@ -70,7 +70,7 @@ impl<'a> SiteBuilder<'a> {
} }
/// Prepares the site builder for use and sets up the build directory. /// Prepares the site builder for use and sets up the build directory.
pub fn prepare(mut self) -> anyhow::Result<Self> { pub fn prepare(mut self) -> eyre::Result<Self> {
if self.build_path.exists() { if self.build_path.exists() {
for entry in self.build_path.read_dir()? { for entry in self.build_path.read_dir()? {
let path = &entry?.path(); let path = &entry?.path();
@ -84,13 +84,13 @@ impl<'a> SiteBuilder<'a> {
} }
} }
} else { } else {
std::fs::create_dir(&self.build_path).context("Failed to create build directory")?; std::fs::create_dir(&self.build_path).wrap_err("Failed to create build directory")?;
} }
for (template_name, template_path) in &self.site.template_index { for (template_name, template_path) in &self.site.template_index {
self.reg self.reg
.register_template_file(template_name, template_path) .register_template_file(template_name, template_path)
.context("Failed to register template file")?; .wrap_err("Failed to register template file")?;
} }
let root_path = self.site.site_path.join(ROOT_PATH); let root_path = self.site.site_path.join(ROOT_PATH);
@ -104,21 +104,21 @@ impl<'a> SiteBuilder<'a> {
let images_path = self.build_path.join(crate::images::IMAGES_OUT_PATH); let images_path = self.build_path.join(crate::images::IMAGES_OUT_PATH);
if !images_path.exists() { if !images_path.exists() {
std::fs::create_dir(images_path).context("Failed to create images path")?; std::fs::create_dir(images_path).wrap_err("Failed to create images path")?;
} }
self.images_builder self.images_builder
.load_all(&self) .load_all(&self)
.context("Failed to load images metadata")?; .wrap_err("Failed to load images metadata")?;
self.blog_builder self.blog_builder
.load_all(&self) .load_all(&self)
.context("Failed to load blog metadata")?; .wrap_err("Failed to load blog metadata")?;
Ok(self) Ok(self)
} }
/// Function to rewrite HTML wow. /// Function to rewrite HTML wow.
pub fn rewrite_html(&self, html: String) -> anyhow::Result<String> { pub fn rewrite_html(&self, html: String) -> eyre::Result<String> {
let mut output = Vec::new(); let mut output = Vec::new();
let mut rewriter = HtmlRewriter::new( let mut rewriter = HtmlRewriter::new(
Settings { Settings {
@ -179,7 +179,7 @@ impl<'a> SiteBuilder<'a> {
&self, &self,
page_metadata: PageMetadata, page_metadata: PageMetadata,
page_html: &str, page_html: &str,
) -> anyhow::Result<String> { ) -> eyre::Result<String> {
self.build_page_raw_with_extra_data(page_metadata, page_html, ()) self.build_page_raw_with_extra_data(page_metadata, page_html, ())
} }
@ -189,7 +189,7 @@ impl<'a> SiteBuilder<'a> {
page_metadata: PageMetadata, page_metadata: PageMetadata,
page_html: &str, page_html: &str,
extra_data: T, extra_data: T,
) -> anyhow::Result<String> ) -> eyre::Result<String>
where where
T: Serialize, T: Serialize,
{ {
@ -226,7 +226,7 @@ impl<'a> SiteBuilder<'a> {
} }
/// Builds a standard page. /// Builds a standard page.
pub fn build_page(&self, page_name: &str) -> anyhow::Result<()> { pub fn build_page(&self, page_name: &str) -> eyre::Result<()> {
let page_path = self.site.page_index.get(page_name).expect("Missing page"); let page_path = self.site.page_index.get(page_name).expect("Missing page");
let input = std::fs::read_to_string(page_path) let input = std::fs::read_to_string(page_path)
@ -260,14 +260,14 @@ impl<'a> SiteBuilder<'a> {
} }
/// Builds the Sass styles in the site. /// Builds the Sass styles in the site.
pub fn build_sass(&self) -> anyhow::Result<()> { pub fn build_sass(&self) -> eyre::Result<()> {
let styles_path = self.build_path.join("styles"); let styles_path = self.build_path.join("styles");
if !styles_path.exists() { if !styles_path.exists() {
std::fs::create_dir(&styles_path)?; std::fs::create_dir(&styles_path)?;
} }
if self.serving { if self.serving {
util::remove_dir_contents(&styles_path) util::remove_dir_contents(&styles_path)
.context("Failed to remove old contents of styles directory")?; .wrap_err("Failed to remove old contents of styles directory")?;
} }
let sass_path = self.site.site_path.join(SASS_PATH); let sass_path = self.site.site_path.join(SASS_PATH);
for sheet in &self.site.config.sass_styles { for sheet in &self.site.config.sass_styles {
@ -277,7 +277,7 @@ impl<'a> SiteBuilder<'a> {
Ok(mut css) => { Ok(mut css) => {
if !self.serving { if !self.serving {
css = minifier::css::minify(&css) css = minifier::css::minify(&css)
.map_err(|err| anyhow::anyhow!(err))? .map_err(|err| eyre::anyhow!(err))?
.to_string(); .to_string();
} }
std::fs::write(styles_path.join(sheet).with_extension("css"), css) std::fs::write(styles_path.join(sheet).with_extension("css"), css)
@ -302,12 +302,12 @@ impl<'a> SiteBuilder<'a> {
} }
/// Builds the site's various image pages. /// Builds the site's various image pages.
pub fn build_images(&self) -> anyhow::Result<()> { pub fn build_images(&self) -> eyre::Result<()> {
self.images_builder.build_all(self) self.images_builder.build_all(self)
} }
/// Builds the site's blog. /// Builds the site's blog.
pub fn build_blog(&self) -> anyhow::Result<()> { pub fn build_blog(&self) -> eyre::Result<()> {
self.blog_builder.build_all(self) self.blog_builder.build_all(self)
} }
} }

View file

@ -5,7 +5,7 @@ use crate::{blog::BlogPostMetadata, builder::SiteBuilder, resource::ResourceTemp
#[derive(Debug)] #[derive(Debug)]
pub enum Extra { pub enum Extra {
HtmlModification(fn(page: String, builder: &SiteBuilder) -> anyhow::Result<String>), HtmlModification(fn(page: String, builder: &SiteBuilder) -> eyre::Result<String>),
} }
/// Gets the extra for the given value. /// Gets the extra for the given value.
@ -17,7 +17,7 @@ pub fn get_extra(extra: &str) -> Option<Extra> {
} }
/// Extra to add a sidebar to the index page with recent blog posts on it. /// Extra to add a sidebar to the index page with recent blog posts on it.
fn index(page: String, builder: &SiteBuilder) -> anyhow::Result<String> { fn index(page: String, builder: &SiteBuilder) -> eyre::Result<String> {
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct SidebarTemplateData<'r> { struct SidebarTemplateData<'r> {
// resources: Vec<&'r ResourceMetadata<BlogPostMetadata>>, // resources: Vec<&'r ResourceMetadata<BlogPostMetadata>>,

View file

@ -53,7 +53,7 @@ impl ResourceMethods<ImageTemplateData> for ResourceMetadata<ImageMetadata> {
fn get_extra_resource_template_data( fn get_extra_resource_template_data(
&self, &self,
site_config: &SiteConfig, site_config: &SiteConfig,
) -> anyhow::Result<ImageTemplateData> { ) -> eyre::Result<ImageTemplateData> {
Ok(ImageTemplateData { Ok(ImageTemplateData {
src: site_config.cdn_url(&self.inner.file)?.to_string(), src: site_config.cdn_url(&self.inner.file)?.to_string(),
}) })

View file

@ -13,7 +13,7 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use anyhow::Context; use eyre::Context;
use serde::Deserialize; use serde::Deserialize;
use serving::get_name; use serving::get_name;
use url::Url; use url::Url;
@ -51,7 +51,7 @@ pub struct SiteConfig {
impl SiteConfig { impl SiteConfig {
/// Gets a CDN url from the given file name. /// Gets a CDN url from the given file name.
pub fn cdn_url(&self, file: &str) -> anyhow::Result<Url> { pub fn cdn_url(&self, file: &str) -> eyre::Result<Url> {
Ok(self.cdn_url.join(&self.s3_prefix)?.join(file)?) Ok(self.cdn_url.join(&self.s3_prefix)?.join(file)?)
} }
} }
@ -86,24 +86,24 @@ pub struct Site {
impl Site { impl Site {
/// Creates a new site from the given path. /// Creates a new site from the given path.
pub fn new(site_path: &Path) -> anyhow::Result<Self> { pub fn new(site_path: &Path) -> eyre::Result<Self> {
let config: SiteConfig = serde_yaml::from_str( let config: SiteConfig = serde_yaml::from_str(
&std::fs::read_to_string(site_path.join("config.yaml")) &std::fs::read_to_string(site_path.join("config.yaml"))
.context("Failed to read site config")?, .wrap_err("Failed to read site config")?,
) )
.context("Failed to parse site config")?; .wrap_err("Failed to parse site config")?;
let mut template_index = HashMap::new(); let mut template_index = HashMap::new();
let templates_path = site_path.join(TEMPLATES_PATH); let templates_path = site_path.join(TEMPLATES_PATH);
for entry in WalkDir::new(&templates_path).into_iter() { for entry in WalkDir::new(&templates_path).into_iter() {
let entry = entry.context("Failed to read template entry")?; let entry = entry.wrap_err("Failed to read template entry")?;
let path = entry.path(); let path = entry.path();
if let Some(ext) = path.extension() { if let Some(ext) = path.extension() {
if ext == "hbs" && entry.file_type().is_file() { if ext == "hbs" && entry.file_type().is_file() {
let (_, name) = get_name( let (_, name) = get_name(
path.strip_prefix(&templates_path) path.strip_prefix(&templates_path)
.context("This really shouldn't have happened")?, .wrap_err("This really shouldn't have happened")?,
); );
template_index.insert(name, path.to_owned()); template_index.insert(name, path.to_owned());
} }
@ -113,14 +113,14 @@ impl Site {
let mut page_index = HashMap::new(); let mut page_index = HashMap::new();
let pages_path = site_path.join(PAGES_PATH); let pages_path = site_path.join(PAGES_PATH);
for entry in WalkDir::new(&pages_path).into_iter() { for entry in WalkDir::new(&pages_path).into_iter() {
let entry = entry.context("Failed to read page entry")?; let entry = entry.wrap_err("Failed to read page entry")?;
let path = entry.path(); let path = entry.path();
if let Some(ext) = path.extension() { if let Some(ext) = path.extension() {
if ext == "md" && entry.file_type().is_file() { if ext == "md" && entry.file_type().is_file() {
page_index.insert( page_index.insert(
path.strip_prefix(&pages_path) path.strip_prefix(&pages_path)
.context("This really shouldn't have happened")? .wrap_err("This really shouldn't have happened")?
.with_extension("") .with_extension("")
.to_string_lossy() .to_string_lossy()
.to_string(), .to_string(),
@ -139,7 +139,7 @@ impl Site {
} }
/// Builds the site once. /// Builds the site once.
pub fn build_once(self) -> anyhow::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)?;
@ -151,7 +151,7 @@ impl Site {
} }
/// Helper method to build all available pages. /// Helper method to build all available pages.
fn build_all_pages(&self, builder: &SiteBuilder) -> anyhow::Result<()> { fn build_all_pages(&self, builder: &SiteBuilder) -> eyre::Result<()> {
for page_name in self.page_index.keys() { for page_name in self.page_index.keys() {
builder.build_page(page_name)?; builder.build_page(page_name)?;
} }

View file

@ -27,7 +27,7 @@ pub fn render_basic_link_list(
builder: &SiteBuilder, builder: &SiteBuilder,
links: Vec<Link>, links: Vec<Link>,
title: &str, title: &str,
) -> anyhow::Result<String> { ) -> eyre::Result<String> {
let data = LinkTemplateData { links, title }; let data = LinkTemplateData { links, title };
let out = builder.reg.render("basic-link-list", &data)?; let out = builder.reg.render("basic-link-list", &data)?;
let out = builder.build_page_raw( let out = builder.build_page_raw(

View file

@ -11,7 +11,10 @@ enum Mode {
#[cfg(feature = "serve")] #[cfg(feature = "serve")]
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> eyre::Result<()> {
#[cfg(feature = "color-eyre")]
color_eyre::install()?;
let site = Site::new(&Path::new("site").canonicalize()?)?; let site = Site::new(&Path::new("site").canonicalize()?)?;
let mut mode = Mode::Build; let mut mode = Mode::Build;
@ -33,7 +36,7 @@ async fn main() -> anyhow::Result<()> {
} }
#[cfg(not(feature = "serve"))] #[cfg(not(feature = "serve"))]
fn main() -> anyhow::Result<()> { fn main() -> eyre::Result<()> {
let site = Site::new(&Path::new("site").canonicalize()?)?; let site = Site::new(&Path::new("site").canonicalize()?)?;
site.build_once()?; site.build_once()?;

View file

@ -5,7 +5,7 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use anyhow::Context; use eyre::Context;
use itertools::Itertools; use itertools::Itertools;
use pulldown_cmark::{Options, Parser}; use pulldown_cmark::{Options, Parser};
use rss::{validation::Validate, ChannelBuilder, ItemBuilder}; use rss::{validation::Validate, ChannelBuilder, ItemBuilder};
@ -72,9 +72,9 @@ where
{ {
fn get_short_desc(&self) -> String; fn get_short_desc(&self) -> String;
fn get_extra_resource_template_data(&self, site_config: &SiteConfig) -> anyhow::Result<E>; fn get_extra_resource_template_data(&self, site_config: &SiteConfig) -> eyre::Result<E>;
fn get_head_data(&self, _site_config: &SiteConfig) -> anyhow::Result<String> { fn get_head_data(&self, _site_config: &SiteConfig) -> eyre::Result<String> {
Ok(String::new()) Ok(String::new())
} }
} }
@ -158,14 +158,14 @@ where
} }
/// Loads resource metadata from the given path. /// Loads resource metadata from the given path.
fn load(builder: &SiteBuilder, path: &Path) -> anyhow::Result<(String, ResourceMetadata<M>)> { fn load(builder: &SiteBuilder, path: &Path) -> eyre::Result<(String, ResourceMetadata<M>)> {
let id = Self::get_id(path); let id = Self::get_id(path);
let input = std::fs::read_to_string(path)?; let input = std::fs::read_to_string(path)?;
let mut page = builder let mut page = builder
.matter .matter
.parse_with_struct::<ResourceMetadata<M>>(&input) .parse_with_struct::<ResourceMetadata<M>>(&input)
.ok_or_else(|| anyhow::anyhow!("Failed to parse resource front matter"))?; .ok_or_else(|| eyre::anyhow!("Failed to parse resource front matter"))?;
let parser = Parser::new_ext(&page.content, Options::all()); let parser = Parser::new_ext(&page.content, Options::all());
let mut html = String::new(); let mut html = String::new();
@ -177,7 +177,7 @@ where
} }
/// Loads all resource metadata from the given config. /// Loads all resource metadata from the given config.
pub fn load_all(&self, builder: &SiteBuilder) -> anyhow::Result<()> { pub fn load_all(&self, builder: &SiteBuilder) -> eyre::Result<()> {
let mut lmd = self.loaded_metadata.borrow_mut(); let mut lmd = self.loaded_metadata.borrow_mut();
lmd.clear(); lmd.clear();
for e in builder for e in builder
@ -213,7 +213,7 @@ where
builder: &SiteBuilder, builder: &SiteBuilder,
id: String, id: String,
resource: &ResourceMetadata<M>, resource: &ResourceMetadata<M>,
) -> anyhow::Result<()> { ) -> eyre::Result<()> {
let out_path = self.build_path(&builder.build_path, &id); let out_path = self.build_path(&builder.build_path, &id);
let out = { let out = {
let data = ResourceTemplateData { let data = ResourceTemplateData {
@ -240,7 +240,7 @@ where
Ok(()) Ok(())
} }
pub fn build_all(&self, builder: &SiteBuilder) -> anyhow::Result<()> { pub fn build_all(&self, builder: &SiteBuilder) -> eyre::Result<()> {
let out_short = builder.build_path.join(&self.config.output_path_short); let out_short = builder.build_path.join(&self.config.output_path_short);
let out_long = builder.build_path.join(&self.config.output_path_long); let out_long = builder.build_path.join(&self.config.output_path_long);
@ -276,7 +276,7 @@ where
tag: Option<&str>, tag: Option<&str>,
out_path: &Path, out_path: &Path,
items_per_page: usize, items_per_page: usize,
) -> anyhow::Result<()> ) -> eyre::Result<()>
where where
M: Serialize, M: Serialize,
E: Serialize, E: Serialize,
@ -415,7 +415,7 @@ where
.last_build_date(Some(OffsetDateTime::now_utc().format(&Rfc2822)?)) .last_build_date(Some(OffsetDateTime::now_utc().format(&Rfc2822)?))
.items(items) .items(items)
.build(); .build();
channel.validate().context("Failed to validate RSS feed")?; channel.validate().wrap_err("Failed to validate RSS feed")?;
let out = channel.to_string(); let out = channel.to_string();
std::fs::write(out_long.join("rss.xml"), out)?; std::fs::write(out_long.join("rss.xml"), out)?;

View file

@ -7,7 +7,7 @@ use std::{
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };
use anyhow::Context; use eyre::Context;
use futures::SinkExt; use futures::SinkExt;
use hotwatch::{EventKind, Hotwatch}; use hotwatch::{EventKind, Hotwatch};
use warp::{ use warp::{
@ -47,7 +47,7 @@ fn create(
path: &Path, path: &Path,
relative_path: &Path, relative_path: &Path,
build: bool, build: bool,
) -> anyhow::Result<()> { ) -> eyre::Result<()> {
if path.is_dir() { if path.is_dir() {
return Ok(()); return Ok(());
} }
@ -76,7 +76,7 @@ fn create(
builder.site.build_all_pages(builder)?; builder.site.build_all_pages(builder)?;
} else if let Ok(_sass_path) = relative_path.strip_prefix(SASS_PATH) { } else if let Ok(_sass_path) = relative_path.strip_prefix(SASS_PATH) {
if build { if build {
builder.build_sass().context("Failed to rebuild Sass")?; builder.build_sass().wrap_err("Failed to rebuild Sass")?;
} }
} else if let Ok(root_path) = relative_path.strip_prefix(ROOT_PATH) { } else if let Ok(root_path) = relative_path.strip_prefix(ROOT_PATH) {
std::fs::copy(path, builder.build_path.join(root_path))?; std::fs::copy(path, builder.build_path.join(root_path))?;
@ -94,7 +94,7 @@ fn create(
} }
/// Removes an existing resource. /// Removes an existing resource.
fn remove(builder: &mut SiteBuilder, path: &Path, relative_path: &Path) -> anyhow::Result<()> { fn remove(builder: &mut SiteBuilder, path: &Path, relative_path: &Path) -> eyre::Result<()> {
if path.is_dir() { if path.is_dir() {
return Ok(()); return Ok(());
} }
@ -111,9 +111,9 @@ fn remove(builder: &mut SiteBuilder, path: &Path, relative_path: &Path) -> anyho
builder builder
.site .site
.build_all_pages(builder) .build_all_pages(builder)
.context("Failed to rebuild pages")?; .wrap_err("Failed to rebuild pages")?;
} else if let Ok(_sass_path) = relative_path.strip_prefix(SASS_PATH) { } else if let Ok(_sass_path) = relative_path.strip_prefix(SASS_PATH) {
builder.build_sass().context("Failed to rebuild Sass")?; builder.build_sass().wrap_err("Failed to rebuild Sass")?;
} else if let Ok(root_path) = relative_path.strip_prefix(ROOT_PATH) { } else if let Ok(root_path) = relative_path.strip_prefix(ROOT_PATH) {
std::fs::remove_file(builder.build_path.join(root_path))?; std::fs::remove_file(builder.build_path.join(root_path))?;
} else if let Ok(_image_path) = relative_path.strip_prefix(crate::images::IMAGES_PATH) { } else if let Ok(_image_path) = relative_path.strip_prefix(crate::images::IMAGES_PATH) {
@ -136,7 +136,7 @@ fn skip_path(builder: &SiteBuilder, path: &Path) -> bool {
impl Site { impl Site {
/// Serves the site for development. Don't use this in production. /// Serves the site for development. Don't use this in production.
pub async fn serve(self) -> anyhow::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()?;
@ -149,11 +149,11 @@ impl Site {
eprintln!("Failed to build page {}: {}", page_name, e); eprintln!("Failed to build page {}: {}", page_name, e);
} }
} }
builder.build_sass().context("Failed to build Sass")?; builder.build_sass().wrap_err("Failed to build Sass")?;
builder builder
.build_images() .build_images()
.context("Failed to build image pages")?; .wrap_err("Failed to build image pages")?;
builder.build_blog().context("Failed to build blog")?; builder.build_blog().wrap_err("Failed to build blog")?;
// Map of websocket connections // Map of websocket connections
let peers: Arc<Mutex<HashMap<SocketAddr, WebSocket>>> = let peers: Arc<Mutex<HashMap<SocketAddr, WebSocket>>> =
@ -187,7 +187,7 @@ impl Site {
println!("CHANGED - {:?}", relp); println!("CHANGED - {:?}", relp);
create(&mut builder, path, &relp, true)?; create(&mut builder, path, &relp, true)?;
} }
Ok::<_, anyhow::Error>(true) Ok::<_, eyre::Error>(true)
} }
} }
EventKind::Create(_) => { EventKind::Create(_) => {
@ -235,7 +235,7 @@ impl Site {
for (addr, peer) in peers.iter_mut() { for (addr, peer) in peers.iter_mut() {
let task = async { let task = async {
peer.send(Message::text("reload".to_string())).await?; peer.send(Message::text("reload".to_string())).await?;
Ok::<_, anyhow::Error>(()) Ok::<_, eyre::Error>(())
}; };
to_remove.push(*addr); to_remove.push(*addr);
if let Err(e) = futures::executor::block_on(task) { if let Err(e) = futures::executor::block_on(task) {
@ -339,7 +339,7 @@ impl<'a> SiteBuilder<'a> {
&mut self, &mut self,
template_name: &str, template_name: &str,
template_path: &Path, template_path: &Path,
) -> anyhow::Result<()> { ) -> eyre::Result<()> {
self.reg self.reg
.register_template_file(template_name, template_path)?; .register_template_file(template_name, template_path)?;

View file

@ -3,7 +3,7 @@
use std::path::Path; use std::path::Path;
/// Simple helper to remove the contents of a directory without removing the directory itself. /// Simple helper to remove the contents of a directory without removing the directory itself.
pub fn remove_dir_contents(path: &Path) -> anyhow::Result<()> { pub fn remove_dir_contents(path: &Path) -> eyre::Result<()> {
for entry in path.read_dir()? { for entry in path.read_dir()? {
let entry = entry?; let entry = entry?;
let path = entry.path(); let path = entry.path();