1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::{cmp, fmt, str};
use std::collections::HashSet;
#[derive(PartialEq, Eq, Clone, Debug)]
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.len() > 0);
Ok(Scope{ tokens: tokens.map(|r| r.to_string()).collect() })
}
}
impl fmt::Display for ParseScopeErr {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self {
&ParseScopeErr::InvalidCharacter(ref chr)
=> write!(fmt, "Encountered invalid character in scope: {}", chr)
}
}
}
impl fmt::Display for Scope {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let output = self.tokens.iter()
.map(|s| s.as_str())
.collect::<Vec<&str>>()
.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));
}
}