Base: Start work on database backends
This commit is contained in:
parent
477a64e88b
commit
3ef0229518
14
Cargo.toml
Normal file
14
Cargo.toml
Normal 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
28
src/db/mod.rs
Normal 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
14
src/db/redis/mod.rs
Normal 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
14
src/db/sqlite/mod.rs
Normal 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
6
src/main.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
mod db;
|
||||
|
||||
fn main() {
|
||||
db::get_backend("redis");
|
||||
println!("{}", db::get_backend_names().join(", "));
|
||||
}
|
Loading…
Reference in New Issue
Block a user