-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstructs.rs
72 lines (60 loc) · 1.41 KB
/
structs.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
use std::net::SocketAddr;
use serde::{Deserialize, Serialize};
// Hashmap struct
#[derive(Debug, Clone)]
pub struct UserData {
pub name: String,
pub color: String,
pub addr: SocketAddr,
// pub socket: &'a std::net::TcpStream,
}
// Data blocks
#[derive(Debug, Deserialize, Clone)]
#[serde(untagged)]
pub enum RequestBlock {
#[serde(rename = "data")]
Login(ReqLogin),
#[serde(rename = "data")]
Msg(String),
}
#[derive(Debug, Serialize, Clone)]
#[serde(untagged)]
pub enum ResponseBlock<'a> {
#[serde(rename = "data")]
Error(ResError<'a>),
#[serde(rename = "data")]
Msg(ResMsg),
}
// Base structs
#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Request {
pub op: String,
// #[serde(flatten)]
pub data: RequestBlock,
}
#[derive(Debug, Serialize, Clone)]
pub struct Response<'a> {
pub op: &'a str,
pub status: &'a str,
// #[serde(flatten)]
pub data: ResponseBlock<'a>,
}
// Request data blocks
#[derive(Debug, Deserialize, Clone)]
pub struct ReqLogin {
pub name: String,
pub color: String,
}
// Response data blocks
#[derive(Debug, Serialize, Clone)]
pub struct ResError<'a> {
pub kind: &'a str,
pub msg: String,
}
#[derive(Debug, Serialize, Clone)]
pub struct ResMsg {
pub user: String,
pub color: String,
pub msg: String,
}