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
use std::{borrow::Borrow, collections::BTreeMap};
use crate::{DeviceIdBox, KeyNameBox, ServerNameBox, SigningKeyId, UserId};
pub type EntitySignatures<K> = BTreeMap<SigningKeyId<K>, String>;
#[derive(Clone, Debug, Default)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(transparent, crate = "serde")
)]
pub struct Signatures<E: Ord, K: ?Sized>(BTreeMap<E, EntitySignatures<K>>);
impl<E: Ord, K: ?Sized> Signatures<E, K> {
pub fn new() -> Self {
Self(BTreeMap::new())
}
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)
}
pub fn get<Q>(&self, entity: &Q) -> Option<&EntitySignatures<K>>
where
E: Borrow<Q>,
Q: Ord + ?Sized,
{
self.0.get(entity)
}
}
pub type ServerSignatures = Signatures<ServerNameBox, KeyNameBox>;
pub type DeviceSignatures = Signatures<UserId, DeviceIdBox>;