mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-16 07:18:54 +00:00
Improve db ergonomics
This commit is contained in:
parent
c4916a24f7
commit
d5f7a17a2c
3 changed files with 13 additions and 23 deletions
|
@ -3,6 +3,7 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
|||
use serenity::{framework::standard::CommandError as Error, model::id::GuildId, prelude::*};
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// GuildMap defines the guild-map type.
|
||||
/// It is basically a HashMap from a GuildId to a data structure.
|
||||
|
@ -10,7 +11,7 @@ pub type GuildMap<V> = HashMap<GuildId, V>;
|
|||
/// The generic DB type we will be using.
|
||||
pub struct DB<T>(std::marker::PhantomData<T>);
|
||||
impl<T: std::any::Any> serenity::prelude::TypeMapKey for DB<T> {
|
||||
type Value = FileDatabase<T, Ron>;
|
||||
type Value = Arc<FileDatabase<T, Ron>>;
|
||||
}
|
||||
|
||||
impl<T: std::any::Any + Default + Send + Sync + Clone + Serialize + std::fmt::Debug> DB<T>
|
||||
|
@ -24,32 +25,33 @@ where
|
|||
dbg!(e);
|
||||
db.save()
|
||||
})?;
|
||||
data.insert::<DB<T>>(db);
|
||||
data.insert::<DB<T>>(Arc::new(db));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Open a previously inserted DB.
|
||||
pub fn open(data: &ShareMap) -> DBWriteGuard<'_, T> {
|
||||
data.get::<Self>().expect("DB initialized").into()
|
||||
pub fn open(data: &ShareMap) -> DBWriteGuard<T> {
|
||||
data.get::<Self>().expect("DB initialized").clone().into()
|
||||
}
|
||||
}
|
||||
|
||||
/// The write guard for our FileDatabase.
|
||||
/// It wraps the FileDatabase in a write-on-drop lock.
|
||||
pub struct DBWriteGuard<'a, T>(&'a FileDatabase<T, Ron>)
|
||||
#[derive(Debug)]
|
||||
pub struct DBWriteGuard<T>(Arc<FileDatabase<T, Ron>>)
|
||||
where
|
||||
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned;
|
||||
|
||||
impl<'a, T> From<&'a FileDatabase<T, Ron>> for DBWriteGuard<'a, T>
|
||||
impl<T> From<Arc<FileDatabase<T, Ron>>> for DBWriteGuard<T>
|
||||
where
|
||||
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
|
||||
{
|
||||
fn from(v: &'a FileDatabase<T, Ron>) -> Self {
|
||||
fn from(v: Arc<FileDatabase<T, Ron>>) -> Self {
|
||||
DBWriteGuard(v)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> DBWriteGuard<'a, T>
|
||||
impl<T> DBWriteGuard<T>
|
||||
where
|
||||
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
|
||||
{
|
||||
|
@ -62,12 +64,3 @@ where
|
|||
(*self).0.borrow_data_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Drop for DBWriteGuard<'a, T>
|
||||
where
|
||||
T: Send + Sync + Clone + std::fmt::Debug + Serialize + DeserializeOwned,
|
||||
{
|
||||
fn drop(&mut self) {
|
||||
self.0.save().expect("Save succeed")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,8 +33,7 @@ pub fn soft_ban(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResu
|
|||
};
|
||||
let guild = msg.guild_id.ok_or(Error::from("Command is guild only"))?;
|
||||
|
||||
let db = ctx.data.read();
|
||||
let db = SoftBans::open(&*db);
|
||||
let db = SoftBans::open(&*ctx.data.read());
|
||||
let mut db = db.borrow_mut()?;
|
||||
let mut server_ban = db.get_mut(&guild).and_then(|v| match v {
|
||||
ServerSoftBans::Unimplemented => None,
|
||||
|
@ -95,8 +94,7 @@ pub fn soft_ban_init(ctx: &mut Context, msg: &Message, mut args: Args) -> Comman
|
|||
)));
|
||||
}
|
||||
// Check if we already set up
|
||||
let db = ctx.data.read();
|
||||
let db = SoftBans::open(&*db);
|
||||
let db = SoftBans::open(&*ctx.data.read());
|
||||
let mut db = db.borrow_mut()?;
|
||||
let server = db
|
||||
.get(&guild.id)
|
||||
|
|
|
@ -95,8 +95,7 @@ pub fn save(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
|||
let user: Option<User> = osu.user(UserID::Auto(user), |f| f)?;
|
||||
match user {
|
||||
Some(u) => {
|
||||
let db = ctx.data.read();
|
||||
let db = OsuSavedUsers::open(&db);
|
||||
let db = OsuSavedUsers::open(&*ctx.data.read());
|
||||
let mut db = db.borrow_mut()?;
|
||||
|
||||
db.insert(
|
||||
|
|
Loading…
Add table
Reference in a new issue