client-api: Add a borrowing iterator for SignedKeys

This commit is contained in:
Jonas Platte 2022-02-18 14:56:00 +01:00
parent b3d4b27b91
commit fa2e3662a4
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
2 changed files with 35 additions and 0 deletions

View File

@ -16,6 +16,8 @@ pub mod v3 {
use crate::PrivOwnedStr;
pub use super::iter::SignedKeysIter;
ruma_api! {
metadata: {
description: "Publishes cross-signing signatures for the user.",
@ -81,6 +83,11 @@ pub mod v3 {
) {
self.0.insert(cross_signing_key_id, cross_signing_keys.into_json());
}
/// Returns an iterator over the keys.
pub fn iter(&self) -> SignedKeysIter<'_> {
SignedKeysIter(self.0.iter())
}
}
/// A failure to process a signed key.
@ -105,3 +112,5 @@ pub mod v3 {
_Custom(PrivOwnedStr),
}
}
mod iter;

View File

@ -0,0 +1,26 @@
use std::collections::btree_map;
use serde_json::value::RawValue as RawJsonValue;
use super::v3::SignedKeys;
impl<'a> IntoIterator for &'a SignedKeys {
type Item = (&'a str, &'a RawJsonValue);
type IntoIter = SignedKeysIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
/// An iterator over signed key IDs and their associated data.
#[derive(Debug)]
pub struct SignedKeysIter<'a>(pub(super) btree_map::Iter<'a, Box<str>, Box<RawJsonValue>>);
impl<'a> Iterator for SignedKeysIter<'a> {
type Item = (&'a str, &'a RawJsonValue);
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(id, val)| (&**id, &**val))
}
}