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
use std::collections::BTreeMap; use crate::{DeviceIdBox, KeyNameBox, ServerNameBox, SigningKeyId, UserId}; /// Map of key identifier to signature values. pub type EntitySignatures<K> = BTreeMap<SigningKeyId<K>, String>; /// Map of all signatures, grouped by entity /// /// ``` /// # use ruma_identifiers::{server_name, KeyId, Signatures, SigningKeyAlgorithm}; /// let key_identifier = KeyId::from_parts(SigningKeyAlgorithm::Ed25519, "1"); /// let mut signatures = Signatures::new(); /// let server_name = server_name!("example.org"); /// let signature = "YbJva03ihSj5mPk+CHMJKUKlCXCPFXjXOK6VqBnN9nA2evksQcTGn6hwQfrgRHIDDXO2le49x7jnWJHMJrJoBQ"; /// signatures.insert(server_name, key_identifier, signature.into()); /// ``` #[derive(Clone, Debug, Default)] pub struct Signatures<E: Ord, K: ?Sized>(BTreeMap<E, EntitySignatures<K>>); impl<E: Ord, K: ?Sized> Signatures<E, K> { /// Creates an empty signature map. pub fn new() -> Self { Self(BTreeMap::new()) } /// Add a signature for the given server name and key identifier. /// /// If there was already one, it is returned. pub fn insert( &mut self, entity: E, key_identifier: SigningKeyId<K>, value: String, ) -> Option<String> { self.0.entry(entity).or_insert_with(Default::default).insert(key_identifier, value) } } /// Map of server signatures for an event, grouped by server. pub type ServerSignatures = Signatures<ServerNameBox, KeyNameBox>; /// Map of device signatures for an event, grouped by user. pub type DeviceSignatures = Signatures<UserId, DeviceIdBox>;