diff --git a/Cargo.lock b/Cargo.lock index 3de1291..7bc4572 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "anyhow" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" + [[package]] name = "async-trait" version = "0.1.80" @@ -153,6 +159,7 @@ checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" name = "cards" version = "0.1.0" dependencies = [ + "anyhow", "axum", "futures", "rand", diff --git a/Cargo.toml b/Cargo.toml index 8af7f88..330d631 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,4 @@ tokio = { version = "1", features = ["full"] } tower = { version = "0.4", features = ["util"] } tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } +anyhow = "1.0.82" diff --git a/src/gamemaster.rs b/src/gamemaster.rs index 2620b8b..66a3aee 100644 --- a/src/gamemaster.rs +++ b/src/gamemaster.rs @@ -2,7 +2,7 @@ use crate::message_handler::*; //change this use rand::prelude::IteratorRandom; use rand::thread_rng; use serde::{Deserialize, Serialize}; -use serde_json::Result; +use anyhow::Result; /// A CAH white card #[derive(Debug, Serialize, Deserialize)] diff --git a/src/main.rs b/src/main.rs index b0a7600..14ac042 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,9 @@ +use anyhow::{Context, Result}; use axum::{response::Html, routing::get, Router}; use std::{ // collections::HashSet, - error::Error, fs, net::SocketAddr, - result::Result, sync::{Arc, Mutex}, }; use tokio::sync::broadcast; @@ -16,16 +15,18 @@ use crate::api::*; use crate::message_handler::*; /// Parse json for card data -fn load_json(path: &str) -> Result, Box> { - let data: String = fs::read_to_string(path)?; - let jayson: Vec = serde_json::from_str(&data)?; +fn load_json(path: &str) -> Result> { + let data: String = + fs::read_to_string(path).with_context(|| format!("Invalid JSON path: \"{}\"", path))?; + let jayson: Vec = + serde_json::from_str(&data).with_context(|| format!("\"{path}\" is invalid json"))?; Ok(jayson) } // this is still around just for reference #[allow(dead_code)] -fn test() -> Result<(), Box> { +fn test() -> Result<()> { // choose decks let cards_input_path: &str = "data/cah-cards-full.json"; @@ -96,7 +97,7 @@ async fn spawnclients() -> Html<&'static str> { } #[tokio::main] -async fn main() -> Result<(), Box> { +async fn main() -> Result<()> { // stuff for logging tracing_subscriber::registry() .with( @@ -127,7 +128,10 @@ async fn main() -> Result<(), Box> { .with_state(app_state); // send it - let listener = tokio::net::TcpListener::bind("0.0.0.0:3030").await?; + let address = "0.0.0.0:3030"; + let listener = tokio::net::TcpListener::bind(address) + .await + .with_context(|| format!("{} is not a valid bind address.", address))?; tracing::debug!("listening on {}", listener.local_addr()?); axum::serve( listener,