2021-05-09 14:17:24 -06:00
|
|
|
use std::{borrow::Cow, convert::Infallible};
|
2021-05-07 14:06:56 -06:00
|
|
|
|
2021-04-13 12:14:07 -06:00
|
|
|
use chrono::Utc;
|
2021-05-09 14:17:24 -06:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-05-07 14:06:56 -06:00
|
|
|
use warp::{reply::Response, Reply};
|
2021-04-13 12:14:07 -06:00
|
|
|
|
2021-04-20 10:07:11 -06:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-05-09 14:17:24 -06:00
|
|
|
pub struct UserResponse<'a> {
|
|
|
|
pub username: Cow<'a, str>,
|
2021-04-20 10:07:11 -06:00
|
|
|
}
|
2021-04-13 12:14:07 -06:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-05-09 14:17:24 -06:00
|
|
|
pub struct RegisterRequest<'a> {
|
|
|
|
pub email: Cow<'a, str>,
|
|
|
|
pub username: Cow<'a, str>,
|
|
|
|
pub password: Cow<'a, str>,
|
2021-04-13 12:14:07 -06:00
|
|
|
}
|
|
|
|
|
2021-04-20 10:07:11 -06:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-05-09 14:17:24 -06:00
|
|
|
pub struct RegisterResponse<'a> {
|
|
|
|
pub session: Cow<'a, str>,
|
2021-04-20 10:07:11 -06:00
|
|
|
}
|
|
|
|
|
2021-04-13 12:14:07 -06:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-05-09 14:17:24 -06:00
|
|
|
pub struct LoginRequest<'a> {
|
|
|
|
pub username: Cow<'a, str>,
|
|
|
|
pub password: Cow<'a, str>,
|
2021-04-13 12:14:07 -06:00
|
|
|
}
|
|
|
|
|
2021-04-20 10:07:11 -06:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-05-09 14:17:24 -06:00
|
|
|
pub struct LoginResponse<'a> {
|
|
|
|
pub session: Cow<'a, str>,
|
2021-04-20 10:07:11 -06:00
|
|
|
}
|
|
|
|
|
2021-04-13 12:14:07 -06:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-05-09 14:17:24 -06:00
|
|
|
pub struct AddHistoryRequest<'a, D> {
|
|
|
|
pub id: Cow<'a, str>,
|
2021-04-13 12:14:07 -06:00
|
|
|
pub timestamp: chrono::DateTime<Utc>,
|
2021-05-09 14:17:24 -06:00
|
|
|
pub data: D,
|
|
|
|
pub hostname: Cow<'a, str>,
|
2021-04-13 12:14:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct CountResponse {
|
|
|
|
pub count: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-05-09 14:17:24 -06:00
|
|
|
pub struct SyncHistoryRequest<'a> {
|
2021-04-20 10:07:11 -06:00
|
|
|
pub sync_ts: chrono::DateTime<chrono::FixedOffset>,
|
|
|
|
pub history_ts: chrono::DateTime<chrono::FixedOffset>,
|
2021-05-09 14:17:24 -06:00
|
|
|
pub host: Cow<'a, str>,
|
2021-04-20 10:07:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct SyncHistoryResponse {
|
2021-04-13 12:14:07 -06:00
|
|
|
pub history: Vec<String>,
|
|
|
|
}
|
2021-04-20 10:07:11 -06:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-05-09 14:17:24 -06:00
|
|
|
pub struct ErrorResponse<'a> {
|
|
|
|
pub reason: Cow<'a, str>,
|
2021-04-20 10:07:11 -06:00
|
|
|
}
|
|
|
|
|
2021-05-09 14:17:24 -06:00
|
|
|
impl Reply for ErrorResponse<'_> {
|
2021-05-07 14:06:56 -06:00
|
|
|
fn into_response(self) -> Response {
|
|
|
|
warp::reply::json(&self).into_response()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 14:17:24 -06:00
|
|
|
pub struct ErrorResponseStatus<'a> {
|
|
|
|
pub error: ErrorResponse<'a>,
|
2021-05-07 14:06:56 -06:00
|
|
|
pub status: warp::http::StatusCode,
|
|
|
|
}
|
|
|
|
|
2021-05-09 14:17:24 -06:00
|
|
|
impl Reply for ErrorResponseStatus<'_> {
|
2021-05-07 14:06:56 -06:00
|
|
|
fn into_response(self) -> Response {
|
|
|
|
warp::reply::with_status(self.error, self.status).into_response()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 14:17:24 -06:00
|
|
|
impl<'a> ErrorResponse<'a> {
|
|
|
|
pub fn with_status(self, status: warp::http::StatusCode) -> ErrorResponseStatus<'a> {
|
2021-05-07 14:06:56 -06:00
|
|
|
ErrorResponseStatus {
|
|
|
|
error: self,
|
2021-04-20 10:07:11 -06:00
|
|
|
status,
|
2021-05-07 14:06:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 14:17:24 -06:00
|
|
|
pub fn reply(reason: &'a str) -> ErrorResponse {
|
2021-05-07 14:06:56 -06:00
|
|
|
Self {
|
2021-05-09 14:17:24 -06:00
|
|
|
reason: reason.into(),
|
2021-05-07 14:06:56 -06:00
|
|
|
}
|
2021-04-20 10:07:11 -06:00
|
|
|
}
|
|
|
|
}
|
2021-05-07 14:06:56 -06:00
|
|
|
|
|
|
|
pub enum ReplyEither<T, E> {
|
|
|
|
Ok(T),
|
|
|
|
Err(E),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Reply, E: Reply> Reply for ReplyEither<T, E> {
|
|
|
|
fn into_response(self) -> Response {
|
|
|
|
match self {
|
|
|
|
ReplyEither::Ok(t) => t.into_response(),
|
|
|
|
ReplyEither::Err(e) => e.into_response(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type ReplyResult<T, E> = Result<ReplyEither<T, E>, Infallible>;
|
|
|
|
pub fn reply_error<T, E>(e: E) -> ReplyResult<T, E> {
|
|
|
|
Ok(ReplyEither::Err(e))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type JSONResult<E> = Result<ReplyEither<warp::reply::Json, E>, Infallible>;
|
|
|
|
pub fn reply_json<E>(t: impl Serialize) -> JSONResult<E> {
|
|
|
|
reply(warp::reply::json(&t))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reply<T, E>(t: T) -> ReplyResult<T, E> {
|
|
|
|
Ok(ReplyEither::Ok(t))
|
|
|
|
}
|