turingdb_helpers/db.rs
1use crate::commands::{from_op, TuringOp};
2
3/// ### Handles all queries releated to fields
4/// ```rust
5/// #[derive(Debug, Clone)]
6/// pub struct DbQuery {
7/// db: String,
8/// }
9/// ```
10#[derive(Debug, Clone, Default)]
11pub struct DbQuery {
12 db: String,
13}
14
15impl<'tp> DbQuery {
16 /// ### Initialize a new empty database
17 /// #### Usage
18 /// ```rust
19 /// use crate::DatabaseQuery;
20 ///
21 /// Database::new()
22 /// ```
23 pub fn new() -> Self {
24 Self {
25 db: Default::default(),
26 }
27 }
28 /// ### Add a database name
29 /// #### Usage
30 /// ```rust
31 /// use crate::DatabaseQuery;
32 ///
33 /// let mut foo = Database::new();
34 /// foo.db("db_name");
35 /// ```
36 pub fn db(&mut self, name: &str) -> &Self {
37 self.db = name.into();
38
39 self
40 }
41 /// ### Creates a new a database in a repo
42 /// #### Usage
43 /// ```rust
44 /// use crate::DatabaseQuery;
45 ///
46 /// let mut foo = DatabaseQuery::new();
47 /// foo
48 /// .db("db_name")
49 /// .create()
50 /// ```
51 pub fn create(&self) -> Vec<u8> {
52 let mut packet = from_op(&TuringOp::DbCreate).to_vec();
53 packet.extend_from_slice(self.db.as_bytes());
54
55 packet
56 }
57 /// ### Creates a new a database in a repo
58 /// #### Usage
59 /// ```rust
60 /// use crate::DatabaseQuery;
61 ///
62 /// let mut foo = DatabaseQuery::new();
63 /// foo
64 /// .db("db_name").await
65 /// .drop().await
66 /// ```
67 pub fn drop(&self) -> Vec<u8> {
68 let mut packet = from_op(&TuringOp::DbDrop).to_vec();
69 packet.extend_from_slice(self.db.as_bytes());
70
71 packet
72 }
73 /// ### List all databases in a repo
74 /// #### Usage
75 /// ```rust
76 /// use crate::DatabaseQuery;
77 ///
78 /// let mut foo = DatabaseQuery::new();
79 /// foo.list()
80 /// ```
81 pub fn list(&self) -> &'tp [u8] {
82 from_op(&TuringOp::DbList)
83 }
84}