Skip to content

Commit

Permalink
Fix response types for Departures
Browse files Browse the repository at this point in the history
  • Loading branch information
tascord committed Apr 8, 2024
1 parent 7c21560 commit dbf0a82
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ jobs:
env:
DEVID: ${{ secrets.DEVID }}
KEY: ${{ secrets.KEY }}
QUIET: true
run: cargo test --verbose
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ impl Client {
let hash = hex::encode(hasher.finalize().into_bytes());
let url = format!("{API_URL}{}&signature={}", path, hash);

if std::env::var("DEBUG").is_ok() {
println!("Requesting: {}", url);
}

let res = reqwest::get(&url).await?;
if !res.status().is_success() {
return Err(anyhow::anyhow!("Request failed: {}", res.status()));
Expand Down
14 changes: 6 additions & 8 deletions src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
opt_de_rfc3339,
};

pub struct I32ButSilly(i32);
pub struct I32ButSilly(pub i32);
impl<'de> Deserialize<'de> for I32ButSilly {
fn deserialize<D>(deserializer: D) -> Result<I32ButSilly, D::Error>
where
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Display for RouteType {
pub enum DisruptionModes {}

impl From<i8> for DisruptionModes {
fn from(value: i8) -> Self {
fn from(_value: i8) -> Self {
todo!();
}
}
Expand Down Expand Up @@ -246,7 +246,7 @@ pub struct DeparturesResponse {
/// Timetabled and real-time service departures
pub departures: Vec<Departure>,
/// A train station, tram stop, bus stop, regional coach stop or Night Bus stop
pub stops: Vec<DepartureStop>,
pub stops: HashMap<String, DepartureStop>,
/// Train lines, tram routes, bus routes, regional coach routes, Night Bus routes
pub routes: HashMap<String, DepartureRoute>,
/// Individual trips/services of a route
Expand Down Expand Up @@ -297,7 +297,7 @@ pub struct Departure {
#[derive(Deserialize, Debug)]
pub struct DepartureStop {
#[serde(rename = "stop_distance")]
pub distance: i32, // TODO: Check if this should be a float
pub distance: f32,
#[serde(rename = "stop_suburb")]
pub suburb: String,
#[serde(rename = "stop_name")]
Expand Down Expand Up @@ -471,11 +471,9 @@ pub struct Disruption {
/// Description of the disruption
pub description: String,
/// Status of the disruption (e.g. "Planned", "Current")
#[serde(deserialize_with = "de_rfc3339")]
pub disruption_status: NaiveDateTime,
pub disruption_status: DisruptionStatus, // TODO: This might want to be a String
/// Type of disruption
#[serde(deserialize_with = "de_rfc3339")]
pub disruption_type: NaiveDateTime,
pub disruption_type: String,
/// Date and time disruption information is published on PTV website
#[serde(deserialize_with = "de_rfc3339")]
pub published_on: NaiveDateTime,
Expand Down
28 changes: 18 additions & 10 deletions tests/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#[allow(dead_code)]
#[cfg(test)]
pub mod test {
use std::{
collections::HashMap, future::Future, pin::Pin, sync::Arc,
};
use std::{collections::HashMap, future::Future, pin::Pin, sync::Arc};

use colored::Colorize;
use dotenv::dotenv;
use once_cell::sync::Lazy;
use ptv::*;

static CLIENT: Lazy<Client> = Lazy::new(|| {

// Load .env file if DEVID and KEY are not set
if std::env::var("DEVID").is_err() || std::env::var("KEY").is_err() {
dotenv().ok();
Expand All @@ -23,9 +21,9 @@ pub mod test {
});

// TODO: Find sensible constants
static ROUTE_TYPE: RouteType = RouteType::Train;
static ROUTE_ID: i32 = 1;
static STOP_ID: i32 = 1;
static ROUTE_TYPE: RouteType = RouteType::Train; // Train
static ROUTE_ID: i32 = 1; // Alamein (Line)
static STOP_ID: i32 = 1002; // Alamein (Station)

type Task =
Arc<dyn Fn() -> Pin<Box<dyn Future<Output = anyhow::Result<String>>>> + Send + Sync>;
Expand All @@ -38,7 +36,11 @@ pub mod test {
Arc::new(|| {
Box::pin(async {
let res = CLIENT
.departures_stop(ROUTE_TYPE, STOP_ID, DeparturesStopOpts::default())
.departures_stop(
ROUTE_TYPE,
STOP_ID,
DeparturesStopOpts::default(),
)
.await?;

Ok(format!("{:?}", res))
Expand Down Expand Up @@ -79,12 +81,18 @@ pub mod test {
let elapsed = start.elapsed();
match res {
Ok(res) => println!(
"[{}] {} {} in {:?}:\n{}",
"[{}] {} {} in {:?}:{}",
"+".green(),
name.yellow(),
"passed".green(),
elapsed,
res.cyan()
{
if std::env::var("quiet").is_ok() {
format!("\n{}", res.cyan())
} else {
" ...".cyan().to_string()
}
}
),
Err(e) => {
failed += 1;
Expand Down

0 comments on commit dbf0a82

Please sign in to comment.