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.1";
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 RegisterPostResponse {
43    /// Created.
44    Created
45    ,
46    /// User with that ID does already exist.
47    UserWithThatIDDoesAlreadyExist
48}
49
50#[derive(Debug, PartialEq, Serialize, Deserialize)]
51pub enum StatisticsGetResponse {
52    /// Game result details
53    GameResultDetails
54    (Vec<String>)
55}
56
57#[derive(Debug, PartialEq, Serialize, Deserialize)]
58#[must_use]
59pub enum StatisticsUuidGetResponse {
60    /// Game result details
61    GameResultDetails
62    (models::ResultDto)
63    ,
64    /// This statistic does not exist.
65    ThisStatisticDoesNotExist
66}
67
68#[derive(Debug, PartialEq, Serialize, Deserialize)]
69#[must_use]
70pub enum UserUuidGetResponse {
71    /// Successfully fetched user data
72    SuccessfullyFetchedUserData
73    (models::UserDto)
74    ,
75    /// User with that ID does exist.
76    UserWithThatIDDoesExist
77}
78
79/// API
80#[async_trait]
81#[allow(clippy::too_many_arguments, clippy::ptr_arg)]
82pub trait Api<C: Send + Sync> {
83    fn poll_ready(&self, _cx: &mut Context) -> Poll<Result<(), Box<dyn Error + Send + Sync + 'static>>> {
84        Poll::Ready(Ok(()))
85    }
86
87    /// Calls for Booby Drop Tables
88    async fn bobby_delete(
89        &self,
90        context: &C) -> Result<BobbyDeleteResponse, ApiError>;
91
92    /// Submit game end data
93    async fn game_post(
94        &self,
95        game_end_dto: models::GameEndDto,
96        context: &C) -> Result<GamePostResponse, ApiError>;
97
98    /// Creates a User in our System.
99    async fn register_post(
100        &self,
101        create_user_dto: models::CreateUserDto,
102        context: &C) -> Result<RegisterPostResponse, ApiError>;
103
104    /// Fetch all statistics
105    async fn statistics_get(
106        &self,
107        context: &C) -> Result<StatisticsGetResponse, ApiError>;
108
109    /// Fetch details for a specific run.
110    async fn statistics_uuid_get(
111        &self,
112        uuid: String,
113        context: &C) -> Result<StatisticsUuidGetResponse, ApiError>;
114
115    /// Gets user Details from UUID.
116    async fn user_uuid_get(
117        &self,
118        uuid: String,
119        context: &C) -> Result<UserUuidGetResponse, ApiError>;
120
121}
122
123/// API where `Context` isn't passed on every API call
124#[async_trait]
125#[allow(clippy::too_many_arguments, clippy::ptr_arg)]
126pub trait ApiNoContext<C: Send + Sync> {
127
128    fn poll_ready(&self, _cx: &mut Context) -> Poll<Result<(), Box<dyn Error + Send + Sync + 'static>>>;
129
130    fn context(&self) -> &C;
131
132    /// Calls for Booby Drop Tables
133    async fn bobby_delete(
134        &self,
135        ) -> Result<BobbyDeleteResponse, ApiError>;
136
137    /// Submit game end data
138    async fn game_post(
139        &self,
140        game_end_dto: models::GameEndDto,
141        ) -> Result<GamePostResponse, ApiError>;
142
143    /// Creates a User in our System.
144    async fn register_post(
145        &self,
146        create_user_dto: models::CreateUserDto,
147        ) -> Result<RegisterPostResponse, ApiError>;
148
149    /// Fetch all statistics
150    async fn statistics_get(
151        &self,
152        ) -> Result<StatisticsGetResponse, ApiError>;
153
154    /// Fetch details for a specific run.
155    async fn statistics_uuid_get(
156        &self,
157        uuid: String,
158        ) -> Result<StatisticsUuidGetResponse, ApiError>;
159
160    /// Gets user Details from UUID.
161    async fn user_uuid_get(
162        &self,
163        uuid: String,
164        ) -> Result<UserUuidGetResponse, ApiError>;
165
166}
167
168/// Trait to extend an API to make it easy to bind it to a context.
169pub trait ContextWrapperExt<C: Send + Sync> where Self: Sized
170{
171    /// Binds this API to a context.
172    fn with_context(self, context: C) -> ContextWrapper<Self, C>;
173}
174
175impl<T: Api<C> + Send + Sync, C: Clone + Send + Sync> ContextWrapperExt<C> for T {
176    fn with_context(self: T, context: C) -> ContextWrapper<T, C> {
177         ContextWrapper::<T, C>::new(self, context)
178    }
179}
180
181#[async_trait]
182impl<T: Api<C> + Send + Sync, C: Clone + Send + Sync> ApiNoContext<C> for ContextWrapper<T, C> {
183    fn poll_ready(&self, cx: &mut Context) -> Poll<Result<(), ServiceError>> {
184        self.api().poll_ready(cx)
185    }
186
187    fn context(&self) -> &C {
188        ContextWrapper::context(self)
189    }
190
191    /// Calls for Booby Drop Tables
192    async fn bobby_delete(
193        &self,
194        ) -> Result<BobbyDeleteResponse, ApiError>
195    {
196        let context = self.context().clone();
197        self.api().bobby_delete(&context).await
198    }
199
200    /// Submit game end data
201    async fn game_post(
202        &self,
203        game_end_dto: models::GameEndDto,
204        ) -> Result<GamePostResponse, ApiError>
205    {
206        let context = self.context().clone();
207        self.api().game_post(game_end_dto, &context).await
208    }
209
210    /// Creates a User in our System.
211    async fn register_post(
212        &self,
213        create_user_dto: models::CreateUserDto,
214        ) -> Result<RegisterPostResponse, ApiError>
215    {
216        let context = self.context().clone();
217        self.api().register_post(create_user_dto, &context).await
218    }
219
220    /// Fetch all statistics
221    async fn statistics_get(
222        &self,
223        ) -> Result<StatisticsGetResponse, ApiError>
224    {
225        let context = self.context().clone();
226        self.api().statistics_get(&context).await
227    }
228
229    /// Fetch details for a specific run.
230    async fn statistics_uuid_get(
231        &self,
232        uuid: String,
233        ) -> Result<StatisticsUuidGetResponse, ApiError>
234    {
235        let context = self.context().clone();
236        self.api().statistics_uuid_get(uuid, &context).await
237    }
238
239    /// Gets user Details from UUID.
240    async fn user_uuid_get(
241        &self,
242        uuid: String,
243        ) -> Result<UserUuidGetResponse, ApiError>
244    {
245        let context = self.context().clone();
246        self.api().user_uuid_get(uuid, &context).await
247    }
248
249}
250
251
252#[cfg(feature = "client")]
253pub mod client;
254
255// Re-export Client as a top-level name
256#[cfg(feature = "client")]
257pub use client::Client;
258
259#[cfg(feature = "server")]
260pub mod server;
261
262// Re-export router() as a top-level name
263#[cfg(feature = "server")]
264pub use self::server::Service;
265
266#[cfg(feature = "server")]
267pub mod context;
268
269pub mod models;
270
271#[cfg(any(feature = "client", feature = "server"))]
272pub(crate) mod header;