diff --git a/src/configuration.rs b/src/configuration.rs index 4681c93..f2a9f04 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -89,15 +89,15 @@ impl GuildConfiguration { #[derive(Debug, Clone)] pub struct ChannelConfiguration { /// True if the posting loop is running - active: bool, + pub(crate) active: bool, /// Timeout in minutes - timeout: u64, + pub(crate) timeout: u64, /// True if timeout should be interpreted as a maximum timeout - random_timeout: bool, + pub(crate) random_timeout: bool, /// If the query should return sfw or nsfw posts - nsfw_mode: NsfwMode, + pub(crate) nsfw_mode: NsfwMode, /// The tags to search for - tags: Vec, + pub(crate) tags: Vec, } impl Default for ChannelConfiguration { diff --git a/src/main.rs b/src/main.rs index b6e49d3..c3b9dec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod commands; mod configuration; mod constants; mod error; +mod persistance; mod setup; mod tasks; mod utils; diff --git a/src/persistance.rs b/src/persistance.rs new file mode 100644 index 0000000..dc35c24 --- /dev/null +++ b/src/persistance.rs @@ -0,0 +1,96 @@ +use std::str::FromStr; + +use fred::{ + self, + error::RedisErrorKind, + prelude::RedisError, + types::{RedisResponse, RedisValue}, +}; +use poise::serenity_prelude::{GuildId, ChannelId}; + +use crate::{configuration::ChannelConfiguration, utils::NsfwMode}; + +pub async fn get_channel_config(guild: GuildId, channel: ChannelId) -> Result { + Err(RedisError::new_canceled()) +} + + +impl RedisResponse for ChannelConfiguration { + fn from_value(value: RedisValue) -> Result { + let value = value.into_map()?; + + let active = value + .get("active") + .ok_or(RedisError::new( + RedisErrorKind::NotFound, + "missing key: active", + ))? + .as_bool() + .ok_or(RedisError::new( + RedisErrorKind::Parse, + "invalid value for key: active", + ))?; + + let timeout = value + .get("timeout") + .ok_or(RedisError::new( + RedisErrorKind::NotFound, + "missing key: timeout", + ))? + .as_u64() + .ok_or(RedisError::new( + RedisErrorKind::Parse, + "invalid value for key: timeout", + ))?; + + let random_timeout = value + .get("random_timeout") + .ok_or(RedisError::new( + RedisErrorKind::NotFound, + "missing key: random_timeout", + ))? + .as_bool() + .ok_or(RedisError::new( + RedisErrorKind::Parse, + "invalid value for key: random_timeout", + ))?; + + let nsfw_mode = value + .get("nsfw_mode") + .ok_or(RedisError::new( + RedisErrorKind::NotFound, + "missing key: nsfw_mode", + ))? + .clone() + .convert::()?; + + let tags = value + .get("tags") + .ok_or(RedisError::new( + RedisErrorKind::NotFound, + "missing key: tags", + ))? + .clone() + .convert::>()?; + + Ok(Self { + active, + timeout, + random_timeout, + nsfw_mode, + tags, + }) + } +} + +impl RedisResponse for NsfwMode { + fn from_value(value: RedisValue) -> Result { + let value = value.as_str().ok_or(RedisError::new( + RedisErrorKind::NotFound, + "Nsfw mode is not a string", + ))?; + let mode = NsfwMode::from_str(&value) + .map_err(|e| RedisError::new(RedisErrorKind::Parse, e.to_string()))?; + Ok(mode) + } +}