DB: Pass DBError up naturally

This commit is contained in:
Natsu Kagami 2020-09-02 17:37:00 -04:00
parent 830faf7424
commit 1e40e54f17
No known key found for this signature in database
GPG key ID: F17543D4B9424B94

View file

@ -1,6 +1,9 @@
use rustbreak::{deser::Yaml, FileDatabase}; use rustbreak::{deser::Yaml, FileDatabase, RustbreakError as DBError};
use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serenity::{framework::standard::CommandError as Error, model::id::GuildId, prelude::*}; use serenity::{
model::id::GuildId,
prelude::{TypeMap, TypeMapKey},
};
use std::{collections::HashMap, path::Path}; use std::{collections::HashMap, path::Path};
/// GuildMap defines the guild-map type. /// GuildMap defines the guild-map type.
@ -12,7 +15,7 @@ pub struct DB<T>(std::marker::PhantomData<T>);
/// A short type abbreviation for a FileDatabase. /// A short type abbreviation for a FileDatabase.
type Database<T> = FileDatabase<T, Yaml>; type Database<T> = FileDatabase<T, Yaml>;
impl<T: std::any::Any + Send + Sync> serenity::prelude::TypeMapKey for DB<T> { impl<T: std::any::Any + Send + Sync> TypeMapKey for DB<T> {
type Value = Database<T>; type Value = Database<T>;
} }
@ -21,7 +24,7 @@ where
for<'de> T: Deserialize<'de>, for<'de> T: Deserialize<'de>,
{ {
/// Insert into a ShareMap. /// Insert into a ShareMap.
pub fn insert_into(data: &mut TypeMap, path: impl AsRef<Path>) -> Result<(), Error> { pub fn insert_into(data: &mut TypeMap, path: impl AsRef<Path>) -> Result<(), DBError> {
let db = Database::<T>::load_from_path_or_default(path)?; let db = Database::<T>::load_from_path_or_default(path)?;
data.insert::<DB<T>>(db); data.insert::<DB<T>>(db);
Ok(()) Ok(())
@ -61,13 +64,11 @@ where
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned, T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
{ {
/// Borrows the FileDatabase. /// Borrows the FileDatabase.
pub fn borrow(&'a self) -> Result<std::sync::RwLockReadGuard<T>, rustbreak::RustbreakError> { pub fn borrow(&'a self) -> Result<std::sync::RwLockReadGuard<T>, DBError> {
self.db.borrow_data() self.db.borrow_data()
} }
/// Borrows the FileDatabase for writing. /// Borrows the FileDatabase for writing.
pub fn borrow_mut( pub fn borrow_mut(&'a mut self) -> Result<std::sync::RwLockWriteGuard<T>, DBError> {
&'a mut self,
) -> Result<std::sync::RwLockWriteGuard<T>, rustbreak::RustbreakError> {
self.needs_save = true; self.needs_save = true;
self.db.borrow_data_mut() self.db.borrow_data_mut()
} }