From 5fbaf7153f51cd922b07b1720e4702dd16db25ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20P=C5=82aczek?= Date: Tue, 10 Sep 2024 01:23:06 +0100 Subject: [PATCH] refactor: remove anyhow --- Cargo.lock | 7 ------- Cargo.toml | 1 - src/main.rs | 40 ++++++++++++++++++++++------------------ 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e44ca84..5bfbfdb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -75,12 +75,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "anyhow" -version = "1.0.86" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" - [[package]] name = "arc-swap" version = "1.7.1" @@ -1024,7 +1018,6 @@ dependencies = [ name = "rspi-bios" version = "1.0.0" dependencies = [ - "anyhow", "askama", "axum", "axum-server", diff --git a/Cargo.toml b/Cargo.toml index 50e93ce..70a9623 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,6 @@ assets = [ ] [dependencies] -anyhow = "1.0.86" askama = { version = "0.12.1", default-features = false, features = ["config"] } axum = { version = "0.7.5", default-features = false, features = ["tokio", "http1"] } axum-server = { version = "0.7.1", features = ["tls-rustls"] } diff --git a/src/main.rs b/src/main.rs index addcdf8..383cd18 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,6 @@ use std::path::PathBuf; use std::{convert::Infallible, net::SocketAddr}; use std::{sync::Arc, time::Duration}; -use anyhow::{Context, Result}; - use axum::extract::ConnectInfo; use axum::response::sse::KeepAlive; use axum::response::Redirect; @@ -120,19 +118,30 @@ struct AppState { #[allow(clippy::too_many_lines)] #[tokio::main] -async fn main() -> Result<()> { +async fn main() { let args = Args::parse(); - let exe_path = env::current_exe().context("Failed to get exe path")?; + let exe_path = match env::current_exe() { + Ok(e) => e, + Err(e) => { + tracing::error!(error = %e, "Failed to get exe path"); + std::process::exit(1); + } + }; let log_path = get_log_path(&exe_path, &args.log_path, args.force_debug_local); - let log_file = std::fs::OpenOptions::new() + let log_file_result = std::fs::OpenOptions::new() .append(true) .create(true) - .open(&log_path) - .with_context(|| { - format!("Failed to open/create {log_path:?}, did you set the correct permissions?") - })?; + .open(&log_path); + + let log_file = match log_file_result { + Ok(l) => l, + Err(e) => { + tracing::error!(error = %e, "Failed to open/create {log_path:?}, did you set the correct permissions?"); + std::process::exit(1); + } + }; let subscriber = tracing_subscriber::registry() .with( @@ -176,9 +185,8 @@ async fn main() -> Result<()> { ) .await else { - const ERR_MSG: &str = "Failed to create TLS config, did you set the correct permissions? Did you put the .pem files in the correct place?"; - tracing::error!("{}", ERR_MSG.to_string()); - anyhow::bail!(ERR_MSG); + tracing::error!("Failed to create TLS config, did you set the correct permissions? Did you put the .pem files in the correct place?"); + std::process::exit(1); }; // Create a handle for our TLS server so the shutdown signal can all shutdown @@ -237,15 +245,11 @@ async fn main() -> Result<()> { .await; if let Err(e) = axum_result { - let err_msg: String = - format!("Failed to start HTTPS server at {addr}, did you set the correct permissions?"); - tracing::error!(error = %e, "{}", err_msg); - anyhow::bail!(err_msg); + tracing::error!(error = %e, "Failed to start HTTPS server at {addr}, did you set the correct permissions?"); + std::process::exit(1); }; tracing::info!("Server has been shut down."); - - Ok(()) } #[derive(Template)]