aurora_db/network/
protocol.rs

1use crate::query::SimpleQueryBuilder;
2use crate::types::{Document, FieldType, Value};
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// A module to handle Base64 encoding for Vec<u8> in JSON.
7mod base64_serde {
8    use base64::{engine::general_purpose, Engine as _};
9    use serde::{self, Deserialize, Deserializer, Serializer};
10
11    pub fn serialize<S>(bytes: &Vec<u8>, serializer: S) -> Result<S::Ok, S::Error>
12    where
13        S: Serializer,
14    {
15        if serializer.is_human_readable() {
16            let base64 = general_purpose::STANDARD.encode(bytes);
17            serializer.serialize_str(&base64)
18        } else {
19            serializer.serialize_bytes(bytes)
20        }
21    }
22
23    pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
24    where
25        D: Deserializer<'de>,
26    {
27        if deserializer.is_human_readable() {
28            let base64 = <String>::deserialize(deserializer)?;
29            general_purpose::STANDARD
30                .decode(&base64)
31                .map_err(serde::de::Error::custom)
32        } else {
33            <Vec<u8>>::deserialize(deserializer)
34        }
35    }
36}
37
38/// Represents a request sent from a client to the server.
39#[derive(Serialize, Deserialize, Debug)]
40pub enum Request {
41    /// Get a value by key.
42    Get(String),
43    /// Put a value by key.
44    Put(String, #[serde(with = "base64_serde")] Vec<u8>),
45    /// Delete a key.
46    Delete(String),
47    /// Create a new collection.
48    NewCollection {
49        name: String,
50        fields: Vec<(String, FieldType, bool)>,
51    },
52    /// Insert a document into a collection.
53    Insert {
54        collection: String,
55        data: HashMap<String, Value>,
56    },
57    /// Get a document from a collection by ID.
58    GetDocument { collection: String, id: String },
59    /// Query a collection.
60    Query(SimpleQueryBuilder),
61    /// Begin a transaction.
62    BeginTransaction,
63    /// Commit the current transaction.
64    CommitTransaction,
65    /// Roll back the current transaction.
66    RollbackTransaction,
67}
68
69/// Represents a response sent from the server to a client.
70#[derive(Serialize, Deserialize, Debug)]
71pub enum Response {
72    /// A successful operation with an optional value.
73    Success(#[serde(with = "base64_serde_option")] Option<Vec<u8>>),
74    /// A successful operation with a Document.
75    Document(Option<Document>),
76    /// A successful operation with a list of Documents.
77    Documents(Vec<Document>),
78    /// A successful operation with a string response (e.g., a document ID).
79    Message(String),
80    /// A successful operation with no return value.
81    Done,
82    /// An error occurred.
83    Error(String),
84}
85
86/// A module for optional Base64 encoding.
87mod base64_serde_option {
88    use base64::{engine::general_purpose, Engine as _};
89    use serde::{self, Deserialize, Deserializer, Serializer};
90
91    pub fn serialize<S>(bytes: &Option<Vec<u8>>, serializer: S) -> Result<S::Ok, S::Error>
92    where
93        S: Serializer,
94    {
95        match bytes {
96            Some(b) => super::base64_serde::serialize(b, serializer),
97            None => serializer.serialize_none(),
98        }
99    }
100
101    pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Vec<u8>>, D::Error>
102    where
103        D: Deserializer<'de>,
104    {
105        let opt: Option<Vec<u8>> = if deserializer.is_human_readable() {
106            let base64: Option<String> = Option::deserialize(deserializer)?;
107            match base64 {
108                Some(s) => Some(
109                    general_purpose::STANDARD
110                        .decode(&s)
111                        .map_err(serde::de::Error::custom)?,
112                ),
113                None => None,
114            }
115        } else {
116            Option::deserialize(deserializer)?
117        };
118        Ok(opt)
119    }
120}