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
use crate::{LinkedDataVerificationMethod, VerificationMethod};
use educe::Educe;
use iref::{Iri, IriBuf};
use serde::{Deserialize, Serialize};

/// Reference to a verification method.
#[derive(
    Debug,
    Clone,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Serialize,
    Deserialize,
    linked_data::Serialize,
    linked_data::Deserialize,
)]
#[serde(untagged)]
pub enum ReferenceOrOwned<M> {
    Reference(#[ld(id)] IriBuf),
    Owned(M),
}

impl<M> ReferenceOrOwned<M> {
    pub fn id(&self) -> &Iri
    where
        M: VerificationMethod,
    {
        match self {
            Self::Reference(r) => r,
            Self::Owned(m) => m.id(),
        }
    }

    pub fn borrowed(&self) -> ReferenceOrOwnedRef<M> {
        match self {
            Self::Reference(r) => ReferenceOrOwnedRef::Reference(r.as_iri()),
            Self::Owned(m) => ReferenceOrOwnedRef::Owned(m),
        }
    }

    pub fn map<N>(self, f: impl FnOnce(M) -> N) -> ReferenceOrOwned<N> {
        match self {
            Self::Reference(r) => ReferenceOrOwned::Reference(r),
            Self::Owned(o) => ReferenceOrOwned::Owned(f(o)),
        }
    }

    pub fn try_map<N, E>(
        self,
        f: impl FnOnce(M) -> Result<N, E>,
    ) -> Result<ReferenceOrOwned<N>, E> {
        match self {
            Self::Reference(r) => Ok(ReferenceOrOwned::Reference(r)),
            Self::Owned(o) => f(o).map(ReferenceOrOwned::Owned),
        }
    }

    pub fn try_cast<N>(self) -> Result<ReferenceOrOwned<N>, M::Error>
    where
        M: TryInto<N>,
    {
        self.try_map(M::try_into)
    }
}

impl<M> From<IriBuf> for ReferenceOrOwned<M> {
    fn from(value: IriBuf) -> Self {
        Self::Reference(value)
    }
}

impl<'a, M> From<&'a Iri> for ReferenceOrOwned<M> {
    fn from(value: &'a Iri) -> Self {
        Self::Reference(value.to_owned())
    }
}

impl<M: LinkedDataVerificationMethod> LinkedDataVerificationMethod for ReferenceOrOwned<M> {
    fn quads(&self, quads: &mut Vec<rdf_types::Quad>) -> rdf_types::Object {
        match self {
            Self::Reference(r) => rdf_types::Object::Id(rdf_types::Id::Iri(r.clone())),
            Self::Owned(m) => m.quads(quads),
        }
    }
}

/// Reference to a verification method.
#[derive(Educe, Debug, Serialize, linked_data::Serialize, linked_data::Deserialize)]
#[educe(Clone, Copy)]
#[serde(untagged)]
pub enum ReferenceOrOwnedRef<'a, M> {
    Reference(#[ld(id)] &'a Iri),
    Owned(&'a M),
}

impl<'a, M: VerificationMethod> ReferenceOrOwnedRef<'a, M> {
    pub fn cloned(&self) -> ReferenceOrOwned<M> {
        match *self {
            Self::Reference(iri) => ReferenceOrOwned::Reference(iri.to_owned()),
            Self::Owned(m) => ReferenceOrOwned::Owned(m.clone()),
        }
    }

    pub fn id(&self) -> &'a Iri {
        match self {
            Self::Reference(r) => r,
            Self::Owned(m) => m.id(),
        }
    }
}

impl<'a, M> ReferenceOrOwnedRef<'a, M> {
    pub fn map<N>(self, f: impl FnOnce(&'a M) -> &'a N) -> ReferenceOrOwnedRef<'a, N> {
        match self {
            Self::Reference(r) => ReferenceOrOwnedRef::Reference(r),
            Self::Owned(o) => ReferenceOrOwnedRef::Owned(f(o)),
        }
    }

    pub fn try_map<N, E>(
        self,
        f: impl FnOnce(&'a M) -> Result<&'a N, E>,
    ) -> Result<ReferenceOrOwnedRef<'a, N>, E> {
        match self {
            Self::Reference(r) => Ok(ReferenceOrOwnedRef::Reference(r)),
            Self::Owned(o) => f(o).map(ReferenceOrOwnedRef::Owned),
        }
    }

    pub fn try_cast<N>(self) -> Result<ReferenceOrOwnedRef<'a, N>, <&'a M as TryInto<&'a N>>::Error>
    where
        &'a M: TryInto<&'a N>,
    {
        self.try_map(TryInto::try_into)
    }
}

impl<'a, M> LinkedDataVerificationMethod for ReferenceOrOwnedRef<'a, M>
where
    &'a M: LinkedDataVerificationMethod,
{
    fn quads(&self, quads: &mut Vec<rdf_types::Quad>) -> rdf_types::Object {
        match self {
            Self::Reference(r) => rdf_types::Object::Id(rdf_types::Id::Iri(Iri::to_owned(*r))),
            Self::Owned(m) => m.quads(quads),
        }
    }
}