Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove explain relevant code when feature has been disabled #164

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
remove explain relevant code when feature has been disabled
  • Loading branch information
Cheng JIANG committed May 21, 2020
commit 588f0c566d7adb26596a3e9155ed05072772dac0
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "casbin"
version = "0.9.2"
version = "0.9.3"
authors = ["Joey <joey.xf@gmail.com>", "Cheng JIANG <jiang.cheng@vip.163.com>"]
edition = "2018"
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Add this package to `Cargo.toml` of your project. (Check https://crates.io/crate

```toml
[dependencies]
casbin = { version = "0.8.5", default-features = false, features = ["runtime-async-std", "logging"] }
casbin = { version = "0.9.3", default-features = false, features = ["runtime-async-std", "logging"] }
async-std = { version = "1.5.0", features = ["attributes"] }
env_logger = "0.7.1"
```
Expand Down
45 changes: 29 additions & 16 deletions src/effector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub trait Effector: Send + Sync {

pub trait EffectorStream: Send + Sync {
fn next(&self) -> bool;
#[cfg(feature = "explain")]
fn expl(&self) -> Option<Vec<usize>>;
fn push_effect(&mut self, eft: EffectKind) -> bool;
}
Expand All @@ -22,6 +23,7 @@ pub struct DefaultEffectStream {
expr: String,
idx: usize,
cap: usize,
#[cfg(feature = "explain")]
expl: Vec<usize>,
}

Expand All @@ -46,6 +48,7 @@ impl Effector for DefaultEffector {
expr: expr.to_owned(),
cap,
idx: 0,
#[cfg(feature = "explain")]
expl: Vec::with_capacity(10),
})
}
Expand All @@ -58,13 +61,10 @@ impl EffectorStream for DefaultEffectStream {
self.res
}

#[cfg(feature = "explain")]
#[inline]
fn expl(&self) -> Option<Vec<usize>> {
assert!(self.done);
#[cfg(feature = "logging")]
#[cfg(feature = "explain")]
return None;

if self.expl.is_empty() {
None
} else {
Expand All @@ -73,39 +73,49 @@ impl EffectorStream for DefaultEffectStream {
}

fn push_effect(&mut self, eft: EffectKind) -> bool {
let has_policy = self.cap > 1;

if self.expr == "some(where (p_eft == allow))" {
if eft == EffectKind::Allow {
self.done = true;
self.res = true;

if has_policy {
self.expl.push(self.idx);
#[cfg(feature = "explain")]
{
if self.cap > 1 {
self.expl.push(self.idx);
}
}
}
} else if self.expr == "some(where (p_eft == allow)) && !some(where (p_eft == deny))" {
if eft == EffectKind::Allow {
self.res = true;

if has_policy {
self.expl.push(self.idx);
#[cfg(feature = "explain")]
{
if self.cap > 1 {
self.expl.push(self.idx);
}
}
} else if eft == EffectKind::Deny {
self.done = true;
self.res = false;

if has_policy {
self.expl.push(self.idx);
#[cfg(feature = "explain")]
{
if self.cap > 1 {
self.expl.push(self.idx);
}
}
}
} else if self.expr == "!some(where (p_eft == deny))" {
if eft == EffectKind::Deny {
self.done = true;
self.res = false;

if has_policy {
self.expl.push(self.idx);
#[cfg(feature = "explain")]
{
if self.cap > 1 {
self.expl.push(self.idx);
}
}
}
} else if self.expr == "priority(p_eft) || deny" && eft != EffectKind::Indeterminate {
Expand All @@ -116,8 +126,11 @@ impl EffectorStream for DefaultEffectStream {
}
self.done = true;

if has_policy {
self.expl.push(self.idx);
#[cfg(feature = "explain")]
{
if self.cap > 1 {
self.expl.push(self.idx);
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/enforcer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,16 @@ impl Enforcer {
};
eft_stream.push_effect(eft);
}
Ok((eft_stream.next(), eft_stream.expl()))
Ok((eft_stream.next(), {
#[cfg(feature = "explain")]
{
eft_stream.expl()
}
#[cfg(not(feature = "explain"))]
{
None
}
}))
}
}

Expand Down
1 change: 1 addition & 0 deletions src/logger/default_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl Logger for DefaultLogger {
info!("{}", d);
}

#[cfg(feature = "explain")]
fn print_expl_log(&self, rules: Vec<&Vec<String>>) {
if !self.is_enabled() {
return;
Expand Down
1 change: 1 addition & 0 deletions src/logger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub trait Logger: Send + Sync {
fn is_enabled(&self) -> bool;
fn print_enforce_log(&self, rvals: Vec<String>, authorized: bool, cached: bool);
fn print_mgmt_log(&self, d: &EventData);
#[cfg(feature = "explain")]
fn print_expl_log(&self, rules: Vec<&Vec<String>>);
fn print_status_log(&self, enabled: bool);
}