Base: Start work on database backends

This commit is contained in:
ProtoByter 2022-09-06 09:23:14 +01:00
parent 477a64e88b
commit 3ef0229518
5 changed files with 76 additions and 0 deletions

14
Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
name = "ogs_chat_node"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
redis = { version = "0.21.6", optional = true }
sqlite = { version = "0.27.0", optional = true }
[features]
redis_db = ["dep:redis"]
sqlite_db = ["dep:sqlite"]

28
src/db/mod.rs Normal file
View File

@ -0,0 +1,28 @@
mod sqlite;
mod redis;
pub trait DbBackend {
}
pub fn get_backend_names() -> Vec<&'static str> {
vec![
#[cfg(feature = "sqlite_db")]
"sqlite",
#[cfg(feature = "redis_db")]
"redis"
]
}
pub fn get_backend(db_type: &str) -> Box<dyn DbBackend> {
#[cfg(not(any(feature = "sqlite_db", feature = "redis_db")))]
compile_error!("No database backend selected");
return match db_type {
#[cfg(feature = "sqlite_db")]
"sqlite" => Box::new(sqlite::SqliteBackend::new()),
#[cfg(feature = "redis_db")]
"redis" => Box::new(redis::RedisBackend::new()),
_ => panic!("Unknown database type"),
};
}

14
src/db/redis/mod.rs Normal file
View File

@ -0,0 +1,14 @@
use crate::db::DbBackend;
pub(crate) struct RedisBackend {
}
impl RedisBackend {
pub(crate) fn new() -> Self {
Self {}
}
}
impl DbBackend for RedisBackend {
}

14
src/db/sqlite/mod.rs Normal file
View File

@ -0,0 +1,14 @@
use crate::db::DbBackend;
pub(crate) struct SqliteBackend {
}
impl SqliteBackend {
pub(crate) fn new() -> Self {
Self {}
}
}
impl DbBackend for SqliteBackend {
}

6
src/main.rs Normal file
View File

@ -0,0 +1,6 @@
mod db;
fn main() {
db::get_backend("redis");
println!("{}", db::get_backend_names().join(", "));
}