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
use std::borrow::Cow;

use serde::{Deserialize, Serialize};

use crate::DID;

use super::{relative::RelativeDIDURLBuf, DIDURLBuf, RelativeDIDURL, DIDURL};

pub enum DIDURLReference<'a> {
    Absolute(&'a DIDURL),
    Relative(&'a RelativeDIDURL),
}

impl<'a> DIDURLReference<'a> {
    pub fn resolve(&self, base_id: &DID) -> Cow<DIDURL> {
        match self {
            Self::Absolute(a) => Cow::Borrowed(a),
            Self::Relative(r) => Cow::Owned(r.resolve(base_id)),
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(untagged)]
pub enum DIDURLReferenceBuf {
    Absolute(DIDURLBuf),
    Relative(RelativeDIDURLBuf),
}

impl DIDURLReferenceBuf {
    pub fn as_did_reference(&self) -> DIDURLReference {
        match self {
            Self::Absolute(a) => DIDURLReference::Absolute(a),
            Self::Relative(r) => DIDURLReference::Relative(r),
        }
    }

    pub fn resolve(&self, base_id: &DID) -> Cow<DIDURL> {
        match self {
            Self::Absolute(a) => Cow::Borrowed(a.as_did_url()),
            Self::Relative(r) => Cow::Owned(r.resolve(base_id)),
        }
    }
}

impl From<DIDURLBuf> for DIDURLReferenceBuf {
    fn from(value: DIDURLBuf) -> Self {
        Self::Absolute(value)
    }
}

impl From<RelativeDIDURLBuf> for DIDURLReferenceBuf {
    fn from(value: RelativeDIDURLBuf) -> Self {
        Self::Relative(value)
    }
}