Updated dependencies

Updated formatting file
Changed the command line argument system
Updated error checking and matching logic to be easier to work with
Moved groups of functions to more correct modules
Updated the constants.rs file and added a help string for use with the new argument system
Disabled lua for now, the rust side of the bot takes priority
Changed the shutdown handler to be easier to work with
Added shutdown monitor task that gets started after the bot is ready, allowing a graceful shutdown from anywhere in the program, authentication is expected to be handled at the place of calling, not within the shutdown function
Added a basic database interface, SQLite will be implemented first as it is the simplest and most useful to the spiff team
This commit is contained in:
lever1209 2025-02-18 16:46:25 -04:00
commit 578918b22f
Signed by: lever1209
GPG key ID: AD770D25A908AFF4
18 changed files with 1564 additions and 644 deletions

60
src/databases/mod.rs Normal file
View file

@ -0,0 +1,60 @@
use std::{
fmt::Debug,
panic::Location,
sync::{Arc, OnceLock},
};
use serenity::all::UserId;
use crate::errors::DatabaseStoredError;
pub mod access;
pub mod mariadb;
pub mod postgres;
pub mod sqlite;
#[derive(Debug)]
pub enum DBKind {
Access,
SQLite,
MariaDB,
PostgreSQL,
}
static DATABASE_ACCESSOR: OnceLock<Arc<Box<dyn DatabaseAccessor>>> = OnceLock::new();
/// # Panics
/// This function will panic if used after the database has already been set
///
/// You should not worry about this as this function only ever gets called once at the start of the program and you are doing something wrong if you need to call this a second time
pub fn set_db(dba: Box<dyn DatabaseAccessor>) {
assert!(DATABASE_ACCESSOR.set(Arc::new(dba)).is_ok(), "attempted to set database accessor after init");
}
/// # Panics
/// This function will panic if used before the database has been set
///
/// You should not worry about this as this function only ever gets called after the database has been set and you are doing something wrong if you are calling this before the database is live
pub fn get_db() -> Arc<Box<dyn DatabaseAccessor>> {
#[allow(clippy::expect_used)]
DATABASE_ACCESSOR.get().expect("attempted to get database before init").clone()
}
/// This trait will provide a very high level interface to all supported databases
/// Its implementations should also sanitize all inputs regardless of what type
/// The implementation may be multithreaded/multiconnection, but for sqlite it is limited to a single thread/locked direct access to the connection via a mutex
/// If you need more performance use a different database type
pub trait DatabaseAccessor: Sync + Send {
// TODO make a db upgrade table
fn get_db_version(&self);
fn check_db_health(&self);
fn fix_db_health(&self);
fn is_dev(&self, user_id: UserId) -> bool;
fn set_dev(&self, user_id: UserId);
fn store_error(&self, err: &DatabaseStoredError);
}