easydb/lib.rs
1#![doc(html_root_url = "https://docs.rs/easydb/0.2.0")]
2
3//! An interface for working with [easydb.io](https://easydb.io) in rust.
4//!
5//! # Quick start
6//!
7//! ```
8//! # use std::collections::HashMap;
9//! # use crate::easydb::errors::EdbError;
10//! use easydb::EasyDB;
11//!
12//! // Create an EasyDB struct to interact with.
13//! // Gets information from `./easydb.toml`.
14//! let edb: EasyDB = EasyDB::new()?;
15//! # edb.clear()?;
16//! #
17//! # std::thread::sleep(std::time::Duration::from_secs(1));
18//!
19//! // Store some data
20//! edb.put("hello", "world")?;
21//! edb.put("goodbye", "earth")?;
22//! #
23//! # std::thread::sleep(std::time::Duration::from_secs(1));
24//!
25//! // Get a single item
26//! let stored_hello: String = edb.get("hello")?;
27//! assert_eq!(&stored_hello, "world");
28//!
29//! // Update an item
30//! edb.put("goodbye", "dirt")?;
31//! # std::thread::sleep(std::time::Duration::from_secs(1));
32//! assert_eq!(&edb.get("goodbye")?, "dirt");
33//!
34//! // Get a HashMap of all database entries
35//! let resp: HashMap<String, String> = edb.list()?;
36//! assert_eq!(&resp["hello"], "world");
37//! assert_eq!(&resp["goodbye"], "dirt");
38//!
39//! // Delete items
40//! edb.delete("hello")?;
41//! # std::thread::sleep(std::time::Duration::from_secs(1));
42//! let deleted_item: String = edb.get("hello")?;
43//! assert_eq!(&deleted_item, "");
44//! # edb.clear()?;
45//! # Ok::<(), EdbError>(())
46//! ```
47//!
48//! # Commands
49//!
50//! The easiest way to create an [`EasyDB`][EasyDB] is to call [`EasyDB::new()`][EasyDB::new].
51//! This generates the struct using data in `./easydb.toml`, which should include the following
52//! information:
53//!
54//! ```toml
55//! UUID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
56//! Token = "ffffffff-0000-1111-2222-333333333333"
57//! URL = "https://app.easydb.io/database/"
58//! ```
59//!
60//! The `URL` field is optional and will default to `https://app.easydb.io/database/`.
61//!
62//! If your toml is not at the default location, you can just
63//! [`parse`](./struct.EasyDB.html#method.from_str) it from a string in toml format. For example:
64//!
65//! ```
66//! # use easydb::EasyDB;
67//! let edb: EasyDB = "UUID = \"aaaa...\"\nToken = \"ffff...\"".parse().unwrap();
68//! ```
69//!
70//! If you have individual items, you can initalize an [`EasyDB`][EasyDB] with
71//! [`from_uuid_token`][EasyDB::from_uuid_token]:
72//!
73//! ```
74//! # use easydb::EasyDB;
75//! let edb = EasyDB::from_uuid_token("aaaa...".to_string(), "ffff...".to_string(), None);
76//! ```
77//!
78//! ## Using EasyDB
79//!
80//! The four methods [**`get`**][EasyDB::get], [**`put`**][EasyDB::put],
81//! [**`delete`**][EasyDB::delete], and [**`list`**][EasyDB::list] correspond to the four available
82//! APIs in `easydb.io`. `get` and `delete` take one argument: a key. `put` takes two arguments: a
83//! `key` and a `value`. `list` takes no arguments. Example usage can be seen in the
84//! [quick start](#quick-start) section at the top of this page.
85//!
86//! The above methods deal with [`String`](https://doc.rust-lang.org/std/string/struct.String.html)
87//! values and will fail if any value is not a JSON string. If you would like to use JSON, there are
88//! [**`get_json`**][EasyDB::get_json], [**`put_json`**][EasyDB::put_json], and
89//! [**`list_json`**][EasyDB::list_json] ([**`delete`**][EasyDB::delete] is the same). These deal
90//! with `value`s that are of the `Json` type, which is a re-export of the
91//! [`Value`](https://docs.serde.rs/serde_json/enum.Value.html) type from `serde_json`.
92//!
93//! In addition, there is the [**`clear`**][EasyDB::clear] method for easily clearing the entire
94//! database, which, for example, is useful when initializing the database. This just calls
95//! [`delete`][EasyDB::delete] on every item, but if easydb.io implements a clear function in the
96//! future, this will call it.
97//!
98//! ## Errors
99//!
100//! All network errors as reported by the `reqwest` crate are returned in `Result`s. Other errors
101//! are documented on their respective methods.
102//!
103//! Due to the unknown nature of the database, there may be unexpected results when reading data
104//! just after writing data. Expect that read values will be either up-to-date or old values.
105//!
106
107mod easydb;
108pub use crate::easydb::EasyDB;
109
110/// Re-exported [`Value`](https://docs.serde.rs/serde_json/enum.Value.html) type from serde_json.
111pub use crate::easydb::Json;
112
113pub mod errors;
114
115// Note that in order to run tests, you must create an `easydb.toml` in the current directory.
116#[cfg(test)]
117mod tests {
118 use crate::{errors::EdbResult, EasyDB};
119 use serde_json::json;
120 #[test]
121 fn list() -> EdbResult<()> {
122 let edb = EasyDB::new()?;
123 edb.clear()?;
124 edb.put("hello", "world")?;
125 edb.put("goodbye", "earth")?;
126 std::thread::sleep(std::time::Duration::from_secs(1));
127 assert_eq!(&edb.get("hello")?, "world");
128 let list = edb.list()?;
129 assert_eq!(&list["hello"], "world");
130 assert_eq!(&list["goodbye"], "earth");
131 edb.delete("hello")?;
132 edb.delete("goodbye")?;
133 std::thread::sleep(std::time::Duration::from_secs(1));
134 let list = edb.list()?;
135 assert!(list.get("hello").is_none());
136 assert!(list.get("goodbye").is_none());
137 Ok(())
138 }
139 #[test]
140 fn list_json() -> EdbResult<()> {
141 let edb = EasyDB::new()?;
142 edb.clear()?;
143 edb.put_json("hello", json!("world"))?;
144 edb.put_json(
145 "goodbye",
146 json!({
147 "a": "b",
148 "c": ["d", "e"]
149 }),
150 )?;
151 std::thread::sleep(std::time::Duration::from_secs(1));
152 let list = edb.list_json()?;
153 assert_eq!(list["hello"], json!("world"));
154 assert_eq!(
155 list["goodbye"],
156 json!({
157 "a": "b",
158 "c": ["d", "e"]
159 })
160 );
161 Ok(())
162 }
163}