ssi_vc/
id.rs

1use iref::Uri;
2
3use crate::syntax::{
4    IdOr, IdentifiedObject, IdentifiedTypedObject, MaybeIdentifiedObject,
5    MaybeIdentifiedTypedObject,
6};
7
8/// Object that *may* contain an `id` property.
9///
10/// See: <https://www.w3.org/TR/vc-data-model-2.0/#identifiers>
11pub trait MaybeIdentified {
12    fn id(&self) -> Option<&Uri> {
13        None
14    }
15}
16
17impl MaybeIdentified for MaybeIdentifiedObject {
18    fn id(&self) -> Option<&Uri> {
19        self.id.as_deref()
20    }
21}
22
23impl MaybeIdentified for IdOr<MaybeIdentifiedObject> {
24    fn id(&self) -> Option<&Uri> {
25        match self {
26            Self::Id(id) => Some(id),
27            Self::NotId(o) => o.id(),
28        }
29    }
30}
31
32impl MaybeIdentified for MaybeIdentifiedTypedObject {
33    fn id(&self) -> Option<&Uri> {
34        self.id.as_deref()
35    }
36}
37
38/// Object that contain an `id` property.
39///
40/// See: <https://www.w3.org/TR/vc-data-model-2.0/#identifiers>
41pub trait Identified {
42    fn id(&self) -> &Uri;
43}
44
45impl<T: Identified> MaybeIdentified for T {
46    fn id(&self) -> Option<&Uri> {
47        Some(Identified::id(self))
48    }
49}
50
51impl Identified for std::convert::Infallible {
52    fn id(&self) -> &Uri {
53        unreachable!()
54    }
55}
56
57impl Identified for Uri {
58    fn id(&self) -> &Uri {
59        self
60    }
61}
62
63impl Identified for IdentifiedObject {
64    fn id(&self) -> &Uri {
65        &self.id
66    }
67}
68
69impl Identified for IdentifiedTypedObject {
70    fn id(&self) -> &Uri {
71        &self.id
72    }
73}