From edb35cb4df463ad3dd804b78a29f44d53ba410ce Mon Sep 17 00:00:00 2001 From: Cheng JIANG Date: Sun, 10 May 2020 23:06:04 +0200 Subject: [PATCH] move _ count check outside of loop --- src/model/assertion.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/model/assertion.rs b/src/model/assertion.rs index 9afde39e..081cfe68 100644 --- a/src/model/assertion.rs +++ b/src/model/assertion.rs @@ -47,13 +47,13 @@ impl Assertion { pub fn build_role_links(&mut self, rm: Arc>) -> Result<()> { let count = self.value.chars().filter(|&c| c == '_').count(); + if count < 2 { + return Err(ModelError::P( + r#"the number of "_" in role definition should be at least 2"#.to_owned(), + ) + .into()); + } for rule in &self.policy { - if count < 2 { - return Err(ModelError::P( - r#"the number of "_" in role definition should be at least 2"#.to_owned(), - ) - .into()); - } if rule.len() < count { return Err(PolicyError::UnmatchPolicyDefinition(count, rule.len()).into()); } @@ -78,6 +78,12 @@ impl Assertion { d: EventData, ) -> Result<()> { let count = self.value.chars().filter(|&c| c == '_').count(); + if count < 2 { + return Err(ModelError::P( + r#"the number of "_" in role definition should be at least 2"#.to_owned(), + ) + .into()); + } if let Some((insert, rules)) = match d { EventData::AddPolicy(_, _, rule) => Some((true, vec![rule])), @@ -88,12 +94,6 @@ impl Assertion { _ => None, } { for rule in rules { - if count < 2 { - return Err(ModelError::P( - r#"the number of "_" in role definition should be at least 2"#.to_owned(), - ) - .into()); - } if rule.len() < count { return Err(PolicyError::UnmatchPolicyDefinition(count, rule.len()).into()); }