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
use {
	crate::{Context, Database, DatabaseInner, Glue, InterfaceError, Result},
	std::sync::MutexGuard,
};

impl Glue {
	// TODO: None ref should give a primary
	pub fn get_database(&self, db_ref: &Option<String>) -> Result<MutexGuard<Box<DatabaseInner>>> {
		self.databases
			.get(db_ref.as_ref().unwrap_or(&self.primary))
			.ok_or(InterfaceError::DatabaseNotFound.into())
			.map(|db| db.get())
	}
	pub fn get_mut_database(&mut self, db_ref: &Option<String>) -> Result<&mut Box<DatabaseInner>> {
		self.databases
			.get_mut(db_ref.as_ref().unwrap_or(&self.primary))
			.ok_or(InterfaceError::DatabaseNotFound.into())
			.map(Database::get_mut)
	}
	pub fn get_context(&self) -> Result<MutexGuard<Context>> {
		self.context
			.lock()
			.map_err(|_| InterfaceError::ContextUnavailable.into())
	}
	pub fn get_mut_context(&mut self) -> Result<&mut Context> {
		self.context
			.get_mut()
			.map_err(|_| InterfaceError::ContextUnavailable.into())
	}
	pub fn get_database_list(&self) -> Vec<&String> {
		self.databases.keys().collect()
	}
}