use std::convert::Infallible; use chrono::Utc; use serde::Serialize; use warp::{reply::Response, Reply}; #[derive(Debug, Serialize, Deserialize)] pub struct UserResponse { pub username: String, } #[derive(Debug, Serialize, Deserialize)] pub struct RegisterRequest { pub email: String, pub username: String, pub password: String, } #[derive(Debug, Serialize, Deserialize)] pub struct RegisterResponse { pub session: String, } #[derive(Debug, Serialize, Deserialize)] pub struct LoginRequest { pub username: String, pub password: String, } #[derive(Debug, Serialize, Deserialize)] pub struct LoginResponse { pub session: String, } #[derive(Debug, Serialize, Deserialize)] pub struct AddHistoryRequest { pub id: String, pub timestamp: chrono::DateTime, pub data: String, pub hostname: String, } #[derive(Debug, Serialize, Deserialize)] pub struct CountResponse { pub count: i64, } #[derive(Debug, Serialize, Deserialize)] pub struct SyncHistoryRequest { pub sync_ts: chrono::DateTime, pub history_ts: chrono::DateTime, pub host: String, } #[derive(Debug, Serialize, Deserialize)] pub struct SyncHistoryResponse { pub history: Vec, } #[derive(Debug, Serialize, Deserialize)] pub struct ErrorResponse { pub reason: String, } impl Reply for ErrorResponse { fn into_response(self) -> Response { warp::reply::json(&self).into_response() } } pub struct ErrorResponseStatus { pub error: ErrorResponse, pub status: warp::http::StatusCode, } impl Reply for ErrorResponseStatus { fn into_response(self) -> Response { warp::reply::with_status(self.error, self.status).into_response() } } impl ErrorResponse { pub fn with_status(self, status: warp::http::StatusCode) -> ErrorResponseStatus { ErrorResponseStatus { error: self, status, } } pub fn reply(reason: &str) -> ErrorResponse { Self { reason: reason.to_string(), } } } pub enum ReplyEither { Ok(T), Err(E), } impl Reply for ReplyEither { fn into_response(self) -> Response { match self { ReplyEither::Ok(t) => t.into_response(), ReplyEither::Err(e) => e.into_response(), } } } pub type ReplyResult = Result, Infallible>; pub fn reply_error(e: E) -> ReplyResult { Ok(ReplyEither::Err(e)) } pub type JSONResult = Result, Infallible>; pub fn reply_json(t: impl Serialize) -> JSONResult { reply(warp::reply::json(&t)) } pub fn reply(t: T) -> ReplyResult { Ok(ReplyEither::Ok(t)) }