summaryrefslogtreecommitdiffstats
path: root/atuin-common/src/api.rs
blob: aaf8f6c5ff8cfe62118cb5e6f208f8da2451e188 (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::{borrow::Cow, convert::Infallible};

use chrono::Utc;
use serde::{Deserialize, Serialize};
use warp::{reply::Response, Reply};

#[derive(Debug, Serialize, Deserialize)]
pub struct UserResponse<'a> {
    pub username: Cow<'a, str>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RegisterRequest<'a> {
    pub email: Cow<'a, str>,
    pub username: Cow<'a, str>,
    pub password: Cow<'a, str>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RegisterResponse<'a> {
    pub session: Cow<'a, str>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LoginRequest<'a> {
    pub username: Cow<'a, str>,
    pub password: Cow<'a, str>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LoginResponse<'a> {
    pub session: Cow<'a, str>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AddHistoryRequest<'a, D> {
    pub id: Cow<'a, str>,
    pub timestamp: chrono::DateTime<Utc>,
    pub data: D,
    pub hostname: Cow<'a, str>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CountResponse {
    pub count: i64,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SyncHistoryRequest<'a> {
    pub sync_ts: chrono::DateTime<chrono::FixedOffset>,
    pub history_ts: chrono::DateTime<chrono::FixedOffset>,
    pub host: Cow<'a, str>,
}

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

#[derive(Debug, Serialize, Deserialize)]
pub struct ErrorResponse<'a> {
    pub reason: Cow<'a, str>,
}

impl Reply for ErrorResponse<'_> {
    fn into_response(self) -> Response {
        warp::reply::json(&self).into_response()
    }
}

pub struct ErrorResponseStatus<'a> {
    pub error: ErrorResponse<'a>,
    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<'a> ErrorResponse<'a> {
    pub fn with_status(self, status: warp::http::StatusCode) -> ErrorResponseStatus<'a> {
        ErrorResponseStatus {
            error: self,
            status,
        }
    }

    pub fn reply(reason: &'a str) -> ErrorResponse {
        Self {
            reason: reason.into(),
        }
    }
}

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