feature gated a minimal dev module

This commit is contained in:
Aravinth Manivannan 2021-06-08 18:46:44 +05:30
parent c496b2c61b
commit 06df4d24d0
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
6 changed files with 35 additions and 16 deletions

View file

@ -18,6 +18,7 @@
//! Errors and Result module
use derive_more::{Display, Error};
#[cfg(feature = "full")]
use redis::RedisError;
/// Error datatype
@ -84,6 +85,7 @@ pub enum CaptchaError {
/// RedisError
#[display(fmt = "{}", _0)]
#[cfg(feature = "full")]
RedisError(RedisError),
/// Weird behaviour from mcaptcha redis module
@ -93,12 +95,14 @@ pub enum CaptchaError {
MCaptchaRedisModuleError,
}
#[cfg(feature = "full")]
impl From<RedisError> for CaptchaError {
fn from(e: RedisError) -> Self {
Self::RedisError(e)
}
}
#[cfg(feature = "full")]
impl From<actix::MailboxError> for CaptchaError {
fn from(_: actix::MailboxError) -> Self {
Self::MailboxError

View file

@ -183,16 +183,13 @@
//! }
//! ```
#![forbid(unsafe_code)]
#[cfg(feature = "minimal")]
pub mod defense;
pub mod errors;
#[cfg(feature = "full")]
pub mod master;
/// message datatypes to interact with [MCaptcha] actor
#[cfg(feature = "full")]
pub mod cache;
#[cfg(feature = "minimal")]
pub mod mcaptcha;
#[cfg(feature = "full")]
pub mod pow;
@ -204,9 +201,17 @@ mod utils;
#[cfg(feature = "full")]
pub use crate::cache::hashcache::HashCache;
#[cfg(feature = "minimal")]
pub use defense::{Defense, DefenseBuilder, LevelBuilder};
pub use crate::defense::{Defense, DefenseBuilder, LevelBuilder};
pub use crate::master::{AddVisitorResult, CreateMCaptcha};
pub use crate::mcaptcha::{MCaptcha, MCaptchaBuilder};
#[cfg(feature = "full")]
pub use master::embedded::counter::Counter;
#[cfg(feature = "minimal")]
pub use mcaptcha::{MCaptcha, MCaptchaBuilder};
pub mod dev {
pub use crate::defense;
pub use crate::defense::{Defense, DefenseBuilder, LevelBuilder};
pub use crate::master::{AddVisitorResult, CreateMCaptcha};
pub use crate::mcaptcha;
pub use crate::mcaptcha::{MCaptcha, MCaptchaBuilder};
}

View file

@ -16,14 +16,19 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//! [Master] actor module that manages [MCaptcha] actors
#[cfg(feature = "full")]
use std::sync::mpsc::Receiver;
#[cfg(feature = "full")]
use actix::dev::*;
#[cfg(feature = "full")]
use derive_builder::Builder;
use serde::{Deserialize, Serialize};
#[cfg(feature = "full")]
use crate::errors::CaptchaResult;
#[cfg(feature = "full")]
pub mod embedded;
#[allow(
unused_variables,
@ -40,8 +45,10 @@ use crate::mcaptcha::*;
dead_code,
unused_macros
)]
#[cfg(feature = "full")]
pub mod redis;
#[cfg(feature = "full")]
/// Describes actor handler trait impls that are required by a cache implementation
pub trait Master: actix::Actor + actix::Handler<AddVisitor> + actix::Handler<AddSite> {}
@ -49,6 +56,7 @@ pub trait Master: actix::Actor + actix::Handler<AddVisitor> + actix::Handler<Add
/// Message to add visitor to an [MCaptcha] actor
#[derive(Message)]
#[cfg(feature = "full")]
#[rtype(result = "Receiver<CaptchaResult<Option<AddVisitorResult>>>")]
pub struct AddVisitor(pub String);
@ -64,16 +72,24 @@ pub struct AddVisitorResult {
/// Message to add an [Counter] actor to [Master]
#[derive(Message, Builder)]
#[rtype(result = "()")]
#[cfg(feature = "full")]
pub struct AddSite {
pub id: String,
pub mcaptcha: MCaptcha,
}
impl AddVisitorResult {
fn new(m: &MCaptcha) -> Self {
pub fn new(m: &MCaptcha) -> Self {
AddVisitorResult {
duration: m.get_duration(),
difficulty_factor: m.get_difficulty(),
}
}
}
#[cfg(feature = "minimal")]
#[derive(Serialize, Deserialize)]
pub struct CreateMCaptcha {
pub levels: Vec<crate::defense::Level>,
pub duration: u64,
}

View file

@ -28,9 +28,8 @@ use redis::RedisResult;
use redis::Value;
use redis::{aio::Connection, cluster::ClusterConnection};
use super::CreateMCaptcha;
use crate::errors::*;
use crate::master::{AddSite, AddVisitor, AddVisitorResult};
use crate::master::{AddSite, AddVisitor, AddVisitorResult, CreateMCaptcha};
pub enum RedisConnection {
Single(Rc<RefCell<Connection>>),

View file

@ -34,7 +34,7 @@ use serde::{Deserialize, Serialize};
use crate::defense::Level;
use crate::errors::*;
use crate::master::AddVisitorResult;
use crate::master::{AddSite, AddVisitor, Master as MasterTrait};
use crate::master::{AddSite, AddVisitor, CreateMCaptcha, Master as MasterTrait};
use super::connection::RedisConnection;
@ -44,11 +44,6 @@ pub enum Redis {
Cluster(ClusterClient),
}
#[derive(Serialize, Deserialize)]
pub struct CreateMCaptcha {
pub levels: Vec<Level>,
pub duration: u64,
}
pub struct Master {
pub redis: Redis,
pub con: Rc<RedisConnection>,

View file

@ -78,7 +78,7 @@ pub struct MCaptcha {
duration: u64,
}
impl From<MCaptcha> for crate::master::redis::CreateMCaptcha {
impl From<MCaptcha> for crate::master::CreateMCaptcha {
fn from(m: MCaptcha) -> Self {
Self {
levels: m.defense.into(),