ssi_dids_core/document/
resource.rs1use serde::Serialize;
2
3use crate::{Document, DID, DIDURL};
4
5use super::DIDVerificationMethod;
6
7#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
9#[serde(untagged)]
10pub enum Resource {
11 Document(Document),
13
14 VerificationMethod(DIDVerificationMethod),
16}
17
18impl Resource {
19 pub fn as_verification_method(&self) -> Option<&DIDVerificationMethod> {
20 match self {
21 Self::VerificationMethod(m) => Some(m),
22 _ => None,
23 }
24 }
25
26 pub fn into_verification_method(self) -> Result<DIDVerificationMethod, Self> {
27 match self {
28 Self::VerificationMethod(m) => Ok(m),
29 other => Err(other),
30 }
31 }
32}
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum ResourceRef<'a> {
37 Document(&'a Document),
39
40 VerificationMethod(&'a DIDVerificationMethod),
42}
43
44pub trait UsesResource {
45 fn uses_resource(&self, base_id: &DID, id: &DIDURL) -> bool;
46}
47
48impl<T: UsesResource> UsesResource for Option<T> {
49 fn uses_resource(&self, base_did: &DID, id: &DIDURL) -> bool {
50 self.as_ref().is_some_and(|t| t.uses_resource(base_did, id))
51 }
52}
53
54impl<T: UsesResource> UsesResource for Vec<T> {
55 fn uses_resource(&self, base_did: &DID, id: &DIDURL) -> bool {
56 self.iter().any(|t| t.uses_resource(base_did, id))
57 }
58}
59
60pub trait FindResource {
62 fn find_resource(&self, base_did: &DID, id: &DIDURL) -> Option<ResourceRef>;
63}
64
65impl<T: FindResource> FindResource for Option<T> {
66 fn find_resource(&self, base_did: &DID, id: &DIDURL) -> Option<ResourceRef> {
67 self.as_ref().and_then(|t| t.find_resource(base_did, id))
68 }
69}
70
71impl<T: FindResource> FindResource for Vec<T> {
72 fn find_resource(&self, base_did: &DID, id: &DIDURL) -> Option<ResourceRef> {
73 self.iter().find_map(|t| t.find_resource(base_did, id))
74 }
75}
76
77pub trait ExtractResource {
78 fn extract_resource(self, base_did: &DID, id: &DIDURL) -> Option<Resource>;
79}
80
81impl<T: ExtractResource> ExtractResource for Option<T> {
82 fn extract_resource(self, base_did: &DID, id: &DIDURL) -> Option<Resource> {
83 self.and_then(|t| t.extract_resource(base_did, id))
84 }
85}
86
87impl<T: ExtractResource> ExtractResource for Vec<T> {
88 fn extract_resource(self, base_did: &DID, id: &DIDURL) -> Option<Resource> {
89 self.into_iter()
90 .find_map(|t| t.extract_resource(base_did, id))
91 }
92}