addressing clippy lints

This commit is contained in:
realaravinth 2021-06-09 20:01:41 +05:30
parent ed226a969c
commit fb0d75961c
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
6 changed files with 37 additions and 33 deletions

View file

@ -38,20 +38,21 @@ const REDIS_MCAPTCHA_BUCKET_TYPE_VERSION: i32 = 0;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
/// encoding formats for persistence /// encoding formats for persistence
pub enum Format { pub enum Format {
JSON, Json,
} }
impl Format { impl Format {
#[inline] #[inline]
pub fn parse_str<'a, T: Deserialize<'a>>(&self, data: &'a str) -> CacheResult<T> { pub fn parse_str<'a, T: Deserialize<'a>>(&self, data: &'a str) -> CacheResult<T> {
match self { match self {
Format::JSON => Ok(serde_json::from_str(data)?), Format::Json => Ok(serde_json::from_str(data)?),
} }
} }
#[inline] #[inline]
pub fn from_str<'a, T: Deserialize<'a>>(&self, data: &'a str) -> CacheResult<T> { pub fn from_str<'a, T: Deserialize<'a>>(&self, data: &'a str) -> CacheResult<T> {
Ok(self.parse_str(data)?) let res = self.parse_str(data)?;
Ok(res)
} }
} }
@ -81,7 +82,6 @@ impl Bucket {
let bucket = ctx.open_key_writable(&bucket_name); let bucket = ctx.open_key_writable(&bucket_name);
if bucket.key_type() == KeyType::Empty { if bucket.key_type() == KeyType::Empty {
ctx.log_debug(&format!("Bucket doesn't exist: {}", &key_name)); ctx.log_debug(&format!("Bucket doesn't exist: {}", &key_name));
return;
} else { } else {
Bucket::decrement_runner(ctx, &bucket); Bucket::decrement_runner(ctx, &bucket);
} }
@ -113,7 +113,7 @@ impl Bucket {
fn decrement_runner(ctx: &Context, key: &RedisKeyWritable) { fn decrement_runner(ctx: &Context, key: &RedisKeyWritable) {
match key.get_value::<Bucket>(&MCAPTCHA_BUCKET_TYPE) { match key.get_value::<Bucket>(&MCAPTCHA_BUCKET_TYPE) {
Ok(Some(bucket)) => { Ok(Some(bucket)) => {
ctx.log_debug(&format!("entering loop hashmap ")); ctx.log_debug("entering loop hashmap");
for (captcha, count) in bucket.decrement.drain() { for (captcha, count) in bucket.decrement.drain() {
ctx.log_debug(&format!( ctx.log_debug(&format!(
"reading captcha: {} with decr count {}", "reading captcha: {} with decr count {}",
@ -129,7 +129,7 @@ impl Bucket {
} }
} }
_ => { _ => {
ctx.log_debug(&format!("bucket not found, can't decrement")); ctx.log_debug("bucket not found, can't decrement");
} }
} }
} }
@ -275,7 +275,7 @@ pub mod type_methods {
let bucket = match encver { let bucket = match encver {
0 => { 0 => {
let data = raw::load_string(rdb); let data = raw::load_string(rdb);
let bucket: Bucket = Format::JSON.from_str(&data).unwrap(); let bucket: Bucket = Format::Json.from_str(&data).unwrap();
bucket bucket
} }
_ => panic!("Can't load bucket from old redis RDB, encver: {}", encver,), _ => panic!("Can't load bucket from old redis RDB, encver: {}", encver,),

View file

@ -55,7 +55,7 @@ impl Challenge {
let mut args = args.into_iter().skip(1); let mut args = args.into_iter().skip(1);
let captcha = args.next_string()?; let captcha = args.next_string()?;
let json = args.next_string()?; let json = args.next_string()?;
let add_challenge: AddChallenge = Format::JSON.from_str(&json)?; let add_challenge: AddChallenge = Format::Json.from_str(&json)?;
let challenge_name = get_challenge_name(&captcha, &add_challenge.challenge); let challenge_name = get_challenge_name(&captcha, &add_challenge.challenge);
@ -131,7 +131,7 @@ pub mod type_methods {
let challenge = match encver { let challenge = match encver {
0 => { 0 => {
let data = raw::load_string(rdb); let data = raw::load_string(rdb);
let challenge: Result<Challenge, CacheError> = Format::JSON.from_str(&data); let challenge: Result<Challenge, CacheError> = Format::Json.from_str(&data);
if challenge.is_err() { if challenge.is_err() {
panic!( panic!(
"Can't load Challenge from old redis RDB, error while serde {}, data received: {}", "Can't load Challenge from old redis RDB, error while serde {}, data received: {}",

View file

@ -44,7 +44,7 @@ impl CacheError {
impl From<String> for CacheError { impl From<String> for CacheError {
fn from(e: String) -> Self { fn from(e: String) -> Self {
CacheError::Msg(e.to_string()) CacheError::Msg(e)
} }
} }

View file

@ -78,27 +78,30 @@ pub fn on_delete(ctx: &Context, event_type: NotifyEvent, event: &str, key_name:
if utils::is_bucket_timer(key_name) { if utils::is_bucket_timer(key_name) {
bucket::Bucket::on_delete(ctx, event_type, event, key_name); bucket::Bucket::on_delete(ctx, event_type, event, key_name);
return;
} else if utils::is_mcaptcha_safety(key_name) { } else if utils::is_mcaptcha_safety(key_name) {
crate::safety::MCaptchaSafety::on_delete(ctx, event_type, event, key_name); crate::safety::MCaptchaSafety::on_delete(ctx, event_type, event, key_name);
} }
} }
redis_module! { #[allow(clippy::not_unsafe_ptr_arg_deref)]
name: "mcaptcha_cahce", pub mod redis {
version: PKG_VERSION, use super::*;
data_types: [MCAPTCHA_BUCKET_TYPE, MCAPTCHA_MCAPTCHA_TYPE, MCAPTCHA_SAFETY_TYPE, MCAPTCHA_CHALLENGE_TYPE],
commands: [ redis_module! {
["mcaptcha_cache.add_visitor", bucket::Bucket::counter_create, "write", 1, 1, 1], name: "mcaptcha_cahce",
["mcaptcha_cache.get", mcaptcha::MCaptcha::get_count, "readonly", 1, 1, 1], version: PKG_VERSION,
["mcaptcha_cache.add_captcha", mcaptcha::MCaptcha::add_captcha, "readonly", 1, 1, 1], data_types: [MCAPTCHA_BUCKET_TYPE, MCAPTCHA_MCAPTCHA_TYPE, MCAPTCHA_SAFETY_TYPE, MCAPTCHA_CHALLENGE_TYPE],
["mcaptcha_cache.delete_captcha", mcaptcha::MCaptcha::delete_captcha, "write", 1, 1, 1], commands: [
["mcaptcha_cache.captcha_exists", mcaptcha::MCaptcha::captcha_exists, "readonly", 1, 1, 1], ["mcaptcha_cache.add_visitor", bucket::Bucket::counter_create, "write", 1, 1, 1],
["mcaptcha_cache.add_challenge", challenge::Challenge::create_challenge, "write", 1, 1, 1], ["mcaptcha_cache.get", mcaptcha::MCaptcha::get_count, "readonly", 1, 1, 1],
["mcaptcha_cache.get_challenge", challenge::Challenge::get_challenge, "write", 1, 1, 1], ["mcaptcha_cache.add_captcha", mcaptcha::MCaptcha::add_captcha, "readonly", 1, 1, 1],
], ["mcaptcha_cache.delete_captcha", mcaptcha::MCaptcha::delete_captcha, "write", 1, 1, 1],
event_handlers: [ ["mcaptcha_cache.captcha_exists", mcaptcha::MCaptcha::captcha_exists, "readonly", 1, 1, 1],
[@EXPIRED @EVICTED: on_delete], ["mcaptcha_cache.add_challenge", challenge::Challenge::create_challenge, "write", 1, 1, 1],
//TODO add expire/evicted event for safety ["mcaptcha_cache.get_challenge", challenge::Challenge::get_challenge, "write", 1, 1, 1],
] ],
event_handlers: [
[@EXPIRED @EVICTED: on_delete],
]
}
} }

View file

@ -105,13 +105,13 @@ impl MCaptcha {
/// get mcaptcha from redis key writable /// get mcaptcha from redis key writable
#[inline] #[inline]
pub fn get_mut_mcaptcha<'a>(key: &'a RedisKeyWritable) -> CacheResult<Option<&'a mut Self>> { pub fn get_mut_mcaptcha(key: &RedisKeyWritable) -> CacheResult<Option<&mut Self>> {
Ok(key.get_value::<Self>(&MCAPTCHA_MCAPTCHA_TYPE)?) Ok(key.get_value::<Self>(&MCAPTCHA_MCAPTCHA_TYPE)?)
} }
/// get mcaptcha from redis key /// get mcaptcha from redis key
#[inline] #[inline]
pub fn get_mcaptcha<'a>(key: &'a RedisKey) -> CacheResult<Option<&'a Self>> { pub fn get_mcaptcha(key: &RedisKey) -> CacheResult<Option<&Self>> {
Ok(key.get_value::<Self>(&MCAPTCHA_MCAPTCHA_TYPE)?) Ok(key.get_value::<Self>(&MCAPTCHA_MCAPTCHA_TYPE)?)
} }
@ -128,7 +128,7 @@ impl MCaptcha {
match stored_captcha.get_value::<Self>(&MCAPTCHA_MCAPTCHA_TYPE)? { match stored_captcha.get_value::<Self>(&MCAPTCHA_MCAPTCHA_TYPE)? {
Some(val) => Ok(RedisValue::Integer(val.get_visitors().into())), Some(val) => Ok(RedisValue::Integer(val.get_visitors().into())),
None => return Err(CacheError::CaptchaNotFound.into()), None => Err(CacheError::CaptchaNotFound.into()),
} }
} }
@ -137,7 +137,7 @@ impl MCaptcha {
let mut args = args.into_iter().skip(1); let mut args = args.into_iter().skip(1);
let key_name = get_captcha_key(&args.next_string()?); let key_name = get_captcha_key(&args.next_string()?);
let json = args.next_string()?; let json = args.next_string()?;
let mcaptcha: CreateMCaptcha = Format::JSON.from_str(&json)?; let mcaptcha: CreateMCaptcha = Format::Json.from_str(&json)?;
let duration = mcaptcha.duration; let duration = mcaptcha.duration;
let mcaptcha = Self::new(mcaptcha)?; let mcaptcha = Self::new(mcaptcha)?;
@ -222,7 +222,7 @@ pub mod type_methods {
let mcaptcha = match encver { let mcaptcha = match encver {
0 => { 0 => {
let data = raw::load_string(rdb); let data = raw::load_string(rdb);
let mcaptcha: Result<MCaptcha, CacheError> = Format::JSON.from_str(&data); let mcaptcha: Result<MCaptcha, CacheError> = Format::Json.from_str(&data);
if mcaptcha.is_err() { if mcaptcha.is_err() {
panic!( panic!(
"Can't load mCaptcha from old redis RDB, error while serde {}, data received: {}", "Can't load mCaptcha from old redis RDB, error while serde {}, data received: {}",

View file

@ -94,6 +94,7 @@ impl MCaptchaSafety {
} }
} }
#[allow(clippy::new_ret_no_self)]
pub fn new(ctx: &Context, duration: u64, mcaptcha_name: &str) -> CacheResult<()> { pub fn new(ctx: &Context, duration: u64, mcaptcha_name: &str) -> CacheResult<()> {
let safety_name = get_safety_name(mcaptcha_name); let safety_name = get_safety_name(mcaptcha_name);
let safety = ctx.open_key_writable(&safety_name); let safety = ctx.open_key_writable(&safety_name);