use std::{cmp, fmt, str};
use std::collections::HashSet;
#[derive(Clone, PartialEq, Eq)]
pub struct Scope {
tokens: HashSet<String>,
}
impl Scope {
fn invalid_scope_char(ch: char) -> bool {
match ch {
'\x21' => false,
ch if ch >= '\x23' && ch <= '\x5b' => false,
ch if ch >= '\x5d' && ch <= '\x7e' => false,
' ' => false, _ => true,
}
}
pub fn priviledged_to(&self, rhs: &Scope) -> bool {
rhs <= self
}
pub fn allow_access(&self, rhs: &Scope) -> bool {
self <= rhs
}
}
#[derive(Debug)]
pub enum ParseScopeErr {
InvalidCharacter(char),
}
impl str::FromStr for Scope {
type Err = ParseScopeErr;
fn from_str(string: &str) -> Result<Scope, ParseScopeErr> {
if let Some(ch) = string.chars().find(|&ch| Scope::invalid_scope_char(ch)) {
return Err(ParseScopeErr::InvalidCharacter(ch))
}
let tokens = string.split(' ').filter(|s| !s.is_empty());
Ok(Scope{ tokens: tokens.map(str::to_string).collect() })
}
}
impl fmt::Display for ParseScopeErr {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self {
ParseScopeErr::InvalidCharacter(chr)
=> write!(fmt, "Encountered invalid character in scope: {}", chr)
}
}
}
impl fmt::Debug for Scope {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_tuple("Scope")
.field(&self.tokens)
.finish()
}
}
impl fmt::Display for Scope {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let output = self.tokens.iter()
.map(String::as_str)
.collect::<Vec<_>>()
.join(" ");
fmt.write_str(&output)
}
}
impl cmp::PartialOrd for Scope {
fn partial_cmp(&self, rhs: &Self) -> Option<cmp::Ordering> {
let intersect_count = self.tokens.intersection(&rhs.tokens).count();
if intersect_count == self.tokens.len() && intersect_count == rhs.tokens.len() {
Some(cmp::Ordering::Equal)
} else if intersect_count == self.tokens.len() {
Some(cmp::Ordering::Less)
} else if intersect_count == rhs.tokens.len() {
Some(cmp::Ordering::Greater)
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parsing() {
let scope = Scope { tokens: ["default", "password", "email"].iter().map(|s| s.to_string()).collect() };
let formatted = scope.to_string();
let parsed = formatted.parse::<Scope>().unwrap();
assert_eq!(scope, parsed);
let from_string = "email password default".parse::<Scope>().unwrap();
assert_eq!(scope, from_string);
}
#[test]
fn test_compare() {
let scope_base = "cap1 cap2".parse::<Scope>().unwrap();
let scope_less = "cap1".parse::<Scope>().unwrap();
let scope_uncmp = "cap1 cap3".parse::<Scope>().unwrap();
assert_eq!(scope_base.partial_cmp(&scope_less), Some(cmp::Ordering::Greater));
assert_eq!(scope_less.partial_cmp(&scope_base), Some(cmp::Ordering::Less));
assert_eq!(scope_base.partial_cmp(&scope_uncmp), None);
assert_eq!(scope_uncmp.partial_cmp(&scope_base), None);
assert_eq!(scope_base.partial_cmp(&scope_base), Some(cmp::Ordering::Equal));
assert!(scope_base.priviledged_to(&scope_less));
assert!(scope_base.priviledged_to(&scope_base));
assert!(scope_less.allow_access(&scope_base));
assert!(scope_base.allow_access(&scope_base));
assert!(!scope_less.priviledged_to(&scope_base));
assert!(!scope_base.allow_access(&scope_less));
assert!(!scope_less.priviledged_to(&scope_uncmp));
assert!(!scope_base.priviledged_to(&scope_uncmp));
assert!(!scope_uncmp.allow_access(&scope_less));
assert!(!scope_uncmp.allow_access(&scope_base));
}
}