1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#![doc = include_str!("../LIBDOC.md")]
#[macro_export]
macro_rules! api_url {
($e: expr) => {
concat!("http://datamall2.mytransport.sg/ltaodataservice", $e)
};
}
pub use crate::r#async::prelude::*;
pub use crate::r#async::LTAClient;
pub use lta_models as models;
use reqwest::StatusCode;
use thiserror::Error;
pub mod prelude {
pub use crate::{Bus, Crowd, Facility, Geo, Taxi, Traffic, Train};
}
use crate::models::crowd::passenger_vol::VolType;
pub use reqwest;
pub mod r#async;
#[cfg(feature = "blocking")]
pub mod blocking;
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(#[from] reqwest::Error),
#[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("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) -> Self;
fn with_api_key(api_key: impl Into<String>) -> LTAResult<Self>;
fn req_builder(&self, url: &str) -> Self::RB;
}
#[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) const fn vol_type_to_url(vol_type: VolType) -> LTAResult<&'static str> {
use crate::models::crowd::passenger_vol;
let url = match vol_type {
VolType::BusStops => passenger_vol::URL_BY_BUS_STOPS,
VolType::OdBusStop => passenger_vol::URL_BY_OD_BUS_STOPS,
VolType::Train => passenger_vol::URL_BY_TRAIN,
VolType::OdTrain => passenger_vol::URL_BY_OD_TRAIN,
_ => return Err(LTAError::UnknownEnumVariant),
};
Ok(url)
}