r1_api_layer/
lib.rs

1#![allow(missing_docs, trivial_casts, unused_variables, unused_mut, unused_imports, unused_extern_crates, unused_attributes, non_camel_case_types)]
2#![allow(clippy::derive_partial_eq_without_eq, clippy::disallowed_names)]
3
4use async_trait::async_trait;
5use futures::Stream;
6use std::error::Error;
7use std::collections::BTreeSet;
8use std::task::{Poll, Context};
9use swagger::{ApiError, ContextWrapper};
10use serde::{Serialize, Deserialize};
11use crate::server::Authorization;
12
13
14type ServiceError = Box<dyn Error + Send + Sync + 'static>;
15
16pub const BASE_PATH: &str = "";
17pub const API_VERSION: &str = "1.0.2";
18
19mod auth;
20pub use auth::{AuthenticationApi, Claims};
21
22
23#[derive(Debug, PartialEq, Serialize, Deserialize)]
24#[must_use]
25pub enum BobbyDeleteResponse {
26    /// Dropped all data.
27    DroppedAllData
28    ,
29    /// Wrong Passphrase Provided.
30    WrongPassphraseProvided
31}
32
33#[derive(Debug, PartialEq, Serialize, Deserialize)]
34pub enum GamePostResponse {
35    /// Game result details
36    GameResultDetails
37    (models::ResultDto)
38}
39
40#[derive(Debug, PartialEq, Serialize, Deserialize)]
41#[must_use]
42pub enum MapsGetResponse {
43    /// List of the ids of all existing maps.
44    ListOfTheIdsOfAllExistingMaps
45    (Vec<String>)
46    ,
47    /// No Maps in Database.
48    NoMapsInDatabase
49}
50
51#[derive(Debug, PartialEq, Serialize, Deserialize)]
52#[must_use]
53pub enum RegisterPostResponse {
54    /// Created.
55    Created
56    ,
57    /// User with that ID does already exist.
58    UserWithThatIDDoesAlreadyExist
59}
60
61#[derive(Debug, PartialEq, Serialize, Deserialize)]
62pub enum StatisticsGetResponse {
63    /// Game result details
64    GameResultDetails
65    (Vec<String>)
66}
67
68#[derive(Debug, PartialEq, Serialize, Deserialize)]
69#[must_use]
70pub enum MapLevelGetResponse {
71    /// BeatMap
72    BeatMap
73    (Vec<models::MapDto>)
74    ,
75    /// This map does not exist.
76    ThisMapDoesNotExist
77}
78
79#[derive(Debug, PartialEq, Serialize, Deserialize)]
80#[must_use]
81pub enum StatisticsUuidGetResponse {
82    /// Game result details
83    GameResultDetails
84    (models::ResultDto)
85    ,
86    /// This statistic does not exist.
87    ThisStatisticDoesNotExist
88}
89
90#[derive(Debug, PartialEq, Serialize, Deserialize)]
91#[must_use]
92pub enum UserUuidGetResponse {
93    /// Successfully fetched user data
94    SuccessfullyFetchedUserData
95    (models::UserDto)
96    ,
97    /// User with that ID does exist.
98    UserWithThatIDDoesExist
99}
100
101/// API
102#[async_trait]
103#[allow(clippy::too_many_arguments, clippy::ptr_arg)]
104pub trait Api<C: Send + Sync> {
105    fn poll_ready(&self, _cx: &mut Context) -> Poll<Result<(), Box<dyn Error + Send + Sync + 'static>>> {
106        Poll::Ready(Ok(()))
107    }
108
109    /// Calls for Booby Drop Tables
110    async fn bobby_delete(
111        &self,
112        context: &C) -> Result<BobbyDeleteResponse, ApiError>;
113
114    /// Submit game end data
115    async fn game_post(
116        &self,
117        game_end_dto: models::GameEndDto,
118        context: &C) -> Result<GamePostResponse, ApiError>;
119
120    /// Fetch all existing maps.
121    async fn maps_get(
122        &self,
123        context: &C) -> Result<MapsGetResponse, ApiError>;
124
125    /// Creates a User in our System.
126    async fn register_post(
127        &self,
128        create_user_dto: models::CreateUserDto,
129        context: &C) -> Result<RegisterPostResponse, ApiError>;
130
131    /// Fetch all statistics
132    async fn statistics_get(
133        &self,
134        context: &C) -> Result<StatisticsGetResponse, ApiError>;
135
136    /// Fetch BeatMap of a specific level.
137    async fn map_level_get(
138        &self,
139        level: String,
140        context: &C) -> Result<MapLevelGetResponse, ApiError>;
141
142    /// Fetch details for a specific run.
143    async fn statistics_uuid_get(
144        &self,
145        uuid: String,
146        context: &C) -> Result<StatisticsUuidGetResponse, ApiError>;
147
148    /// Gets user Details from UUID.
149    async fn user_uuid_get(
150        &self,
151        uuid: String,
152        context: &C) -> Result<UserUuidGetResponse, ApiError>;
153
154}
155
156/// API where `Context` isn't passed on every API call
157#[async_trait]
158#[allow(clippy::too_many_arguments, clippy::ptr_arg)]
159pub trait ApiNoContext<C: Send + Sync> {
160
161    fn poll_ready(&self, _cx: &mut Context) -> Poll<Result<(), Box<dyn Error + Send + Sync + 'static>>>;
162
163    fn context(&self) -> &C;
164
165    /// Calls for Booby Drop Tables
166    async fn bobby_delete(
167        &self,
168        ) -> Result<BobbyDeleteResponse, ApiError>;
169
170    /// Submit game end data
171    async fn game_post(
172        &self,
173        game_end_dto: models::GameEndDto,
174        ) -> Result<GamePostResponse, ApiError>;
175
176    /// Fetch all existing maps.
177    async fn maps_get(
178        &self,
179        ) -> Result<MapsGetResponse, ApiError>;
180
181    /// Creates a User in our System.
182    async fn register_post(
183        &self,
184        create_user_dto: models::CreateUserDto,
185        ) -> Result<RegisterPostResponse, ApiError>;
186
187    /// Fetch all statistics
188    async fn statistics_get(
189        &self,
190        ) -> Result<StatisticsGetResponse, ApiError>;
191
192    /// Fetch BeatMap of a specific level.
193    async fn map_level_get(
194        &self,
195        level: String,
196        ) -> Result<MapLevelGetResponse, ApiError>;
197
198    /// Fetch details for a specific run.
199    async fn statistics_uuid_get(
200        &self,
201        uuid: String,
202        ) -> Result<StatisticsUuidGetResponse, ApiError>;
203
204    /// Gets user Details from UUID.
205    async fn user_uuid_get(
206        &self,
207        uuid: String,
208        ) -> Result<UserUuidGetResponse, ApiError>;
209
210}
211
212/// Trait to extend an API to make it easy to bind it to a context.
213pub trait ContextWrapperExt<C: Send + Sync> where Self: Sized
214{
215    /// Binds this API to a context.
216    fn with_context(self, context: C) -> ContextWrapper<Self, C>;
217}
218
219impl<T: Api<C> + Send + Sync, C: Clone + Send + Sync> ContextWrapperExt<C> for T {
220    fn with_context(self: T, context: C) -> ContextWrapper<T, C> {
221         ContextWrapper::<T, C>::new(self, context)
222    }
223}
224
225#[async_trait]
226impl<T: Api<C> + Send + Sync, C: Clone + Send + Sync> ApiNoContext<C> for ContextWrapper<T, C> {
227    fn poll_ready(&self, cx: &mut Context) -> Poll<Result<(), ServiceError>> {
228        self.api().poll_ready(cx)
229    }
230
231    fn context(&self) -> &C {
232        ContextWrapper::context(self)
233    }
234
235    /// Calls for Booby Drop Tables
236    async fn bobby_delete(
237        &self,
238        ) -> Result<BobbyDeleteResponse, ApiError>
239    {
240        let context = self.context().clone();
241        self.api().bobby_delete(&context).await
242    }
243
244    /// Submit game end data
245    async fn game_post(
246        &self,
247        game_end_dto: models::GameEndDto,
248        ) -> Result<GamePostResponse, ApiError>
249    {
250        let context = self.context().clone();
251        self.api().game_post(game_end_dto, &context).await
252    }
253
254    /// Fetch all existing maps.
255    async fn maps_get(
256        &self,
257        ) -> Result<MapsGetResponse, ApiError>
258    {
259        let context = self.context().clone();
260        self.api().maps_get(&context).await
261    }
262
263    /// Creates a User in our System.
264    async fn register_post(
265        &self,
266        create_user_dto: models::CreateUserDto,
267        ) -> Result<RegisterPostResponse, ApiError>
268    {
269        let context = self.context().clone();
270        self.api().register_post(create_user_dto, &context).await
271    }
272
273    /// Fetch all statistics
274    async fn statistics_get(
275        &self,
276        ) -> Result<StatisticsGetResponse, ApiError>
277    {
278        let context = self.context().clone();
279        self.api().statistics_get(&context).await
280    }
281
282    /// Fetch BeatMap of a specific level.
283    async fn map_level_get(
284        &self,
285        level: String,
286        ) -> Result<MapLevelGetResponse, ApiError>
287    {
288        let context = self.context().clone();
289        self.api().map_level_get(level, &context).await
290    }
291
292    /// Fetch details for a specific run.
293    async fn statistics_uuid_get(
294        &self,
295        uuid: String,
296        ) -> Result<StatisticsUuidGetResponse, ApiError>
297    {
298        let context = self.context().clone();
299        self.api().statistics_uuid_get(uuid, &context).await
300    }
301
302    /// Gets user Details from UUID.
303    async fn user_uuid_get(
304        &self,
305        uuid: String,
306        ) -> Result<UserUuidGetResponse, ApiError>
307    {
308        let context = self.context().clone();
309        self.api().user_uuid_get(uuid, &context).await
310    }
311
312}
313
314
315#[cfg(feature = "client")]
316pub mod client;
317
318// Re-export Client as a top-level name
319#[cfg(feature = "client")]
320pub use client::Client;
321
322#[cfg(feature = "server")]
323pub mod server;
324
325// Re-export router() as a top-level name
326#[cfg(feature = "server")]
327pub use self::server::Service;
328
329#[cfg(feature = "server")]
330pub mod context;
331
332pub mod models;
333
334#[cfg(any(feature = "client", feature = "server"))]
335pub(crate) mod header;