mirror of
https://github.com/TECHNOFAB11/jwt-authorizer.git
synced 2025-12-11 23:50:07 +01:00
fix(claims): impl. of iter for OneOrArray
This commit is contained in:
parent
70ce996275
commit
b96c4f323a
1 changed files with 26 additions and 0 deletions
|
|
@ -19,6 +19,15 @@ pub enum OneOrArray<T> {
|
|||
Array(Vec<T>),
|
||||
}
|
||||
|
||||
impl<T> OneOrArray<T> {
|
||||
pub fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = &'a T> + 'a> {
|
||||
match self {
|
||||
OneOrArray::One(v) => Box::new(std::iter::once(v)),
|
||||
OneOrArray::Array(vector) => Box::new(vector.iter()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Claims mentioned in the JWT specifications.
|
||||
///
|
||||
/// https://www.rfc-editor.org/rfc/rfc7519#section-4.1
|
||||
|
|
@ -47,6 +56,23 @@ mod tests {
|
|||
v: OneOrArray<String>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_or_array_iter() {
|
||||
let o = OneOrArray::One("aaa".to_owned());
|
||||
let mut i = o.iter();
|
||||
assert_eq!(Some(&"aaa".to_owned()), i.next());
|
||||
|
||||
let a = OneOrArray::Array(vec!["aaa".to_owned()]);
|
||||
let mut i = a.iter();
|
||||
assert_eq!(Some(&"aaa".to_owned()), i.next());
|
||||
|
||||
let a = OneOrArray::Array(vec!["aaa".to_owned(), "bbb".to_owned()]);
|
||||
let mut i = a.iter();
|
||||
assert_eq!(Some(&"aaa".to_owned()), i.next());
|
||||
assert_eq!(Some(&"bbb".to_owned()), i.next());
|
||||
assert_eq!(None, i.next());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rfc_claims_aud() {
|
||||
let a: TestStruct = serde_json::from_str(r#"{"v":"a"}"#).unwrap();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue