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
use crate::{message_type::MessageType, query::Query};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub enum Message {
    ClientSetup(ClientSetupSecureConnection),
    ClientAuthentification(ClientAuthentication),
    Insert(Insertion),
    InsertResponse { inserted_id: String },
    Query(Query),
    QueryResponse { data: Vec<Vec<u8>> },
    SingleValueResponse { data: Option<Vec<u8>> },
    Count(CountSubject),
    CountResponse(u32), // This is probably a bad idea
    Update(Update),
    UpdateResponse { status: UpdateStatus },
    Delete(Delete),
    DeleteResult(bool),
    DeleteForUsecase { collection: String, id: String },
    Drop(DropSubject),
    DropResult(bool),
    EndOfCommunication,
    CloseCommunication,
}

impl Message {
    pub fn message_type(&self) -> MessageType {
        match self {
            Message::ClientSetup(_) => MessageType::Setup,
            Message::ClientAuthentification(_) => MessageType::Authentification,
            Message::Insert(_) => MessageType::Insert,
            Message::InsertResponse { .. } => MessageType::InsertResponse,
            Message::Query(_) => MessageType::Query,
            Message::QueryResponse { .. } => MessageType::QueryResponse,
            Message::SingleValueResponse { .. } => MessageType::SingleValueResponse,
            Message::Count(_) => MessageType::Count,
            Message::CountResponse(_) => todo!(),
            Message::Update { .. } => MessageType::Update,
            Message::UpdateResponse { .. } => MessageType::UpdateResponse,
            Message::Delete(_) => MessageType::Delete,
            Message::DeleteResult(_) => MessageType::DeleteResult,
            Message::DeleteForUsecase { .. } => MessageType::DeleteForUsecase,
            Message::Drop(_) => MessageType::Drop,
            Message::DropResult(_) => MessageType::DropResult,
            Message::EndOfCommunication => MessageType::EndOfCommunication,
            Message::CloseCommunication => MessageType::CloseCommunication,
        }
    }

    pub fn setup_for_network(&self) -> Result<Vec<u8>, serde_cbor::Error> {
        let message_type: MessageType = self.message_type();
        let message_type: u8 = message_type as u8;
        let message = serde_cbor::to_vec(&self)?;
        let message_length = message.len() as u32;
        let message_length = message_length.to_be_bytes();

        let message_type_as_bytes = [message_type];
        Ok([&message_type_as_bytes[..], &message_length, &message].concat())
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub enum CountSubject {
    Collection(String),
    Usecase { collection: String, usecase: String },
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub enum DropSubject {
    Collection(String),
    Usecase { collection: String, usecase: String },
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct ClientSetupSecureConnection {
    protocol_version: String,
    client_public_key: Vec<u8>,
    cipher_suits: Vec<String>,
    compression: String,
}

impl ClientSetupSecureConnection {
    pub fn new(public_key: Vec<u8>) -> Self {
        Self {
            protocol_version: String::from("0.1.0"),
            client_public_key: public_key,
            cipher_suits: vec![String::from("kyber768"), String::from("falcon")],
            compression: String::from("0"),
        }
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct ClientAuthentication {
    pub username: String,
    pub password: String,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct Update {
    pub collection: String,
    pub id: String,
    pub new_value: Vec<u8>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub enum UpdateStatus {
    Success,
    Failure,
    KeyNotFound,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct Insertion {
    pub collection: String,
    pub acl: Vec<String>,
    pub data: Vec<u8>,
    pub usecases: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct Delete {
    pub collection: String,
    pub id: String,
}