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)]
/// encoding formats for persistence
pub enum Format {
JSON,
Json,
}
impl Format {
#[inline]
pub fn parse_str<'a, T: Deserialize<'a>>(&self, data: &'a str) -> CacheResult<T> {
match self {
Format::JSON => Ok(serde_json::from_str(data)?),
Format::Json => Ok(serde_json::from_str(data)?),
}
}
#[inline]
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);
if bucket.key_type() == KeyType::Empty {
ctx.log_debug(&format!("Bucket doesn't exist: {}", &key_name));
return;
} else {
Bucket::decrement_runner(ctx, &bucket);
}
@ -113,7 +113,7 @@ impl Bucket {
fn decrement_runner(ctx: &Context, key: &RedisKeyWritable) {
match key.get_value::<Bucket>(&MCAPTCHA_BUCKET_TYPE) {
Ok(Some(bucket)) => {
ctx.log_debug(&format!("entering loop hashmap "));
ctx.log_debug("entering loop hashmap");
for (captcha, count) in bucket.decrement.drain() {
ctx.log_debug(&format!(
"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 {
0 => {
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
}
_ => 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 captcha = 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);
@ -131,7 +131,7 @@ pub mod type_methods {
let challenge = match encver {
0 => {
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() {
panic!(
"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 {
fn from(e: String) -> Self {
CacheError::Msg(e.to_string())
CacheError::Msg(e)
}
}

View file

@ -78,12 +78,15 @@ pub fn on_delete(ctx: &Context, event_type: NotifyEvent, event: &str, key_name:
if utils::is_bucket_timer(key_name) {
bucket::Bucket::on_delete(ctx, event_type, event, key_name);
return;
} else if utils::is_mcaptcha_safety(key_name) {
crate::safety::MCaptchaSafety::on_delete(ctx, event_type, event, key_name);
}
}
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub mod redis {
use super::*;
redis_module! {
name: "mcaptcha_cahce",
version: PKG_VERSION,
@ -99,6 +102,6 @@ redis_module! {
],
event_handlers: [
[@EXPIRED @EVICTED: on_delete],
//TODO add expire/evicted event for safety
]
}
}

View file

@ -105,13 +105,13 @@ impl MCaptcha {
/// get mcaptcha from redis key writable
#[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)?)
}
/// get mcaptcha from redis key
#[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)?)
}
@ -128,7 +128,7 @@ impl MCaptcha {
match stored_captcha.get_value::<Self>(&MCAPTCHA_MCAPTCHA_TYPE)? {
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 key_name = get_captcha_key(&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 mcaptcha = Self::new(mcaptcha)?;
@ -222,7 +222,7 @@ pub mod type_methods {
let mcaptcha = match encver {
0 => {
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() {
panic!(
"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<()> {
let safety_name = get_safety_name(mcaptcha_name);
let safety = ctx.open_key_writable(&safety_name);