fix(claims): impl. of iter for OneOrArray

This commit is contained in:
cduvray 2023-07-06 07:47:22 +02:00 committed by cduvray
parent 70ce996275
commit b96c4f323a

View file

@ -19,6 +19,15 @@ pub enum OneOrArray<T> {
Array(Vec<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. /// Claims mentioned in the JWT specifications.
/// ///
/// https://www.rfc-editor.org/rfc/rfc7519#section-4.1 /// https://www.rfc-editor.org/rfc/rfc7519#section-4.1
@ -47,6 +56,23 @@ mod tests {
v: OneOrArray<String>, 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] #[test]
fn rfc_claims_aud() { fn rfc_claims_aud() {
let a: TestStruct = serde_json::from_str(r#"{"v":"a"}"#).unwrap(); let a: TestStruct = serde_json::from_str(r#"{"v":"a"}"#).unwrap();