File: lib.rs

package info (click to toggle)
rust-railway-api 0.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 320 kB
  • sloc: makefile: 2
file content (286 lines) | stat: -rw-r--r-- 11,644 bytes parent folder | download | duplicates (2)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#![doc = include_str!("../README.md")]

mod error;
pub use error::*;

use rcore::Requester;

// XXX: Ugly that this needs to be hard-coded; most of the enum is general over the requester, but the Motis one is not.
type R = rcore::ReqwestRequester;

// Note: The derive macro automatically generates the following:
// - `RailwayProviderType`: The enum equivalent to this enum without the data for each enum variant. It furthermore generates `FromStr` and `Display` for it. There is also a `RailwayProviderType::variants` which lists all providers that are available.
// - `RailwayProvider::new`: Construct `RailwayProvider` from `RailwayProviderType` and `R: Requester` using specified `constructor`.
// - `impl<R: Requester> Provider<R> for RailwayProvider<R>`: Using error `BoxedError` and directly passing through the wrapped type.
// It also looks for `#[cfg(...)]` and enables the specific code only if requested.
/// A wrapper around all implemented [`Provider`s](rcore::Provider).
///
/// Note that the variants of this enum depend on the features activated via the API.
#[derive(Clone, rapi_derive::ProviderApi)]
pub enum RailwayProvider {
    // Hafas
    #[cfg(feature = "vbb-provider")]
    #[provider(hafas = rhafas::profile::vbb::VbbProfile {})]
    Vbb(rhafas::client::HafasClient<R>),
    #[cfg(feature = "oebb-provider")]
    #[provider(hafas = rhafas::profile::oebb::OebbProfile {})]
    Oebb(rhafas::client::HafasClient<R>),
    #[cfg(feature = "nahsh-provider")]
    #[provider(hafas = rhafas::profile::nahsh::NahSHProfile {})]
    NahSh(rhafas::client::HafasClient<R>),
    #[cfg(feature = "vvt-provider")]
    #[provider(hafas = rhafas::profile::vvt::VvtProfile {})]
    Vvt(rhafas::client::HafasClient<R>),
    #[cfg(feature = "pkp-provider")]
    #[provider(hafas = rhafas::profile::pkp::PkpProfile {})]
    Pkp(rhafas::client::HafasClient<R>),
    #[cfg(feature = "irish-rail-provider")]
    #[provider(hafas = rhafas::profile::irish_rail::IrishRailProfile {})]
    IrishRail(rhafas::client::HafasClient<R>),
    #[cfg(feature = "mobiliteit-lu-provider")]
    #[provider(hafas = rhafas::profile::mobiliteit_lu::MobiliteitLuProfile {})]
    MobiliteitLu(rhafas::client::HafasClient<R>),
    #[cfg(feature = "dart-provider")]
    #[provider(hafas = rhafas::profile::dart::DartProfile {})]
    Dart(rhafas::client::HafasClient<R>),
    #[cfg(feature = "rmv-provider")]
    #[provider(hafas = rhafas::profile::rmv::RmvProfile {})]
    Rmv(rhafas::client::HafasClient<R>),
    #[cfg(feature = "insa-provider")]
    #[provider(hafas = rhafas::profile::insa::InsaProfile {})]
    Insa(rhafas::client::HafasClient<R>),
    #[cfg(feature = "cmta-provider")]
    #[provider(hafas = rhafas::profile::cmta::CmtaProfile {})]
    Cmta(rhafas::client::HafasClient<R>),
    #[cfg(feature = "sbahn-muenchen-provider")]
    #[provider(hafas = rhafas::profile::sbahn_muenchen::SBahnMuenchenProfile {})]
    SBahnMuenchen(rhafas::client::HafasClient<R>),
    #[cfg(feature = "saarvv-provider")]
    #[provider(hafas = rhafas::profile::saarvv::SaarvvProfile {})]
    Saarvv(rhafas::client::HafasClient<R>),
    #[cfg(feature = "cfl-provider")]
    #[provider(hafas = rhafas::profile::cfl::CflProfile {})]
    Cfl(rhafas::client::HafasClient<R>),
    #[cfg(feature = "nvv-provider")]
    #[provider(hafas = rhafas::profile::nvv::NvvProfile {})]
    Nvv(rhafas::client::HafasClient<R>),
    #[cfg(feature = "mobil-nrw-provider")]
    #[provider(hafas = rhafas::profile::mobil_nrw::MobilNrwProfile {})]
    MobilNrw(rhafas::client::HafasClient<R>),
    #[cfg(feature = "vsn-provider")]
    #[provider(hafas = rhafas::profile::vsn::VsnProfile {})]
    Vsn(rhafas::client::HafasClient<R>),
    #[cfg(feature = "vgi-provider")]
    #[provider(hafas = rhafas::profile::vgi::VgiProfile {})]
    Vgi(rhafas::client::HafasClient<R>),
    #[cfg(feature = "vbn-provider")]
    #[provider(hafas = rhafas::profile::vbn::VbnProfile {})]
    Vbn(rhafas::client::HafasClient<R>),
    #[cfg(feature = "vrn-provider")]
    #[provider(hafas = rhafas::profile::vrn::VrnProfile {})]
    Vrn(rhafas::client::HafasClient<R>),
    #[cfg(feature = "rsag-provider")]
    #[provider(hafas = rhafas::profile::rsag::RsagProfile {})]
    Rsag(rhafas::client::HafasClient<R>),
    #[cfg(feature = "vmt-provider")]
    #[provider(hafas = rhafas::profile::vmt::VmtProfile {})]
    Vmt(rhafas::client::HafasClient<R>),
    #[cfg(feature = "vos-provider")]
    #[provider(hafas = rhafas::profile::vos::VosProfile {})]
    Vos(rhafas::client::HafasClient<R>),
    #[cfg(feature = "avv-provider")]
    #[provider(hafas = rhafas::profile::avv::AvvProfile {})]
    Avv(rhafas::client::HafasClient<R>),
    #[cfg(feature = "rejseplanen-provider")]
    #[provider(hafas = rhafas::profile::rejseplanen::RejseplanenProfile {})]
    Rejseplanen(rhafas::client::HafasClient<R>),
    #[cfg(feature = "ooevv-provider")]
    #[provider(hafas = rhafas::profile::ooevv::OoevvProfile {})]
    Ooevv(rhafas::client::HafasClient<R>),
    #[cfg(feature = "salzburg-provider")]
    #[provider(hafas = rhafas::profile::salzburg::SalzburgProfile {})]
    Salzburg(rhafas::client::HafasClient<R>),
    #[cfg(feature = "verbundlinie-provider")]
    #[provider(hafas = rhafas::profile::verbundlinie::VerbundlinieProfile {})]
    Verbundlinie(rhafas::client::HafasClient<R>),
    #[cfg(feature = "svv-provider")]
    #[provider(hafas = rhafas::profile::svv::SvvProfile {})]
    Svv(rhafas::client::HafasClient<R>),
    #[cfg(feature = "vor-provider")]
    #[provider(hafas = rhafas::profile::vor::VorProfile {})]
    Vor(rhafas::client::HafasClient<R>),
    #[cfg(feature = "vkg-provider")]
    #[provider(hafas = rhafas::profile::vkg::VkgProfile {})]
    Vkg(rhafas::client::HafasClient<R>),
    #[cfg(feature = "vvv-provider")]
    #[provider(hafas = rhafas::profile::vvv::VvvProfile {})]
    Vvv(rhafas::client::HafasClient<R>),
    #[cfg(feature = "bls-provider")]
    #[provider(hafas = rhafas::profile::bls::BlsProfile {})]
    Bls(rhafas::client::HafasClient<R>),
    #[cfg(feature = "kvb-provider")]
    #[provider(hafas = rhafas::profile::kvb::KvbProfile {})]
    Kvb(rhafas::client::HafasClient<R>),
    #[cfg(feature = "bart-provider")]
    #[provider(hafas = rhafas::profile::bart::BartProfile {})]
    Bart(rhafas::client::HafasClient<R>),
    #[cfg(feature = "ivb-provider")]
    #[provider(hafas = rhafas::profile::ivb::IvbProfile {})]
    Ivb(rhafas::client::HafasClient<R>),
    #[cfg(feature = "resrobot-provider")]
    #[provider(hafas = rhafas::profile::resrobot::ResrobotProfile {})]
    Resrobot(rhafas::client::HafasClient<R>),

