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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use std::{
cmp::Ordering,
convert::TryFrom,
fmt,
hash::{Hash, Hasher},
marker::PhantomData,
rc::Rc,
str::FromStr,
sync::Arc,
};
use crate::{crypto_algorithms::SigningKeyAlgorithm, DeviceId, KeyName};
#[repr(transparent)]
pub struct KeyId<A, K: ?Sized>(PhantomData<(A, K)>, str);
impl<A, K: ?Sized> KeyId<A, K> {
pub fn from_parts(algorithm: A, key_name: &K) -> Box<Self>
where
A: AsRef<str>,
K: AsRef<str>,
{
let algorithm = algorithm.as_ref();
let key_name = key_name.as_ref();
let mut res = String::with_capacity(algorithm.len() + 1 + key_name.len());
res.push_str(algorithm);
res.push(':');
res.push_str(key_name);
Self::from_owned(res.into())
}
pub fn algorithm(&self) -> A
where
A: FromStr,
{
A::from_str(&self.as_str()[..self.colon_idx()]).unwrap_or_else(|_| unreachable!())
}
pub fn key_name<'a>(&'a self) -> &'a K
where
&'a K: From<&'a str>,
{
self.as_str()[self.colon_idx() + 1..].into()
}
pub fn as_str(&self) -> &str {
&self.1
}
pub fn as_bytes(&self) -> &[u8] {
self.1.as_bytes()
}
fn from_borrowed(s: &str) -> &Self {
unsafe { std::mem::transmute(s) }
}
fn from_owned(s: Box<str>) -> Box<Self> {
unsafe { Box::from_raw(Box::into_raw(s) as _) }
}
fn into_owned(self: Box<Self>) -> Box<str> {
unsafe { Box::from_raw(Box::into_raw(self) as _) }
}
fn colon_idx(&self) -> usize {
self.as_str().find(':').unwrap()
}
}
pub type SigningKeyId<K> = KeyId<SigningKeyAlgorithm, K>;
pub type ServerSigningKeyId = SigningKeyId<KeyName>;
pub type DeviceSigningKeyId = SigningKeyId<DeviceId>;
impl<A, K: ?Sized> Clone for Box<KeyId<A, K>> {
fn clone(&self) -> Self {
(**self).to_owned()
}
}
impl<A, K: ?Sized> ToOwned for KeyId<A, K> {
type Owned = Box<KeyId<A, K>>;
fn to_owned(&self) -> Self::Owned {
Self::from_owned(self.1.into())
}
}
impl<A, K: ?Sized> From<&KeyId<A, K>> for Box<KeyId<A, K>> {
fn from(id: &KeyId<A, K>) -> Self {
id.to_owned()
}
}
impl<A, K: ?Sized> AsRef<str> for Box<KeyId<A, K>> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl<A, K: ?Sized> From<&KeyId<A, K>> for Rc<KeyId<A, K>> {
fn from(s: &KeyId<A, K>) -> Rc<KeyId<A, K>> {
let rc = Rc::<str>::from(s.as_str());
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const KeyId<A, K>) }
}
}
impl<A, K: ?Sized> From<&KeyId<A, K>> for Arc<KeyId<A, K>> {
fn from(s: &KeyId<A, K>) -> Arc<KeyId<A, K>> {
let arc = Arc::<str>::from(s.as_str());
unsafe { Arc::from_raw(Arc::into_raw(arc) as *const KeyId<A, K>) }
}
}
impl<A, K: ?Sized> PartialEq<KeyId<A, K>> for Box<KeyId<A, K>> {
fn eq(&self, other: &KeyId<A, K>) -> bool {
self.as_str() == other.as_str()
}
}
impl<A, K: ?Sized> PartialEq<&'_ KeyId<A, K>> for Box<KeyId<A, K>> {
fn eq(&self, other: &&KeyId<A, K>) -> bool {
self.as_str() == other.as_str()
}
}
impl<A, K: ?Sized> PartialEq<Box<KeyId<A, K>>> for KeyId<A, K> {
fn eq(&self, other: &Box<KeyId<A, K>>) -> bool {
self.as_str() == other.as_str()
}
}
impl<A, K: ?Sized> PartialEq<Box<KeyId<A, K>>> for &'_ KeyId<A, K> {
fn eq(&self, other: &Box<KeyId<A, K>>) -> bool {
self.as_str() == other.as_str()
}
}
impl<A, K: ?Sized> AsRef<str> for KeyId<A, K> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl<A, K: ?Sized> fmt::Display for KeyId<A, K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_str().fmt(f)
}
}
impl<A, K: ?Sized> fmt::Debug for KeyId<A, K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_str().fmt(f)
}
}
impl<A, K: ?Sized> PartialEq for KeyId<A, K> {
fn eq(&self, other: &Self) -> bool {
self.as_str() == other.as_str()
}
}
impl<A, K: ?Sized> Eq for KeyId<A, K> {}
impl<A, K: ?Sized> PartialOrd for KeyId<A, K> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
PartialOrd::partial_cmp(self.as_str(), other.as_str())
}
}
impl<A, K: ?Sized> Ord for KeyId<A, K> {
fn cmp(&self, other: &Self) -> Ordering {
Ord::cmp(self.as_str(), other.as_str())
}
}
impl<A, K: ?Sized> Hash for KeyId<A, K> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_str().hash(state);
}
}
#[cfg(feature = "serde")]
impl<A, K: ?Sized> serde::Serialize for KeyId<A, K> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.as_str())
}
}
impl<A, K: ?Sized> From<Box<KeyId<A, K>>> for String {
fn from(id: Box<KeyId<A, K>>) -> Self {
id.into_owned().into()
}
}
#[cfg(feature = "serde")]
impl<'de, A, K: ?Sized> serde::Deserialize<'de> for Box<KeyId<A, K>> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::de::Error;
let s = String::deserialize(deserializer)?;
match try_from(s) {
Ok(o) => Ok(o),
Err(e) => Err(D::Error::custom(e)),
}
}
}
fn try_from<S, A, K: ?Sized>(s: S) -> Result<Box<KeyId<A, K>>, crate::Error>
where
S: AsRef<str> + Into<Box<str>>,
{
ruma_identifiers_validation::key_id::validate(s.as_ref())?;
Ok(KeyId::from_owned(s.into()))
}
impl<'a, A, K: ?Sized> TryFrom<&'a str> for &'a KeyId<A, K> {
type Error = crate::Error;
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
(ruma_identifiers_validation::key_id::validate)(s)?;
Ok(KeyId::from_borrowed(s))
}
}
impl<A, K: ?Sized> FromStr for Box<KeyId<A, K>> {
type Err = crate::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
try_from(s)
}
}
impl<A, K: ?Sized> TryFrom<&str> for Box<KeyId<A, K>> {
type Error = crate::Error;
fn try_from(s: &str) -> Result<Self, Self::Error> {
try_from(s)
}
}
impl<A, K: ?Sized> TryFrom<String> for Box<KeyId<A, K>> {
type Error = crate::Error;
fn try_from(s: String) -> Result<Self, Self::Error> {
try_from(s)
}
}
#[rustfmt::skip]
partial_eq_string!(KeyId<A, K> [A, K]);