use anyhow::{Result, anyhow}; use bytes::Bytes; use fjall::{Keyspace, PartitionCreateOptions, PartitionHandle, UserKey}; pub struct AppData { pub keyspace: Keyspace, } pub trait GuildPartition { fn get_guild_partition(&self) -> Result; } pub trait FetchSettings { fn get_from_partition(&self, key: UserKey) -> Result; } pub type Error = Box; pub type Context<'a> = poise::Context<'a, AppData, Error>; impl GuildPartition for Context<'_> { fn get_guild_partition(&self) -> Result { let id = self .guild_id() .ok_or(anyhow!("guild not found, user command not supported"))?; Ok(self .data() .keyspace .open_partition(format!("{id}").as_str(), PartitionCreateOptions::default())?) } } impl FetchSettings for PartitionHandle { fn get_from_partition<'str>(&self, key: UserKey) -> Result { let channel = self .get(key)? .ok_or(anyhow!("Could not find setting from partition"))?; let bytes = Bytes::from(channel.to_vec()); Ok(std::str::from_utf8(&bytes)?.to_string()) } }