    // Search.ch
    #[cfg(feature = "search-ch-provider")]
    #[provider(constructor = rsearchch::SearchChClient::new)]
    SearchCh(rsearchch::SearchChClient<R>),

    // Transitous
    #[cfg(feature = "transitous-provider")]
    #[provider(constructor = |r| rmotis::MotisClient::new(url::Url::parse(rmotis::TRANSITOUS_URL).expect("Failed to parse TRANSITOUS_URL"), r))]
    Transitous(rmotis::MotisClient),

    // DB Movas
    #[cfg(feature = "db-provider")]
    #[provider(constructor = rdb::DbMovasClient::new)]
    Db(rdb::DbMovasClient<R>),
}

#[cfg(test)]
mod api_test {
    use super::*;

    #[test]
    #[cfg(feature = "db-provider")]
    fn macro_creates_enum_types() {
        let _ = RailwayProviderType::Db;
    }

    #[test]
    #[cfg(feature = "db-provider")]
    fn macro_creates_enum_type_variants() {
        let list = RailwayProviderType::variants();
        assert!(list.len() >= 1);
    }

    #[test]
    #[cfg(feature = "db-provider")]
    fn macro_creates_enum_from_to_string() {
        use std::str::FromStr;
        assert_eq!(
            RailwayProviderType::from_str("db").unwrap(),
            RailwayProviderType::Db
        );
        assert_eq!(RailwayProviderType::Db.to_string(), "db".to_owned());
    }

/*    #[test]
    #[cfg(feature = "db-provider")]
    fn macro_creates_constructor() {
        let _ = RailwayProvider::new(
            RailwayProviderType::Db,
            rcore::ReqwestRequesterBuilder::default(),
        );
    }

    #[test]
    #[cfg(feature = "db-provider")]
    fn macro_implements_provider() {
        use rcore::Provider;

        let client = RailwayProvider::new(
            RailwayProviderType::Db,
            rcore::ReqwestRequesterBuilder::default(),
        );
        let _ = Box::new(client) as Box<dyn Provider<rcore::ReqwestRequester, Error = BoxedError>>;
    }
*/
    #[test]
    #[ignore = "missing README file"]
    #[cfg(feature = "all-providers")]
    fn readme_supported_up_to_date() {
        use std::collections::HashSet;

        let supported: HashSet<_> = RailwayProviderType::variants()
            .into_iter()
            .map(|p| p.to_string().to_lowercase().replace(&['-', '_'], ""))
            .collect();
        let readme = include_str!("../README.md").lines();

        let entries: HashSet<_> = readme
            // Go to profiles-section.
            .skip_while(|l| l != &"## Profiles")
            // Go to first table.
            .skip_while(|l| !l.starts_with("|"))
            // Skip header.
            .skip(2)
            // Take rows
            .take_while(|l| l.starts_with("|"))
            // Take first column
            .flat_map(|r| r.split('|').nth(1))
            .map(|s| s.trim().to_lowercase().replace(&['-', '_', '.'], ""))
            .collect();

        let extra: HashSet<_> = entries.difference(&supported).collect();
        let missing: HashSet<_> = supported.difference(&entries).collect();

        if !(extra.is_empty() && missing.is_empty()) {
            println!("Extra Providers: {:#?}", extra);
            println!("Missing Providers: {:#?}", missing);
            panic!("Mismatched README and available providers");
        }
    }

