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
#[cfg(feature = "reqwest_backend")]
mod reqwest_client {
use crate::{BeatSaverApiAsync, BeatSaverApiError};
use async_trait::async_trait;
use bytes::Bytes;
use reqwest::Client;
use std::convert::From;
use url::Url;
#[derive(Debug, Clone)]
pub struct BeatSaverReqwest {
client: Client,
}
impl BeatSaverReqwest {
pub fn new() -> Self {
let client = Client::builder()
.user_agent(concat!(
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION")
))
.build()
.unwrap();
Self { client }
}
}
impl From<reqwest::Error> for BeatSaverApiError<reqwest::Error> {
fn from(e: reqwest::Error) -> Self {
Self::RequestError(e)
}
}
#[async_trait]
impl<'a> BeatSaverApiAsync<'a, reqwest::Error> for BeatSaverReqwest {
async fn request_raw(&'a self, url: Url) -> Result<Bytes, reqwest::Error> {
self.client.get(url).send().await?.bytes().await
}
}
}
#[cfg(feature = "reqwest_backend")]
pub use reqwest_client::BeatSaverReqwest;
#[cfg(all(
feature = "reqwest_backend",
not(feature = "surf_backend"),
not(feature = "ureq_backend")
))]
pub use reqwest_client::BeatSaverReqwest as BeatSaver;
#[cfg(feature = "surf_backend")]
mod surf_client {
use crate::{BeatSaverApiAsync, BeatSaverApiError};
use async_trait::async_trait;
use bytes::Bytes;
use std::convert::From;
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use surf::Client;
use url::Url;
#[derive(Debug)]
pub enum SurfError {
Error(surf::Error),
}
impl Display for SurfError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Error(e) => e.fmt(f),
}
}
}
impl Error for SurfError {}
impl From<surf::Error> for SurfError {
fn from(e: surf::Error) -> Self {
Self::Error(e)
}
}
impl From<SurfError> for BeatSaverApiError<SurfError> {
fn from(e: SurfError) -> Self {
Self::RequestError(e)
}
}
impl From<surf::Error> for BeatSaverApiError<SurfError> {
fn from(e: surf::Error) -> Self {
Self::RequestError(e.into())
}
}
#[derive(Debug, Clone)]
pub struct BeatSaverSurf {
client: Client,
}
impl BeatSaverSurf {
pub fn new() -> Self {
let client = Client::new();
Self { client }
}
}
#[async_trait]
impl<'a> BeatSaverApiAsync<'a, SurfError> for BeatSaverSurf {
async fn request_raw(&'a self, url: Url) -> Result<Bytes, SurfError> {
Ok(self.client.get(url).recv_bytes().await?.into())
}
}
}
#[cfg(feature = "surf_backend")]
pub use surf_client::BeatSaverSurf;
#[cfg(all(
feature = "surf_backend",
not(feature = "reqwest_backend"),
not(feature = "ureq_backend")
))]
pub use surf_client::BeatSaverSurf as BeatSaver;
#[cfg(feature = "ureq_backend")]
mod ureq_client {
use crate::{BeatSaverApiError, BeatSaverApiSync};
use bytes::Bytes;
use std::convert::From;
use std::io::Read;
use ureq;
use url::Url;
impl From<ureq::Error> for BeatSaverApiError<ureq::Error> {
fn from(e: ureq::Error) -> Self {
Self::RequestError(e)
}
}
#[derive(Debug)]
pub struct BeatSaverUreq {}
impl BeatSaverUreq {
pub fn new() -> Self {
Self {}
}
}
impl<'a> BeatSaverApiSync<'a, ureq::Error> for BeatSaverUreq {
fn request_raw(&'a self, url: Url) -> Result<Bytes, ureq::Error> {
let mut contents = vec![];
let resp = ureq::get(url.as_str()).call();
let mut reader = resp.into_reader();
reader.read_to_end(&mut contents)?;
Ok(contents.into())
}
}
}
#[cfg(feature = "ureq_backend")]
pub use ureq_client::BeatSaverUreq;
#[cfg(all(
feature = "ureq_backend",
not(feature = "reqwest_backend"),
not(feature = "surf_backend")
))]
pub use ureq_client::BeatSaverUreq as BeatSaver;
#[cfg(test)]
mod tests {
#[cfg(feature = "surf_backend")]
#[async_std::test]
async fn test_surf_map() {
use crate::client::BeatSaverSurf;
use crate::BeatSaverApiAsync;
use std::convert::TryInto;
let client = BeatSaverSurf::new();
let map = client.map(&"2144".try_into().unwrap()).await.unwrap();
assert_eq!(map.key, "2144");
}
#[cfg(feature = "reqwest_backend")]
#[tokio::test]
async fn test_reqwest_map() {
use crate::client::BeatSaverReqwest;
use crate::BeatSaverApiAsync;
use std::convert::TryInto;
let client = BeatSaverReqwest::new();
let map = client.map(&"2144".try_into().unwrap()).await.unwrap();
assert_eq!(map.key, "2144");
}
#[cfg(feature = "ureq_backend")]
#[test]
fn test_ureq_map() {
use crate::client::BeatSaverUreq;
use crate::BeatSaverApiSync;
use std::convert::TryInto;
let client = BeatSaverUreq::new();
let map = client.map(&"2144".try_into().unwrap()).unwrap();
assert_eq!(map.key, "2144");
}
}