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

View file

@ -183,16 +183,13 @@
//! } //! }
//! ``` //! ```
#![forbid(unsafe_code)] #![forbid(unsafe_code)]
#[cfg(feature = "minimal")]
pub mod defense; pub mod defense;
pub mod errors; pub mod errors;
#[cfg(feature = "full")]
pub mod master; pub mod master;
/// message datatypes to interact with [MCaptcha] actor /// message datatypes to interact with [MCaptcha] actor
#[cfg(feature = "full")] #[cfg(feature = "full")]
pub mod cache; pub mod cache;
#[cfg(feature = "minimal")]
pub mod mcaptcha; pub mod mcaptcha;
#[cfg(feature = "full")] #[cfg(feature = "full")]
pub mod pow; pub mod pow;
@ -204,9 +201,17 @@ mod utils;
#[cfg(feature = "full")] #[cfg(feature = "full")]
pub use crate::cache::hashcache::HashCache; pub use crate::cache::hashcache::HashCache;
#[cfg(feature = "minimal")] pub use crate::defense::{Defense, DefenseBuilder, LevelBuilder};
pub use defense::{Defense, DefenseBuilder, LevelBuilder}; pub use crate::master::{AddVisitorResult, CreateMCaptcha};
pub use crate::mcaptcha::{MCaptcha, MCaptchaBuilder};
#[cfg(feature = "full")] #[cfg(feature = "full")]
pub use master::embedded::counter::Counter; pub use master::embedded::counter::Counter;
#[cfg(feature = "minimal")] #[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/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
//! [Master] actor module that manages [MCaptcha] actors //! [Master] actor module that manages [MCaptcha] actors
#[cfg(feature = "full")]
use std::sync::mpsc::Receiver; use std::sync::mpsc::Receiver;
#[cfg(feature = "full")]
use actix::dev::*; use actix::dev::*;
#[cfg(feature = "full")]
use derive_builder::Builder; use derive_builder::Builder;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[cfg(feature = "full")]
use crate::errors::CaptchaResult; use crate::errors::CaptchaResult;
#[cfg(feature = "full")]
pub mod embedded; pub mod embedded;
#[allow( #[allow(
unused_variables, unused_variables,
@ -40,8 +45,10 @@ use crate::mcaptcha::*;
dead_code, dead_code,
unused_macros unused_macros
)] )]
#[cfg(feature = "full")]
pub mod redis; pub mod redis;
#[cfg(feature = "full")]
/// Describes actor handler trait impls that are required by a cache implementation /// Describes actor handler trait impls that are required by a cache implementation
pub trait Master: actix::Actor + actix::Handler<AddVisitor> + actix::Handler<AddSite> {} 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 /// Message to add visitor to an [MCaptcha] actor
#[derive(Message)] #[derive(Message)]
#[cfg(feature = "full")]
#[rtype(result = "Receiver<CaptchaResult<Option<AddVisitorResult>>>")] #[rtype(result = "Receiver<CaptchaResult<Option<AddVisitorResult>>>")]
pub struct AddVisitor(pub String); pub struct AddVisitor(pub String);
@ -64,16 +72,24 @@ pub struct AddVisitorResult {
/// Message to add an [Counter] actor to [Master] /// Message to add an [Counter] actor to [Master]
#[derive(Message, Builder)] #[derive(Message, Builder)]
#[rtype(result = "()")] #[rtype(result = "()")]
#[cfg(feature = "full")]
pub struct AddSite { pub struct AddSite {
pub id: String, pub id: String,
pub mcaptcha: MCaptcha, pub mcaptcha: MCaptcha,
} }
impl AddVisitorResult { impl AddVisitorResult {
fn new(m: &MCaptcha) -> Self { pub fn new(m: &MCaptcha) -> Self {
AddVisitorResult { AddVisitorResult {
duration: m.get_duration(), duration: m.get_duration(),
difficulty_factor: m.get_difficulty(), 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::Value;
use redis::{aio::Connection, cluster::ClusterConnection}; use redis::{aio::Connection, cluster::ClusterConnection};
use super::CreateMCaptcha;
use crate::errors::*; use crate::errors::*;
use crate::master::{AddSite, AddVisitor, AddVisitorResult}; use crate::master::{AddSite, AddVisitor, AddVisitorResult, CreateMCaptcha};
pub enum RedisConnection { pub enum RedisConnection {
Single(Rc<RefCell<Connection>>), Single(Rc<RefCell<Connection>>),

View file

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

View file

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