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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use crate::errors::{EdbError, EdbResult, EdbResultExt};
use reqwest::{
	header::{CONTENT_LENGTH, CONTENT_TYPE},
	Client, Url,
};
use serde::{Deserialize, Serialize};
use serde_json::json;
pub use serde_json::Value as Json;
use std::{collections::HashMap, fs::read_to_string, io::Write, str::FromStr};
#[derive(Debug, Deserialize, Serialize)]

/// The main type for dealing with easydb.
///
/// Create an `EasyDB` using [`new`][EasyDB::new] or [`from_uuid_token`][EasyDB::from_uuid_token].
///
pub struct EasyDB {
	#[serde(rename = "UUID")]
	uuid: String,
	#[serde(rename = "Token")]
	token: String,
	#[serde(skip, default = "Client::new")]
	client: Client,
	#[serde(rename = "URL", default = "default_url")]
	url: String,
}

fn default_url() -> String {
	"https://app.easydb.io/database/".to_string()
}

impl EasyDB {
	/// Creates an EasyDB using the `easydb.toml` in the current directory.
	///
	/// # Errors
	///
	/// Will fail if the file cannot be read (e.g. when it doesn't exist), or if the UUID or URL
	/// does not form a valid URL.
	///
	/// # Example
	///
	/// ```
	/// # use crate::easydb::{EasyDB, errors::EdbError};
	/// let edb = EasyDB::new()?;
	/// # Ok::<(), EdbError>(())
	/// ```
	///
	pub fn new() -> EdbResult<Self> {
		let edb: Self = read_to_string("./easydb.toml")?.parse()?;
		edb.validate_uuid()?;
		Ok(edb)
	}
	/// Creates an EasyDB using a UUID, Token, and optional URL (defaults to
	/// `https://app.easydb.io/database/`).
	///
	/// # Errors
	///
	/// Will fail if `url` or `uuid` don't form a valid URL.
	///
	/// # Example
	///
	/// ```
	/// # use crate::easydb::{EasyDB, errors::EdbError};
	/// let edb = EasyDB::from_uuid_token("aaaa...".to_string(), "bbbb...".to_string(), None);
	/// # Ok::<(), EdbError>(())
	/// ```
	///
	pub fn from_uuid_token(uuid: String, token: String, url: Option<String>) -> EdbResult<Self> {
		let edb = Self {
			uuid,
			token,
			client: Client::new(),
			url: url.unwrap_or_else(default_url),
		};
		edb.url.parse::<Url>()?;
		edb.validate_uuid()?;
		Ok(edb)
	}
	fn validate_uuid(&self) -> EdbResult<()> {
		self.url.parse::<Url>().unwrap().join(&self.uuid)?;
		Ok(())
	}
	fn create_key_url(&self, key: &str) -> EdbResult<Url> {
		self.url
			.parse::<Url>()
			.unwrap()
			.join(&format!("{}/", self.uuid))
			.unwrap()
			.join(key)
			.chain_err(|| format!("Invalid key: {}", key))
	}
	/// Returns the stored UUID.
	pub fn uuid(&self) -> &str {
		&self.uuid
	}
	/// Returns the stored token.
	pub fn token(&self) -> &str {
		&self.token
	}
	/// Returns the stored URL.
	pub fn url(&self) -> &str {
		&self.url
	}

