Skip to content

Commit

Permalink
Reorganize modules
Browse files Browse the repository at this point in the history
A flat heirarchy makes more sense.
  • Loading branch information
StefanBossbaly committed May 10, 2024
1 parent c8bcc12 commit d543cd4
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 48 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion examples/filter_trains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion examples/single_train.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
4 changes: 2 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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";
///
Expand Down
41 changes: 7 additions & 34 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -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<serde_json::error::Error> 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<reqwest::Error> for Error {
fn from(error: reqwest::Error) -> Self {
Self::RequestFailed(error)
}
#[error("API returned an error response: {0}")]
ApiErrorResponse(String),
}
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
4 changes: 2 additions & 2 deletions tests/live_endpoint.rs
Original file line number Diff line number Diff line change
@@ -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?;

Expand All @@ -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?;

Expand Down
4 changes: 2 additions & 2 deletions tests/station.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions tests/train.rs
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit d543cd4

Please sign in to comment.