#![allow(async_fn_in_trait)]
#![doc = include_str!("../LIBDOC.md")]
#[cfg(feature = "non-blocking-traits")]
pub use crate::r#async::prelude::*;
#[cfg(feature = "non-blocking-traits")]
pub use crate::r#async::LTAClient;
use http::status::StatusCode;
pub use lta_models as models;
use thiserror::Error;
pub mod prelude {
pub use crate::{Bus, Crowd, Facility, Geo, Taxi, Traffic, Train};
}
use crate::models::crowd::passenger_vol::VolType;
use concat_string::concat_string;
#[cfg(any(feature = "reqwest-async", feature = "reqwest-blocking"))]
pub use reqwest;
#[cfg(feature = "ureq-blocking")]
pub use ureq;
#[cfg(feature = "non-blocking-traits")]
pub mod r#async;
#[cfg(feature = "blocking-traits")]
pub mod blocking;
#[cfg(feature = "reqwest-blocking")]
pub mod reqwest_blocking;
#[cfg(feature = "ureq-blocking")]
pub mod ureq_blocking;
#[cfg(feature = "reqwest-async")]
pub mod reqwest_async;
pub type LTAResult<T> = Result<T, LTAError>;
#[derive(Error, Debug)]
pub enum LTAError {
#[error("Internal error within the client backend, open a PR if this happens!")]
BackendError(Box<dyn std::error::Error + Send + Sync>),
#[error("Invalid API Key!")]
InvalidAPIKey,
#[error("Server rate limit reached!")]
RateLimitReached,
#[error("Unknown enum variant!")]
UnknownEnumVariant,
#[error("HTTP Header Unauthorized")]
Unauthorized,
#[error("HTTP Header NotFound")]
NotFound,
#[error("HTTP Internal Server Error")]
InternalServerError,
#[error("Failed to parse body of response, probably malformed")]
FailedToParseBody,
#[error("Undocumented status code, open an issue if this happens")]
UnhandledStatusCode(StatusCode, String),
#[error("Custom error: `{0}`")]
Custom(String),
}
pub trait Client: Sized {
type InternalClient;
type RB;
fn new(
api_key: impl Into<String>,
client: Self::InternalClient,
base_url: impl Into<String>,
) -> Self;
fn with_api_key(api_key: impl Into<String>, base_url: impl Into<String>) -> LTAResult<Self>;
fn req_builder(&self, url: &str) -> Self::RB;
fn base_url(&self) -> &str;
}
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Bus;
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Crowd;
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Taxi;
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Traffic;
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Train;
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Geo;
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Facility;
pub(crate) fn vol_type_to_url(base_url: &str, vol_type: VolType) -> LTAResult<String> {
let url = match vol_type {
VolType::BusStops => "/PV/Bus",
VolType::OdBusStop => "/PV/ODBus",
VolType::Train => "/PV/Train",
VolType::OdTrain => "/PV/ODTrain",
_ => return Err(LTAError::UnknownEnumVariant),
};
Ok(concat_string!(base_url, url))
}