   /* #[test]
    #[cfg(feature = "all-providers")]
    fn hafas_all_providers_up_to_date() {
        use serde::Deserialize;
        use std::collections::{HashMap, HashSet};
        use toml;

        #[derive(Deserialize)]
        struct CrateCargo {
            features: HashMap<String, Vec<String>>,
        }

        let hafas_cargo = include_str!("../../railway-provider-hafas/Cargo.toml");
        let cargo: CrateCargo = toml::from_str(hafas_cargo).unwrap();

        let hafas_supported_profiles: HashSet<_> = cargo
            .features
            .keys()
            .filter_map(|s| s.strip_suffix("-profile"))
            .map(|p| p.to_string().to_lowercase().replace(['-', '_'], ""))
            .collect();
        let hafas_all_profiles: HashSet<_> = cargo.features["all-profiles"]
            .iter()
            .map(|p| {
                p.strip_suffix("-profile")
                    .unwrap_or_default()
                    .to_string()
                    .to_lowercase()
                    .replace(['-', '_'], "")
            })
            .collect();

        let supported: HashSet<_> = RailwayProviderType::variants()
            .iter()
            .map(|p| p.to_string().to_lowercase().replace(['-', '_'], ""))
            .collect();

        let supported_hafas = supported
            .intersection(&hafas_supported_profiles)
            .map(|s| s.to_string())
            .collect();

        let extra: HashSet<_> = hafas_all_profiles.difference(&supported_hafas).collect();
        let missing: HashSet<_> = supported_hafas.difference(&hafas_all_profiles).collect();

        if !(extra.is_empty() && missing.is_empty()) {
            println!("Extra Providers: {:#?}", extra);
            println!("Missing Providers: {:#?}", missing);
            panic!("Mismatched README and available providers");
        }
    }*/
}