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
#![allow(missing_docs, trivial_casts, unused_variables, unused_mut, unused_imports, unused_extern_crates, non_camel_case_types)]

use async_trait::async_trait;
use futures::Stream;
use std::error::Error;
use std::task::{Poll, Context};
use swagger::{ApiError, ContextWrapper};
use serde::{Serialize, Deserialize};

type ServiceError = Box<dyn Error + Send + Sync + 'static>;

pub const BASE_PATH: &'static str = "";
pub const API_VERSION: &'static str = "0.1.2";

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[must_use]
pub enum ActivateUserResponse {
    /// successful operation
    SuccessfulOperation
    (models::User)
    ,
    /// Invalid input
    InvalidInput
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[must_use]
pub enum DeactivateUserResponse {
    /// successful operation
    SuccessfulOperation
    (models::User)
    ,
    /// Invalid input
    InvalidInput
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[must_use]
pub enum UpdatePasswordResponse {
    /// successful operation
    SuccessfulOperation
    (models::User)
    ,
    /// Invalid input
    InvalidInput
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[must_use]
pub enum QueryUserByIdResponse {
    /// successful operation
    SuccessfulOperation
    (models::User)
    ,
    /// Invalid ID supplied
    InvalidIDSupplied
    ,
    /// User not found
    UserNotFound
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[must_use]
pub enum QueryUsersResponse {
    /// successful operation
    SuccessfulOperation
    (models::User)
    ,
    /// Invalid ID supplied
    InvalidIDSupplied
    ,
    /// User not found
    UserNotFound
}

/// API
#[async_trait]
pub trait Api<C: Send + Sync> {
    fn poll_ready(&self, _cx: &mut Context) -> Poll<Result<(), Box<dyn Error + Send + Sync + 'static>>> {
        Poll::Ready(Ok(()))
    }

    /// Activate a user
    async fn activate_user(
        &self,
        context: &C) -> Result<ActivateUserResponse, ApiError>;

    /// Deactive user
    async fn deactivate_user(
        &self,
        context: &C) -> Result<DeactivateUserResponse, ApiError>;

    /// update password
    async fn update_password(
        &self,
        inline_object: models::InlineObject,
        context: &C) -> Result<UpdatePasswordResponse, ApiError>;

    /// Get user infomation by id
    async fn query_user_by_id(
        &self,
        id: uuid::Uuid,
        context: &C) -> Result<QueryUserByIdResponse, ApiError>;

    /// Get users infomation
    async fn query_users(
        &self,
        username: Option<String>,
        phone: Option<String>,
        context: &C) -> Result<QueryUsersResponse, ApiError>;

}

/// API where `Context` isn't passed on every API call
#[async_trait]
pub trait ApiNoContext<C: Send + Sync> {

    fn poll_ready(&self, _cx: &mut Context) -> Poll<Result<(), Box<dyn Error + Send + Sync + 'static>>>;

    fn context(&self) -> &C;

    /// Activate a user
    async fn activate_user(
        &self,
        ) -> Result<ActivateUserResponse, ApiError>;

    /// Deactive user
    async fn deactivate_user(
        &self,
        ) -> Result<DeactivateUserResponse, ApiError>;

    /// update password
    async fn update_password(
        &self,
        inline_object: models::InlineObject,
        ) -> Result<UpdatePasswordResponse, ApiError>;

    /// Get user infomation by id
    async fn query_user_by_id(
        &self,
        id: uuid::Uuid,
        ) -> Result<QueryUserByIdResponse, ApiError>;

    /// Get users infomation
    async fn query_users(
        &self,
        username: Option<String>,
        phone: Option<String>,
        ) -> Result<QueryUsersResponse, ApiError>;

}

/// Trait to extend an API to make it easy to bind it to a context.
pub trait ContextWrapperExt<C: Send + Sync> where Self: Sized
{
    /// Binds this API to a context.
    fn with_context(self: Self, context: C) -> ContextWrapper<Self, C>;
}

impl<T: Api<C> + Send + Sync, C: Clone + Send + Sync> ContextWrapperExt<C> for T {
    fn with_context(self: T, context: C) -> ContextWrapper<T, C> {
         ContextWrapper::<T, C>::new(self, context)
    }
}

#[async_trait]
impl<T: Api<C> + Send + Sync, C: Clone + Send + Sync> ApiNoContext<C> for ContextWrapper<T, C> {
    fn poll_ready(&self, cx: &mut Context) -> Poll<Result<(), ServiceError>> {
        self.api().poll_ready(cx)
    }

    fn context(&self) -> &C {
        ContextWrapper::context(self)
    }

    /// Activate a user
    async fn activate_user(
        &self,
        ) -> Result<ActivateUserResponse, ApiError>
    {
        let context = self.context().clone();
        self.api().activate_user(&context).await
    }

    /// Deactive user
    async fn deactivate_user(
        &self,
        ) -> Result<DeactivateUserResponse, ApiError>
    {
        let context = self.context().clone();
        self.api().deactivate_user(&context).await
    }

    /// update password
    async fn update_password(
        &self,
        inline_object: models::InlineObject,
        ) -> Result<UpdatePasswordResponse, ApiError>
    {
        let context = self.context().clone();
        self.api().update_password(inline_object, &context).await
    }

    /// Get user infomation by id
    async fn query_user_by_id(
        &self,
        id: uuid::Uuid,
        ) -> Result<QueryUserByIdResponse, ApiError>
    {
        let context = self.context().clone();
        self.api().query_user_by_id(id, &context).await
    }

    /// Get users infomation
    async fn query_users(
        &self,
        username: Option<String>,
        phone: Option<String>,
        ) -> Result<QueryUsersResponse, ApiError>
    {
        let context = self.context().clone();
        self.api().query_users(username, phone, &context).await
    }

}


#[cfg(feature = "client")]
pub mod client;

// Re-export Client as a top-level name
#[cfg(feature = "client")]
pub use client::Client;

#[cfg(feature = "server")]
pub mod server;

// Re-export router() as a top-level name
#[cfg(feature = "server")]
pub use self::server::Service;

#[cfg(feature = "server")]
pub mod context;

pub mod models;

#[cfg(any(feature = "client", feature = "server"))]
pub(crate) mod header;