/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/
pub mod dummy;
pub mod filesystem;
pub mod rocksdb;

use crate::{
    CONF,
    backends::{dummy::Dummy, filesystem::Filesystem, rocksdb::Rocksdb},
};
use anyhow::{Result, bail};

pub(crate) trait CacheStorage: Send + Sync + 'static {
    fn store(&self, key: String, value: &[u8]) -> Result<()>;
    fn read(&self, key: &str) -> Result<Option<Vec<u8>>>;
}

pub fn init_backend() -> Result<Box<dyn CacheStorage>> {
    Ok(match CONF.driver.as_str() {
        "filesystem" => Box::new(Filesystem::new(&CONF)),
        "rocksdb" => Box::new(Rocksdb::new(&CONF)?),
        "dummy" => Box::new(Dummy),
        _ => bail!("unknown driver"),
    })
}
