-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathid.rs
More file actions
285 lines (246 loc) · 7.17 KB
/
id.rs
File metadata and controls
285 lines (246 loc) · 7.17 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use crate::purl::PurlErr;
use hex::ToHex;
use ring::digest::Digest;
use sea_orm::{EntityTrait, QueryFilter, Select, SelectThree, SelectTwo, UpdateMany};
use sea_query::Condition;
use serde::{
Deserialize, Deserializer, Serialize, Serializer,
de::{Error, Visitor},
};
use serde_json::json;
use std::{
borrow::Cow,
fmt::{Display, Formatter},
str::FromStr,
};
use utoipa::{
PartialSchema, ToSchema,
openapi::{Object, RefOr, Schema, Type},
};
use uuid::Uuid;
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq)]
pub enum Id {
Uuid(Uuid),
Sha256(String),
Sha384(String),
Sha512(String),
}
impl Id {
/// Create a `Vec<Id>` from a fields of a document.
pub fn build_vec(sha256: String, sha384: Option<String>, sha512: Option<String>) -> Vec<Self> {
let mut result = vec![Id::Sha256(sha256)];
result.extend(sha384.map(Id::Sha384));
result.extend(sha512.map(Id::Sha512));
result
}
/// Get the value of the [`Id::Uuid`] variant, or return `None` if it is another variant.
pub fn try_as_uid(&self) -> Option<Uuid> {
match &self {
Self::Uuid(uuid) => Some(*uuid),
_ => None,
}
}
}
/// Create a filter for an ID
pub trait TryFilterForId {
/// Return a condition, filtering for the [`Id`]. Or an `Err(IdError::UnsupportedAlgorithm)` if the ID type is not supported.
fn try_filter(id: Id) -> Result<Condition, IdError>;
}
pub trait TrySelectForId: Sized {
fn try_filter(self, id: Id) -> Result<Self, IdError>;
}
impl<E> TrySelectForId for Select<E>
where
E: EntityTrait + TryFilterForId,
{
fn try_filter(self, id: Id) -> Result<Self, IdError> {
Ok(self.filter(E::try_filter(id)?))
}
}
impl<E, F> TrySelectForId for SelectTwo<E, F>
where
E: EntityTrait + TryFilterForId,
F: EntityTrait,
{
fn try_filter(self, id: Id) -> Result<Self, IdError> {
Ok(self.filter(E::try_filter(id)?))
}
}
impl<E, F, G> TrySelectForId for SelectThree<E, F, G>
where
E: EntityTrait + TryFilterForId,
F: EntityTrait,
G: EntityTrait,
{
fn try_filter(self, id: Id) -> Result<Self, IdError> {
Ok(self.filter(E::try_filter(id)?))
}
}
impl<E> TrySelectForId for UpdateMany<E>
where
E: EntityTrait + TryFilterForId,
{
fn try_filter(self, id: Id) -> Result<Self, IdError> {
Ok(self.filter(E::try_filter(id)?))
}
}
impl Id {
pub fn prefix(&self) -> &'static str {
match self {
Id::Sha256(_) => "sha256",
Id::Sha384(_) => "sha384",
Id::Sha512(_) => "sha512",
Id::Uuid(_) => "urn:uuid",
}
}
pub fn value(&self) -> String {
match self {
Id::Sha256(inner) => inner.clone(),
Id::Sha384(inner) => inner.clone(),
Id::Sha512(inner) => inner.clone(),
Id::Uuid(inner) => inner.simple().to_string(),
}
}
pub fn sha256(digest: &Digest) -> Self {
Self::from_digest(digest, Id::Sha256)
}
pub fn sha384(digest: &Digest) -> Self {
Self::from_digest(digest, Id::Sha384)
}
pub fn sha512(digest: &Digest) -> Self {
Self::from_digest(digest, Id::Sha512)
}
fn from_digest<F>(digest: &Digest, f: F) -> Self
where
F: FnOnce(String) -> Self,
{
f(digest.encode_hex())
}
/// Parse string as [`Uuid`] and return as [`Id::Uuid`] variant.
pub fn parse_uuid(uuid: impl AsRef<str>) -> Result<Self, IdError> {
Ok(Self::Uuid(
uuid.as_ref().parse().map_err(IdError::InvalidUuid)?,
))
}
}
impl ToSchema for Id {
fn name() -> Cow<'static, str> {
"Id".into()
}
}
impl PartialSchema for Id {
fn schema() -> RefOr<Schema> {
let mut obj = Object::with_type(Type::String);
obj.description = Some(
r#"Identifier to a document, prefixed with the ID type.
Either an internal ID of the document with the `urn:uuid:` scheme. Or using a digest, with the digest prefix. For example, `sha256:`."#
.to_string(),
);
obj.examples = vec![
json!("urn:uuid:018123ef-a791-40d8-b62a-f70a350245d4"),
json!("sha256:dc60aeb735c16a71b6fc56e84ddb8193e3a6d1ef0b7e958d77e78fc039a5d04e"),
];
RefOr::T(Schema::Object(obj))
}
}
impl Serialize for Id {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
impl<'de> Deserialize<'de> for Id {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(IdVisitor)
}
}
struct IdVisitor;
impl Visitor<'_> for IdVisitor {
type Value = Id;
fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
formatter.write_str("a hash key with a valid prefix")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
Id::from_str(v).map_err(|e| E::custom(e.to_string()))
}
}
impl Display for Id {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Id::Sha256(inner) => {
write!(f, "sha256:{inner}")
}
Id::Sha384(inner) => {
write!(f, "sha384:{inner}")
}
Id::Sha512(inner) => {
write!(f, "sha512:{inner}")
}
Id::Uuid(inner) => {
write!(f, "{}", inner.urn())
}
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum IdError {
#[error("Missing prefix")]
MissingPrefix,
#[error("Unsupported algorithm {0}")]
UnsupportedAlgorithm(String),
#[error(transparent)]
InvalidUuid(uuid::Error),
#[error(transparent)]
Purl(PurlErr),
}
impl FromStr for Id {
type Err = IdError;
fn from_str(key: &str) -> Result<Self, Self::Err> {
if let Some((prefix, value)) = key.split_once(':') {
match prefix {
"sha256" => Ok(Self::Sha256(value.to_string())),
"sha384" => Ok(Self::Sha384(value.to_string())),
"sha512" => Ok(Self::Sha512(value.to_string())),
"urn" => Ok(Self::Uuid(
Uuid::try_parse(key).map_err(IdError::InvalidUuid)?,
)),
_ => Err(Self::Err::UnsupportedAlgorithm(prefix.to_string())),
}
} else {
Err(Self::Err::MissingPrefix)
}
}
}
#[cfg(test)]
mod test {
use crate::id::Id;
use serde_json::json;
#[test]
fn deserialize() -> Result<(), anyhow::Error> {
let key: Id = serde_json::from_value(json!("sha256:123123"))?;
assert_eq!(key, Id::Sha256("123123".to_string()));
let _key: Id =
serde_json::from_value(json!("urn:uuid:2fd0d1b7-a908-4d63-9310-d57a7f77c6df"))?;
Ok(())
}
#[test]
fn serialize() -> Result<(), anyhow::Error> {
let key = Id::Sha256("123123".to_string());
let raw = serde_json::to_string(&key)?;
assert_eq!(raw, "\"sha256:123123\"");
Ok(())
}
#[test]
fn invalid() {
assert!(Id::parse_uuid("invalid").is_err());
}
}