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
/// Request acts as both a request and a response, and is only used for the
/// communication between guest and host. It is not exposed via ft-sdk.
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct Request {
    pub uri: String,
    pub method: String,
    pub headers: Vec<(String, Vec<u8>)>,
    pub body: Vec<u8>,
}

impl Request {
    pub fn server_error(msg: String) -> Self {
        Request {
            uri: "server-error".to_string(),
            method: "500".to_string(),
            headers: vec![],
            body: msg.into_bytes(),
        }
    }
}

impl From<Request> for http::Request<bytes::Bytes> {
    fn from(r: Request) -> Self {
        let mut req = http::Request::builder()
            .method(r.method.as_str())
            .uri(r.uri.as_str());

        for (k, v) in r.headers {
            req = req.header(k, v);
        }

        req.body(r.body.into()).unwrap()
    }
}

impl From<Request> for http::Response<bytes::Bytes> {
    fn from(r: Request) -> Self {
        let mut req = http::Response::builder().status(r.method.parse::<u16>().unwrap());

        for (k, v) in r.headers {
            req = req.header(k, v);
        }

        req.body(r.body.into()).unwrap()
    }
}

impl From<http::Response<bytes::Bytes>> for Request {
    fn from(r: http::Response<bytes::Bytes>) -> Self {
        let (parts, body) = r.into_parts();
        let headers = parts
            .headers
            .iter()
            .map(|(k, v)| (k.as_str().to_string(), v.as_bytes().to_vec()))
            .collect();

        Request {
            uri: "response-has-no-url".to_string(),
            method: parts.status.as_str().to_string(),
            headers,
            body: body.to_vec(),
        }
    }
}

#[derive(Debug, serde::Deserialize, serde::Serialize, thiserror::Error)]
pub enum DecryptionError {
    #[error("Decryption failed: {0}")]
    Generic(String),
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct UserData {
    pub id: i64,
    pub username: String,
    pub name: String,
    pub email: String,
    pub verified_email: bool,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum DbError {
    DatabaseError {
        code: String,
        message: String,
        details: Option<String>,
        hint: Option<String>,
        table_name: Option<String>,
        column_name: Option<String>,
        constraint_name: Option<String>,
        statement_position: Option<i32>,
    },
    UnableToSendCommand(String),
}