From fbafa1793501c84e5cc4227cb4aca14576a482bf Mon Sep 17 00:00:00 2001 From: NTBBloodbath Date: Thu, 13 Jun 2024 15:06:11 -0400 Subject: [PATCH] ref!: migrate to `eyre` from `anyhow`, closes #3 --- Cargo.lock | 24 +++++++++++++++++------- Cargo.toml | 2 +- src/cli.rs | 14 +++++--------- src/cmd/init.rs | 7 ++----- src/cmd/serve.rs | 11 +++++------ src/fs.rs | 2 +- src/main.rs | 2 +- 7 files changed, 32 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b5f324d..47b47d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -89,12 +89,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "anyhow" -version = "1.0.82" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" - [[package]] name = "autocfg" version = "1.2.0" @@ -376,6 +370,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "eyre" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec" +dependencies = [ + "indenter", + "once_cell", +] + [[package]] name = "fnv" version = "1.0.7" @@ -627,6 +631,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "indenter" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" + [[package]] name = "indexmap" version = "2.2.6" @@ -749,10 +759,10 @@ dependencies = [ name = "norgolith" version = "0.1.0" dependencies = [ - "anyhow", "chrono", "clap", "comfy-table", + "eyre", "hyper", "mockall", "tera", diff --git a/Cargo.toml b/Cargo.toml index 3954bab..6f22bfd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,10 +14,10 @@ include = ["src/**/*", "LICENSE", "README.md"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -anyhow = "1.0.82" chrono = "0.4.38" clap = { version = "4.5.4", features = ["deprecated", "derive", "env", "wrap_help"] } comfy-table = "7.1.1" +eyre = "0.6.12" hyper = { version = "=0.14.28", features = ["runtime", "server", "http1", "http2"] } tera = "1.19.1" tokio = { version = "1.37.0", features = ["fs", "time", "rt-multi-thread", "macros", "process"] } diff --git a/src/cli.rs b/src/cli.rs index 9795f82..79681b5 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Result}; +use eyre::{bail, Result}; use clap::{Parser, Subcommand}; use crate::cmd; @@ -53,7 +53,7 @@ pub async fn start() -> Result<()> { match &cli.command { Commands::Init { name } => init_site(name.as_ref()).await?, Commands::Serve { port } => check_and_serve(*port).await?, - _ => eprintln!("Unsupported command"), + _ => bail!("Unsupported command"), } Ok(()) @@ -70,8 +70,7 @@ async fn init_site(name: Option<&String>) -> Result<()> { if let Some(name) = name { cmd::init(name).await?; } else { - return Err(anyhow!("Missing name for the site") - .context("could not initialize the new Norgolith site")); + bail!("Missing name for the site: could not initialize the new Norgolith site"); } Ok(()) } @@ -91,10 +90,7 @@ async fn check_and_serve(port: u16) -> Result<()> { format!("requested port ({})", port) }; - return Err( - anyhow!("Failed to open listener, perhaps the {} is busy?", port_msg) - .context("could not initialize the development server"), - ); + bail!("Could not initialize the development server: failed to open listener, perhaps the {} is busy?", port_msg); } cmd::serve(port).await?; @@ -157,6 +153,6 @@ mod tests { .unwrap_err() .root_cause() .to_string() - .contains("Failed to open listener")); + .contains("failed to open listener")); } } diff --git a/src/cmd/init.rs b/src/cmd/init.rs index 3b9dfd5..dc214ce 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Result}; +use eyre::{bail, Result}; use comfy_table::modifiers::UTF8_SOLID_INNER_BORDERS; use comfy_table::presets::UTF8_FULL; use comfy_table::{Cell, ContentArrangement, Table}; @@ -66,10 +66,7 @@ pub async fn init(name: &String) -> Result<()> { if path_exists { // Get the canonical (absolute) path to the existing site root let path = fs::canonicalize(name).await?; - return Err( - anyhow!("The target directory {} already exists.", path.display()) - .context("could not initialize the new Norgolith site"), - ); + bail!("Could not initialize the new Norgolith site: the target directory {} already exists.", path.display()); } else { // Create site directories create_directories(name).await?; diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 637b869..9a63aa2 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -1,6 +1,6 @@ use std::convert::Infallible; -use anyhow::{anyhow, Result}; +use eyre::{bail, Result}; use hyper::service::{make_service_fn, service_fn}; use hyper::{Body, Request, Response, Server}; // use tokio::process::Command; @@ -47,12 +47,11 @@ pub async fn serve(port: u16) -> Result<()> { println!("Serving site ..."); println!("Web server is available at http://localhost:{:?}/", port); - server - .await - .map_err(|err| anyhow!("Server error: {}", err))?; + if let Err(err) = server.await { + bail!("Server error: {}", err) + } } else { - return Err(anyhow!("Not in a Norgolith site directory") - .context("could not initialize the development server")); + bail!("Could not initialize the development server: not in a Norgolith site directory"); } Ok(()) diff --git a/src/fs.rs b/src/fs.rs index e930c35..a93db62 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use anyhow::Result; +use eyre::Result; use tokio::fs::{metadata, read_dir}; #[cfg(test)] diff --git a/src/main.rs b/src/main.rs index 0e9a865..3e0bb06 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ mod cmd; mod fs; mod net; -use anyhow::Result; +use eyre::Result; #[tokio::main] async fn main() -> Result<()> {