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
use serde::Serialize;

use crate::{Document, DID, DIDURL};

use super::DIDVerificationMethod;

/// Reference to an arbitrary resource in a DID document.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(untagged)]
pub enum Resource {
    /// DID document.
    Document(Document),

    /// Verification method.
    VerificationMethod(DIDVerificationMethod),
}

impl Resource {
    pub fn as_verification_method(&self) -> Option<&DIDVerificationMethod> {
        match self {
            Self::VerificationMethod(m) => Some(m),
            _ => None,
        }
    }

    pub fn into_verification_method(self) -> Result<DIDVerificationMethod, Self> {
        match self {
            Self::VerificationMethod(m) => Ok(m),
            other => Err(other),
        }
    }
}

/// Reference to an arbitrary resource in a DID document.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResourceRef<'a> {
    /// DID document.
    Document(&'a Document),

    /// Verification method.
    VerificationMethod(&'a DIDVerificationMethod),
}

pub trait UsesResource {
    fn uses_resource(&self, base_id: &DID, id: &DIDURL) -> bool;
}

impl<T: UsesResource> UsesResource for Option<T> {
    fn uses_resource(&self, base_did: &DID, id: &DIDURL) -> bool {
        self.as_ref().is_some_and(|t| t.uses_resource(base_did, id))
    }
}

impl<T: UsesResource> UsesResource for Vec<T> {
    fn uses_resource(&self, base_did: &DID, id: &DIDURL) -> bool {
        self.iter().any(|t| t.uses_resource(base_did, id))
    }
}

/// Find a resource definition.
pub trait FindResource {
    fn find_resource(&self, base_did: &DID, id: &DIDURL) -> Option<ResourceRef>;
}

impl<T: FindResource> FindResource for Option<T> {
    fn find_resource(&self, base_did: &DID, id: &DIDURL) -> Option<ResourceRef> {
        self.as_ref().and_then(|t| t.find_resource(base_did, id))
    }
}

impl<T: FindResource> FindResource for Vec<T> {
    fn find_resource(&self, base_did: &DID, id: &DIDURL) -> Option<ResourceRef> {
        self.iter().find_map(|t| t.find_resource(base_did, id))
    }
}

pub trait ExtractResource {
    fn extract_resource(self, base_did: &DID, id: &DIDURL) -> Option<Resource>;
}

impl<T: ExtractResource> ExtractResource for Option<T> {
    fn extract_resource(self, base_did: &DID, id: &DIDURL) -> Option<Resource> {
        self.and_then(|t| t.extract_resource(base_did, id))
    }
}

impl<T: ExtractResource> ExtractResource for Vec<T> {
    fn extract_resource(self, base_did: &DID, id: &DIDURL) -> Option<Resource> {
        self.into_iter()
            .find_map(|t| t.extract_resource(base_did, id))
    }
}