Skip to content

Commit

Permalink
ref!: migrate to eyre from anyhow, closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
NTBBloodbath committed Jun 13, 2024
1 parent 116b5b0 commit fbafa17
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 30 deletions.
24 changes: 17 additions & 7 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
14 changes: 5 additions & 9 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Result};
use eyre::{bail, Result};
use clap::{Parser, Subcommand};

use crate::cmd;
Expand Down Expand Up @@ -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(())
Expand All @@ -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(())
}
Expand All @@ -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?;
Expand Down Expand Up @@ -157,6 +153,6 @@ mod tests {
.unwrap_err()
.root_cause()
.to_string()
.contains("Failed to open listener"));
.contains("failed to open listener"));
}
}
7 changes: 2 additions & 5 deletions src/cmd/init.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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?;
Expand Down
11 changes: 5 additions & 6 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::PathBuf;

use anyhow::Result;
use eyre::Result;
use tokio::fs::{metadata, read_dir};

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod cmd;
mod fs;
mod net;

use anyhow::Result;
use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
Expand Down

0 comments on commit fbafa17

Please sign in to comment.