From d543cd43d1942f3e5dc7502efb6a2549dbdaf5d7 Mon Sep 17 00:00:00 2001 From: Stefan Bossbaly Date: Thu, 9 May 2024 23:21:09 -0400 Subject: [PATCH] Reorganize modules A flat heirarchy makes more sense. --- Cargo.toml | 1 + examples/filter_trains.rs | 2 +- examples/single_train.rs | 2 +- src/client.rs | 4 ++-- src/errors.rs | 41 +++++++-------------------------------- src/lib.rs | 8 +++++--- tests/live_endpoint.rs | 4 ++-- tests/station.rs | 4 ++-- tests/train.rs | 6 +++--- 9 files changed, 24 insertions(+), 48 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c9b4044..1676179 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ reqwest = { version = "0.12.4", features = ["json"] } serde_json = "1.0.116" serde = { version = "1.0.200", features = ["derive"] } chrono = { version = "0.4", features = ["serde"] } +thiserror = "1.0.60" [dev-dependencies] mockito = "1.4.0" diff --git a/examples/filter_trains.rs b/examples/filter_trains.rs index 8b2be92..3200f28 100644 --- a/examples/filter_trains.rs +++ b/examples/filter_trains.rs @@ -2,7 +2,7 @@ //! //! This example shows how to filter trains based on the route name and then //! determine what station the train is currently in route to. -use amtrak_api::{responses::TrainStatus, Client}; +use amtrak_api::{Client, TrainStatus}; use chrono::{Local, Utc}; #[tokio::main] diff --git a/examples/single_train.rs b/examples/single_train.rs index 87fbcbe..358a00a 100644 --- a/examples/single_train.rs +++ b/examples/single_train.rs @@ -2,7 +2,7 @@ //! //! This example shows how to query for a single train based on the unique train //! id and then determine if it has stopped at 30th street station yet. -use amtrak_api::{responses::TrainStatus, Client}; +use amtrak_api::{Client, TrainStatus}; const TRAIN_ID: &str = "612-5"; diff --git a/src/client.rs b/src/client.rs index 900540a..032b117 100644 --- a/src/client.rs +++ b/src/client.rs @@ -83,7 +83,7 @@ impl Client { /// # Example /// /// ```rust - /// use amtrak_api::{responses::TrainStatus, Client}; + /// use amtrak_api::{Client, TrainStatus}; /// use chrono::{Local, Utc}; /// /// #[tokio::main] @@ -166,7 +166,7 @@ impl Client { /// # Example /// /// ```rust - /// use amtrak_api::{responses::TrainStatus, Client}; + /// use amtrak_api::{Client, TrainStatus}; /// /// const TRAIN_ID: &str = "612-5"; /// diff --git a/src/errors.rs b/src/errors.rs index e6932c8..e254b6f 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,38 +1,11 @@ -use std::fmt; - -#[derive(Debug)] +#[derive(Debug, thiserror::Error)] pub enum Error { - RequestFailed(reqwest::Error), - DeserializeFailed(serde_json::error::Error), - ApiErrorResponse(String), -} - -impl std::error::Error for Error {} + #[error("Unable to send the request: {0}")] + RequestFailed(#[from] reqwest::Error), -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Self::RequestFailed(e) => { - write!(f, "Unable to send the request: {}", e) - } - Self::DeserializeFailed(e) => { - write!(f, "Unable to deserialize the received value: {}", e) - } - Self::ApiErrorResponse(e) => { - write!(f, "API returned an error response: {}", e) - } - } - } -} - -impl From for Error { - fn from(error: serde_json::error::Error) -> Self { - Self::DeserializeFailed(error) - } -} + #[error("Unable to deserialize the received value: {0}")] + DeserializeFailed(#[from] serde_json::error::Error), -impl From for Error { - fn from(error: reqwest::Error) -> Self { - Self::RequestFailed(error) - } + #[error("API returned an error response: {0}")] + ApiErrorResponse(String), } diff --git a/src/lib.rs b/src/lib.rs index 88bc5a3..458c995 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,8 +37,10 @@ //! } //! ``` -pub mod client; -pub mod errors; -pub mod responses; +mod client; +mod errors; +mod responses; pub use client::Client; +pub use errors::Error; +pub use responses::{Station, Train, TrainState, TrainStatus}; diff --git a/tests/live_endpoint.rs b/tests/live_endpoint.rs index 1453ff5..f6d6462 100644 --- a/tests/live_endpoint.rs +++ b/tests/live_endpoint.rs @@ -1,7 +1,7 @@ use amtrak_api::Client; #[tokio::test] -async fn test_live_train_api() -> Result<(), amtrak_api::errors::Error> { +async fn test_live_train_api() -> Result<(), amtrak_api::Error> { let client = Client::new(); let response = client.trains().await?; @@ -13,7 +13,7 @@ async fn test_live_train_api() -> Result<(), amtrak_api::errors::Error> { } #[tokio::test] -async fn test_live_station_api() -> Result<(), amtrak_api::errors::Error> { +async fn test_live_station_api() -> Result<(), amtrak_api::Error> { let client = Client::new(); let response = client.stations().await?; diff --git a/tests/station.rs b/tests/station.rs index aaa5fed..ba545ba 100644 --- a/tests/station.rs +++ b/tests/station.rs @@ -2,7 +2,7 @@ use amtrak_api::Client; use mockito::Server; #[tokio::test] -async fn test_single_station() -> Result<(), amtrak_api::errors::Error> { +async fn test_single_station() -> Result<(), amtrak_api::Error> { let mut server = Server::new_async().await; let mock_server = server .mock("GET", "/stations") @@ -50,7 +50,7 @@ async fn test_single_station() -> Result<(), amtrak_api::errors::Error> { } #[tokio::test] -async fn test_empty_station() -> Result<(), amtrak_api::errors::Error> { +async fn test_empty_station() -> Result<(), amtrak_api::Error> { let mut server = Server::new_async().await; let mock_server = server .mock("GET", "/stations/ABC") diff --git a/tests/train.rs b/tests/train.rs index 01f21aa..3cf2380 100644 --- a/tests/train.rs +++ b/tests/train.rs @@ -1,9 +1,9 @@ -use amtrak_api::{responses::TrainStatus, Client}; +use amtrak_api::{Client, TrainStatus}; use chrono::{FixedOffset, NaiveDate}; use mockito::Server; #[tokio::test] -async fn test_single_train() -> Result<(), amtrak_api::errors::Error> { +async fn test_single_train() -> Result<(), amtrak_api::Error> { let mut server = Server::new_async().await; let mock_server = server .mock("GET", "/trains") @@ -332,7 +332,7 @@ async fn test_single_train() -> Result<(), amtrak_api::errors::Error> { } #[tokio::test] -async fn test_empty_trains() -> Result<(), amtrak_api::errors::Error> { +async fn test_empty_trains() -> Result<(), amtrak_api::Error> { let mut server = Server::new_async().await; let mock_server = server .mock("GET", "/trains")