mysqladm/core/protocol/
request_response.rs

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std::{
    collections::BTreeSet,
    fmt::{Display, Formatter},
    ops::{Deref, DerefMut},
    str::FromStr,
};

use serde::{Deserialize, Serialize};
use tokio::net::UnixStream;
use tokio_serde::{formats::Bincode, Framed as SerdeFramed};
use tokio_util::codec::{Framed, LengthDelimitedCodec};

use crate::core::{database_privileges::DatabasePrivilegesDiff, protocol::*};

pub type ServerToClientMessageStream = SerdeFramed<
    Framed<UnixStream, LengthDelimitedCodec>,
    Request,
    Response,
    Bincode<Request, Response>,
>;

pub type ClientToServerMessageStream = SerdeFramed<
    Framed<UnixStream, LengthDelimitedCodec>,
    Response,
    Request,
    Bincode<Response, Request>,
>;

pub fn create_server_to_client_message_stream(socket: UnixStream) -> ServerToClientMessageStream {
    let length_delimited = Framed::new(socket, LengthDelimitedCodec::new());
    tokio_serde::Framed::new(length_delimited, Bincode::default())
}

pub fn create_client_to_server_message_stream(socket: UnixStream) -> ClientToServerMessageStream {
    let length_delimited = Framed::new(socket, LengthDelimitedCodec::new());
    tokio_serde::Framed::new(length_delimited, Bincode::default())
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct MySQLUser(String);

impl FromStr for MySQLUser {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(MySQLUser(s.to_string()))
    }
}

impl Deref for MySQLUser {
    type Target = String;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for MySQLUser {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Display for MySQLUser {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<&str> for MySQLUser {
    fn from(s: &str) -> Self {
        MySQLUser(s.to_string())
    }
}

impl From<String> for MySQLUser {
    fn from(s: String) -> Self {
        MySQLUser(s)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct MySQLDatabase(String);

impl FromStr for MySQLDatabase {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(MySQLDatabase(s.to_string()))
    }
}

impl Deref for MySQLDatabase {
    type Target = String;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for MySQLDatabase {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Display for MySQLDatabase {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<&str> for MySQLDatabase {
    fn from(s: &str) -> Self {
        MySQLDatabase(s.to_string())
    }
}

impl From<String> for MySQLDatabase {
    fn from(s: String) -> Self {
        MySQLDatabase(s)
    }
}

#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Request {
    CreateDatabases(Vec<MySQLDatabase>),
    DropDatabases(Vec<MySQLDatabase>),
    ListDatabases(Option<Vec<MySQLDatabase>>),
    ListPrivileges(Option<Vec<MySQLDatabase>>),
    ModifyPrivileges(BTreeSet<DatabasePrivilegesDiff>),

    CreateUsers(Vec<MySQLUser>),
    DropUsers(Vec<MySQLUser>),
    PasswdUser(MySQLUser, String),
    ListUsers(Option<Vec<MySQLUser>>),
    LockUsers(Vec<MySQLUser>),
    UnlockUsers(Vec<MySQLUser>),

    // Commit,
    Exit,
}

// TODO: include a generic "message" that will display a message to the user?

#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Response {
    // Specific data for specific commands
    CreateDatabases(CreateDatabasesOutput),
    DropDatabases(DropDatabasesOutput),
    ListDatabases(ListDatabasesOutput),
    ListAllDatabases(ListAllDatabasesOutput),
    ListPrivileges(GetDatabasesPrivilegeData),
    ListAllPrivileges(GetAllDatabasesPrivilegeData),
    ModifyPrivileges(ModifyDatabasePrivilegesOutput),

    CreateUsers(CreateUsersOutput),
    DropUsers(DropUsersOutput),
    PasswdUser(SetPasswordOutput),
    ListUsers(ListUsersOutput),
    ListAllUsers(ListAllUsersOutput),
    LockUsers(LockUsersOutput),
    UnlockUsers(UnlockUsersOutput),

    // Generic responses
    Ready,
    Error(String),
}