Skip to content

Use eyre for error handling #1157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 155 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ name = "blog"
path = "src/blog.rs"

[dependencies]
color-eyre = "0.6.2"
eyre = "0.6.8"
handlebars = { version = "3", features = ["dir_source"] }
lazy_static = "1.4.0"
serde = "1.0"
Expand Down
6 changes: 3 additions & 3 deletions src/blog.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::error::Error;

#[path = "lib.rs"]
mod lib;

pub fn main() -> Result<(), Box<dyn Error>> {
pub fn main() -> eyre::Result<()> {
color_eyre::install()?;

lib::main()?;

println!("blog has been generated; you can now serve its content by running\n\
Expand Down
7 changes: 3 additions & 4 deletions src/blogs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::posts::Post;
use serde_derive::{Deserialize, Serialize};
use std::error::Error;
use std::path::{Path, PathBuf};

static MANIFEST_FILE: &str = "blog.yml";
Expand Down Expand Up @@ -46,7 +45,7 @@ pub(crate) struct Blog {
}

impl Blog {
fn load(prefix: PathBuf, dir: &Path) -> Result<Self, Box<dyn Error>> {
fn load(prefix: PathBuf, dir: &Path) -> eyre::Result<Self> {
let manifest_content = std::fs::read_to_string(dir.join(MANIFEST_FILE))?;
let manifest: Manifest = serde_yaml::from_str(&manifest_content)?;

Expand Down Expand Up @@ -122,7 +121,7 @@ impl Blog {

/// Recursively load blogs in a directory. A blog is a directory with a `blog.yml`
/// file inside it.
pub(crate) fn load(base: &Path) -> Result<Vec<Blog>, Box<dyn Error>> {
pub(crate) fn load(base: &Path) -> eyre::Result<Vec<Blog>> {
let mut blogs = Vec::new();
load_recursive(base, base, &mut blogs)?;
Ok(blogs)
Expand All @@ -132,7 +131,7 @@ fn load_recursive(
base: &Path,
current: &Path,
blogs: &mut Vec<Blog>,
) -> Result<(), Box<dyn Error>> {
) -> eyre::Result<()> {
for entry in std::fs::read_dir(current)? {
let path = entry?.path();
let file_type = path.metadata()?.file_type();
Expand Down
Loading