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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
use crate::commands::{from_op, TuringOp}; use anyhow::Result; use serde::Serialize; /// ### Handles all queries releated to fields /// ```rust ///#[derive(Debug, Serialize, Clone)] ///pub struct FieldQuery { /// db: String, /// document: String, /// field: String, /// payload: Option<T>, ///} ///``` #[derive(Debug, Serialize, Clone)] pub struct FieldQuery<T> { db: String, document: String, field: String, payload: Option<T>, } impl<T> FieldQuery<T> where T: serde::Serialize, { /// ### Initialize a new empty field /// #### Usage /// ```rust /// use crate::FieldQuery; /// /// FieldQuery::new().await /// ``` pub async fn new() -> Self { Self { db: Default::default(), document: Default::default(), field: Default::default(), payload: Default::default(), } } /// ### Add a database name /// #### Usage /// ```rust /// use crate::FieldQuery; /// /// let mut foo = FieldQuery::new().await; /// foo.db("db_name").await; /// ``` pub async fn db(&mut self, name: &str) -> &Self { self.db = name.into(); self } /// ### Add a document name /// #### Usage /// ```rust /// use crate::FieldQuery; /// /// let mut foo = FieldQuery::new().await; /// foo /// .db("db_name").await /// .document("document_name").await; /// ``` pub async fn document(&mut self, name: &str) -> &Self { self.document = name.into(); self } /// ### Add a field name /// #### Usage /// ```rust /// use crate::FieldQuery; /// /// let mut foo = FieldQuery::new().await; /// foo /// .db("db_name").await /// .document("document_name").await /// .field("field_name").await; /// ``` pub async fn field(&mut self, name: &str) -> &Self { self.field = name.into(); self } /// ### Add a payload of bytes /// This takes a generic value and convertes it into bytes using bincode /// #### Usage /// ```rust /// use crate::FieldQuery; /// /// let mut foo = FieldQuery::new().await; /// foo /// .db("db_name").await /// .document("document_name").await /// .field("field_name").await /// .payload("my_data_converted_into_bytes".as_bytes()).await; /// ``` pub async fn payload(&mut self, value: T) -> &Self { self.payload = Some(value); self } /// ### Inserts a `key/value` to a document in a database /// #### Usage /// ```rust /// use crate::FieldQuery; /// /// let mut foo = FieldQuery::new().await; /// foo /// .db("db_name").await /// .document("document_name").await /// .field("field_name").await /// .payload("my_data_converted_into_bytes".as_bytes()).await /// .set().await /// ``` pub async fn set(&self) -> Result<Vec<u8>> { let mut packet = from_op(&TuringOp::FieldInsert).await.to_vec(); packet.extend_from_slice(self.db.as_bytes()); let data = bincode::serialize::<Self>(self)?; packet.extend_from_slice(&data); Ok(packet) } /// ### Gets a `value` to a document in a database by `key` /// #### Usage /// ```rust /// use crate::FieldQuery; /// /// let mut foo = FieldQuery::new().await; /// foo /// .db("db_name").await /// .document("document_name").await /// .field("field_name").await /// .get().await; /// ``` pub async fn get(&self) -> Result<Vec<u8>> { let mut packet = from_op(&TuringOp::FieldGet).await.to_vec(); packet.extend_from_slice(self.db.as_bytes()); let data = bincode::serialize::<Self>(self)?; packet.extend_from_slice(&data); Ok(packet) } /// ### List all the `keys` in a document /// #### Usage /// ```rust /// use crate::FieldQuery; /// /// let mut foo = FieldQuery::new().await; /// foo /// .db("db_name").await /// .document("document_name").await /// .list().await; /// ``` pub async fn list(&self) -> Result<Vec<u8>> { let mut packet = from_op(&TuringOp::FieldList).await.to_vec(); packet.extend_from_slice(self.db.as_bytes()); let data = bincode::serialize::<Self>(self)?; packet.extend_from_slice(&data); Ok(packet) } /// ### Removes a `value` from a document in a database by `key` /// #### Usage /// ```rust /// use crate::FieldQuery; /// /// let mut foo = FieldQuery::new().await; /// foo /// .db("db_name").await /// .document("document_name").await /// .field("field_name").await /// .remove().await; /// ``` pub async fn remove(&self) -> Result<Vec<u8>> { let mut packet = from_op(&TuringOp::FieldRemove).await.to_vec(); packet.extend_from_slice(self.db.as_bytes()); let data = bincode::serialize::<Self>(self)?; packet.extend_from_slice(&data); Ok(packet) } /// ### Modifies a `value` in a document in a database by its `key` /// #### Usage /// ```rust /// use crate::FieldQuery; /// /// let mut foo = FieldQuery::new().await; /// foo /// .db("db_name").await /// .document("document_name").await /// .field("field_name").await /// .payload("my_data_converted_into_bytes".as_bytes()).await /// .modify().await /// ``` pub async fn modify(&self) -> Result<Vec<u8>> { let mut packet = from_op(&TuringOp::FieldModify).await.to_vec(); packet.extend_from_slice(self.db.as_bytes()); let data = bincode::serialize::<Self>(self)?; packet.extend_from_slice(&data); Ok(packet) } }