Skip to content

Commit

Permalink
Add persistance module #4 #1
Browse files Browse the repository at this point in the history
  • Loading branch information
NotNorom committed Feb 12, 2022
1 parent e8de485 commit 87f7cbb
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub(crate) tags: Vec<String>,
}

impl Default for ChannelConfiguration {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod commands;
mod configuration;
mod constants;
mod error;
mod persistance;
mod setup;
mod tasks;
mod utils;
Expand Down
96 changes: 96 additions & 0 deletions src/persistance.rs
Original file line number Diff line number Diff line change
@@ -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<ChannelConfiguration, RedisError> {
Err(RedisError::new_canceled())
}


impl RedisResponse for ChannelConfiguration {
fn from_value(value: RedisValue) -> Result<Self, RedisError> {
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::<NsfwMode>()?;

let tags = value
.get("tags")
.ok_or(RedisError::new(
RedisErrorKind::NotFound,
"missing key: tags",
))?
.clone()
.convert::<Vec<String>>()?;

Ok(Self {
active,
timeout,
random_timeout,
nsfw_mode,
tags,
})
}
}

impl RedisResponse for NsfwMode {
fn from_value(value: RedisValue) -> Result<Self, RedisError> {
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)
}
}

0 comments on commit 87f7cbb

Please sign in to comment.