ssi_verification_methods_core/
reference.rs1use crate::{LinkedDataVerificationMethod, VerificationMethod};
2use educe::Educe;
3use iref::{Iri, IriBuf};
4use serde::{Deserialize, Serialize};
5
6#[derive(
8 Debug,
9 Clone,
10 PartialEq,
11 Eq,
12 PartialOrd,
13 Ord,
14 Hash,
15 Serialize,
16 Deserialize,
17 linked_data::Serialize,
18 linked_data::Deserialize,
19)]
20#[serde(untagged)]
21pub enum ReferenceOrOwned<M> {
22 Reference(#[ld(id)] IriBuf),
23 Owned(M),
24}
25
26impl<M> ReferenceOrOwned<M> {
27 pub fn id(&self) -> &Iri
28 where
29 M: VerificationMethod,
30 {
31 match self {
32 Self::Reference(r) => r,
33 Self::Owned(m) => m.id(),
34 }
35 }
36
37 pub fn borrowed(&self) -> ReferenceOrOwnedRef<M> {
38 match self {
39 Self::Reference(r) => ReferenceOrOwnedRef::Reference(r.as_iri()),
40 Self::Owned(m) => ReferenceOrOwnedRef::Owned(m),
41 }
42 }
43
44 pub fn map<N>(self, f: impl FnOnce(M) -> N) -> ReferenceOrOwned<N> {
45 match self {
46 Self::Reference(r) => ReferenceOrOwned::Reference(r),
47 Self::Owned(o) => ReferenceOrOwned::Owned(f(o)),
48 }
49 }
50
51 pub fn try_map<N, E>(
52 self,
53 f: impl FnOnce(M) -> Result<N, E>,
54 ) -> Result<ReferenceOrOwned<N>, E> {
55 match self {
56 Self::Reference(r) => Ok(ReferenceOrOwned::Reference(r)),
57 Self::Owned(o) => f(o).map(ReferenceOrOwned::Owned),
58 }
59 }
60
61 pub fn try_cast<N>(self) -> Result<ReferenceOrOwned<N>, M::Error>
62 where
63 M: TryInto<N>,
64 {
65 self.try_map(M::try_into)
66 }
67}
68
69impl<M> From<IriBuf> for ReferenceOrOwned<M> {
70 fn from(value: IriBuf) -> Self {
71 Self::Reference(value)
72 }
73}
74
75impl<'a, M> From<&'a Iri> for ReferenceOrOwned<M> {
76 fn from(value: &'a Iri) -> Self {
77 Self::Reference(value.to_owned())
78 }
79}
80
81impl<M: LinkedDataVerificationMethod> LinkedDataVerificationMethod for ReferenceOrOwned<M> {
82 fn quads(&self, quads: &mut Vec<rdf_types::Quad>) -> rdf_types::Object {
83 match self {
84 Self::Reference(r) => rdf_types::Object::Id(rdf_types::Id::Iri(r.clone())),
85 Self::Owned(m) => m.quads(quads),
86 }
87 }
88}
89
90#[derive(Educe, Debug, Serialize, linked_data::Serialize, linked_data::Deserialize)]
92#[educe(Clone, Copy)]
93#[serde(untagged)]
94pub enum ReferenceOrOwnedRef<'a, M> {
95 Reference(#[ld(id)] &'a Iri),
96 Owned(&'a M),
97}
98
99impl<'a, M: VerificationMethod> ReferenceOrOwnedRef<'a, M> {
100 pub fn cloned(&self) -> ReferenceOrOwned<M> {
101 match *self {
102 Self::Reference(iri) => ReferenceOrOwned::Reference(iri.to_owned()),
103 Self::Owned(m) => ReferenceOrOwned::Owned(m.clone()),
104 }
105 }
106
107 pub fn id(&self) -> &'a Iri {
108 match self {
109 Self::Reference(r) => r,
110 Self::Owned(m) => m.id(),
111 }
112 }
113}
114
115impl<'a, M> ReferenceOrOwnedRef<'a, M> {
116 pub fn map<N>(self, f: impl FnOnce(&'a M) -> &'a N) -> ReferenceOrOwnedRef<'a, N> {
117 match self {
118 Self::Reference(r) => ReferenceOrOwnedRef::Reference(r),
119 Self::Owned(o) => ReferenceOrOwnedRef::Owned(f(o)),
120 }
121 }
122
123 pub fn try_map<N, E>(
124 self,
125 f: impl FnOnce(&'a M) -> Result<&'a N, E>,
126 ) -> Result<ReferenceOrOwnedRef<'a, N>, E> {
127 match self {
128 Self::Reference(r) => Ok(ReferenceOrOwnedRef::Reference(r)),
129 Self::Owned(o) => f(o).map(ReferenceOrOwnedRef::Owned),
130 }
131 }
132
133 pub fn try_cast<N>(self) -> Result<ReferenceOrOwnedRef<'a, N>, <&'a M as TryInto<&'a N>>::Error>
134 where
135 &'a M: TryInto<&'a N>,
136 {
137 self.try_map(TryInto::try_into)
138 }
139}
140
141impl<'a, M> LinkedDataVerificationMethod for ReferenceOrOwnedRef<'a, M>
142where
143 &'a M: LinkedDataVerificationMethod,
144{
145 fn quads(&self, quads: &mut Vec<rdf_types::Quad>) -> rdf_types::Object {
146 match self {
147 Self::Reference(r) => rdf_types::Object::Id(rdf_types::Id::Iri(Iri::to_owned(*r))),
148 Self::Owned(m) => m.quads(quads),
149 }
150 }
151}