summaryrefslogtreecommitdiffstats
path: root/atuin-common/src/api.rs
blob: 44a73c1cff60fcb746c9313de6c7da21575e6107 (plain)
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
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<Utc>,
    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<chrono::FixedOffset>,
    pub history_ts: chrono::DateTime<chrono::FixedOffset>,
    pub host: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SyncHistoryResponse {
    pub history: Vec<String>,
}

#[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<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))
}