	/// Gets the value associated with `key`.
	///
	/// # Example
	///
	/// ```
	/// # use crate::easydb::{EasyDB, errors::EdbError};
	/// # let edb = EasyDB::new()?;
	/// let s = edb.get("somekey")?;
	/// # Ok::<(), EdbError>(())
	/// ```
	///
	pub fn get(&self, key: &str) -> EdbResult<String> {
		self.get_json(key)?
			.as_str()
			.map(|s| s.to_string())
			.ok_or_else(|| "Value was not a string".into())
	}
	/// Gets the value associated with `key` in json format.
	///
	/// # Example
	///
	/// ```
	/// # use crate::easydb::{EasyDB, errors::EdbError};
	/// # let edb = EasyDB::new()?;
	/// let json = edb.get_json("somekey")?;
	/// # Ok::<(), EdbError>(())
	/// ```
	///
	pub fn get_json(&self, key: &str) -> EdbResult<Json> {
		let mut s = Vec::new();
		self.get_writer(key, &mut s)?;
		Ok(serde_json::from_slice(&s)?)
	}
	/// Assigns `value` to `key` and returns the status code.
	///
	/// # Example
	///
	/// ```
	/// # use crate::easydb::{EasyDB, errors::EdbError};
	/// # let edb = EasyDB::new()?;
	/// let status = edb.put("somekey", "somevalue")?;
	/// # Ok::<(), EdbError>(())
	/// ```
	///
	pub fn put(&self, key: &str, value: &str) -> EdbResult<u16> {
		let new_value = json!(value);
		self.put_json(key, new_value)
	}
	/// Assigns a json `value` to `key` and returns the status code.
	///
	/// # Example
	///
	/// ```
	/// # use crate::easydb::{EasyDB, errors::EdbError};
	/// # use serde_json::json;
	/// # let edb = EasyDB::new()?;
	/// let status = edb.put_json("somekey", json!({"a": "b"}))?;
	/// # Ok::<(), EdbError>(())
	/// ```
	///
	pub fn put_json(&self, key: &str, value: Json) -> EdbResult<u16> {
		let body = json!({ "value": value }).to_string();
		Ok(self
			.client
			.post(self.create_key_url(key)?)
			.header(CONTENT_TYPE, "application/json")
			.header(CONTENT_LENGTH, body.len())
			.header("token", &self.token)
			.body(body)
			.send()?
			.status()
			.as_u16())
	}
	/// Deletes the value associated with `key` and returns the status code.
	///
	/// # Example
	///
	/// ```
	/// # use crate::easydb::{EasyDB, errors::EdbError};
	/// # let edb = EasyDB::new()?;
	/// let status = edb.delete("somekey")?;
	/// # Ok::<(), EdbError>(())
	/// ```
	///
	pub fn delete(&self, key: &str) -> EdbResult<u16> {
		Ok(self
			.client
			.delete(self.create_key_url(key)?)
			.header(CONTENT_TYPE, "application/json")
			.header("token", &self.token)
			.send()?
			.status()
			.as_u16())
	}
	/// Returns a `HashMap<String, String>` of all the data in this database.
	///
	/// # Errors
	///
	/// Will fail if any of the values are not strings.
	///
	/// # Example
	///
	/// ```
	/// # use crate::easydb::{EasyDB, errors::EdbError};
	/// # let edb = EasyDB::new()?;
	/// let map = edb.list()?;
	/// # Ok::<(), EdbError>(())
	/// ```
	///
	pub fn list(&self) -> EdbResult<HashMap<String, String>> {
		self.list_json()?
			.drain()
			.map(|(s, v)| match v.as_str() {
				Some(v_str) => Ok((s, v_str.to_string())),
				None => Err(format!("A value was not a string: key: {}, value: {}", s, v).into()),
			})
			.collect()
	}
	/// Returns a `HashMap<String, Json>` of all the data in this database.
	///
	/// # Example
	///
	/// ```
	/// # use crate::easydb::{EasyDB, errors::EdbError};
	/// # let edb = EasyDB::new()?;
	/// let map = edb.list()?;
	/// # Ok::<(), EdbError>(())
	/// ```
	///
	pub fn list_json(&self) -> EdbResult<HashMap<String, Json>> {
		let mut s = Vec::new();
		self.list_writer(&mut s)?;
		Ok(serde_json::from_slice(&s)?)
	}
	/// Clears the database.
	///
	/// # Example
	///
	/// ```
	/// # use crate::easydb::{EasyDB, errors::EdbError};
	/// # let edb = EasyDB::new()?;
	/// edb.clear()?;
	/// # std::thread::sleep(std::time::Duration::from_secs(1));
	/// assert_eq!(edb.list()?.len(), 0);
	/// # Ok::<(), EdbError>(())
	/// ```
	///
	pub fn clear(&self) -> EdbResult<()> {
		let map = self.list_json()?;
		self.clear_keys(map.keys().map(|k| &k[..]))?;
		Ok(())
	}
	fn clear_keys<'a, I>(&self, keys: I) -> EdbResult<()>
	where
		I: Iterator<Item = &'a str>,
	{
		for key in keys {
			self.delete(key)?;
		}
		Ok(())
	}
	/// An alternative to `get()` that works with a writer. Fetches data associated with `key` and
	/// writes into `value`, returning the status code.
	///
	/// The response should have the value that was originally set in JSON form. If the key was
	/// never set, was deleted, or has no data, the response will be an empty string: `""`.
	pub fn get_writer<W>(&self, key: &str, value: &mut W) -> EdbResult<u16>
	where
		W: Write,
	{
		let mut resp = self
			.client
			.get(self.create_key_url(key)?)
			.header("token", &self.token)
			.send()?;
		resp.copy_to(value)?;
		Ok(resp.status().as_u16())
	}
	/// An alternative to `list()` that works with a writer. Fetches all the data in the database
	/// and writes it to `list`, returning the status code.
	pub fn list_writer<W>(&self, list: &mut W) -> EdbResult<u16>
	where
		W: Write,
	{
		let mut resp = self
			.client
			.get(self.url.parse::<Url>().unwrap().join(&self.uuid).unwrap())
			.header("token", &self.token)
			.send()?;
		resp.copy_to(list)?;
		Ok(resp.status().as_u16())
	}
}

impl FromStr for EasyDB {
	type Err = EdbError;
	/// Create an `EasyDB` from a `&str` in the TOML format.
	///
	/// # Example
	///
	/// ```
	/// # use crate::easydb::{EasyDB, errors::EdbError};
	/// let s = r#"
	/// UUID = "abcd"
	/// Token = "efgh"
	/// "#;
	/// let edb: EasyDB = s.parse()?;
	/// assert_eq!(edb.uuid(), "abcd");
	/// assert_eq!(edb.token(), "efgh");
	/// # Ok::<(), EdbError>(())
	/// ```
	///
	fn from_str(s: &str) -> Result<Self, Self::Err> {
		Ok(toml::from_str(s)?)
	}
}