ssi_status/impl/
any.rs

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use iref::Uri;
use ssi_claims_core::{DateTimeProvider, Eip712TypesLoaderProvider, ResolverProvider};
use ssi_json_ld::JsonLdLoaderProvider;
use ssi_jwk::JWKResolver;
use ssi_verification_methods::{AnyMethod, VerificationMethodResolver};

use crate::{
    bitstring_status_list::{
        self, BitstringStatusListCredential, BitstringStatusListEntry,
        BitstringStatusListEntrySetCredential,
    },
    token_status_list::{self, StatusListToken},
    EncodedStatusMap, FromBytes, FromBytesOptions, StatusMap, StatusMapEntry, StatusMapEntrySet,
    StatusSizeError,
};

pub enum AnyStatusMap {
    BitstringStatusList(BitstringStatusListCredential),
    TokenStatusList(StatusListToken),
}

impl AnyStatusMap {
    /// Returns the URL of the status list credential.
    pub fn credential_url(&self) -> Option<&Uri> {
        match self {
            Self::BitstringStatusList(cred) => cred.id.as_deref(),
            Self::TokenStatusList(_) => None,
        }
    }
}

#[derive(Debug, thiserror::Error)]
pub enum FromBytesError {
    #[error("unexpected media type `{0}`")]
    UnexpectedMediaType(String),

    #[error(transparent)]
    BitstringStatusList(bitstring_status_list::FromBytesError),

    #[error(transparent)]
    TokenStatusList(token_status_list::FromBytesError),
}

impl<V> FromBytes<V> for AnyStatusMap
where
    V: ResolverProvider + DateTimeProvider + JsonLdLoaderProvider + Eip712TypesLoaderProvider,
    V::Resolver: JWKResolver + VerificationMethodResolver<Method = AnyMethod>,
{
    type Error = FromBytesError;

    async fn from_bytes_with(
        bytes: &[u8],
        media_type: &str,
        verifier: &V,
        options: FromBytesOptions,
    ) -> Result<Self, Self::Error> {
        match media_type {
            "statuslist+jwt" | "statuslist+cwt" => {
                StatusListToken::from_bytes_with(bytes, media_type, verifier, options)
                    .await
                    .map(AnyStatusMap::TokenStatusList)
                    .map_err(FromBytesError::TokenStatusList)
            }
            "application/vc+ld+json+jwt"
            | "application/vc+ld+json+sd-jwt"
            | "application/vc+ld+json+cose"
            | "application/vc+ld+json" => {
                BitstringStatusListCredential::from_bytes_with(bytes, media_type, verifier, options)
                    .await
                    .map(AnyStatusMap::BitstringStatusList)
                    .map_err(FromBytesError::BitstringStatusList)
            }
            other => Err(FromBytesError::UnexpectedMediaType(other.to_owned())),
        }
    }
}

#[derive(Debug, thiserror::Error)]
pub enum DecodeError {
    #[error(transparent)]
    BitstringStatusList(#[from] bitstring_status_list::DecodeError),

    #[error(transparent)]
    TokenStatusList(#[from] token_status_list::DecodeError),
}

impl EncodedStatusMap for AnyStatusMap {
    type DecodeError = DecodeError;
    type Decoded = AnyDecodedStatusMap;

    fn decode(self) -> Result<Self::Decoded, Self::DecodeError> {
        match self {
            Self::BitstringStatusList(m) => m
                .decode()
                .map(AnyDecodedStatusMap::BitstringStatusList)
                .map_err(Into::into),
            Self::TokenStatusList(m) => m
                .decode()
                .map(AnyDecodedStatusMap::TokenStatusList)
                .map_err(Into::into),
        }
    }
}

#[derive(Clone)]
pub enum AnyDecodedStatusMap {
    BitstringStatusList(bitstring_status_list::StatusList),
    TokenStatusList(token_status_list::StatusList),
}

impl AnyDecodedStatusMap {
    pub fn iter(
        &self,
        status_size: Option<u8>,
    ) -> Result<AnyDecodedStatusMapIter, StatusSizeError> {
        match self {
            Self::BitstringStatusList(m) => Ok(AnyDecodedStatusMapIter::BitstringStatusList(
                m.iter(status_size.ok_or(StatusSizeError::Missing)?.try_into()?),
            )),
            Self::TokenStatusList(m) => Ok(AnyDecodedStatusMapIter::TokenStatusList(m.iter())),
        }
    }
}

impl StatusMap for AnyDecodedStatusMap {
    type Key = usize;
    type StatusSize = u8;
    type Status = u8;

    fn time_to_live(&self) -> Option<std::time::Duration> {
        match self {
            Self::BitstringStatusList(m) => m.time_to_live(),
            Self::TokenStatusList(m) => m.time_to_live(),
        }
    }

    fn get_by_key(
        &self,
        status_size: Option<u8>,
        key: Self::Key,
    ) -> Result<Option<Self::Status>, StatusSizeError> {
        match self {
            Self::BitstringStatusList(m) => {
                m.get_by_key(status_size.map(TryInto::try_into).transpose()?, key)
            }
            Self::TokenStatusList(m) => {
                m.get_by_key(status_size.map(TryInto::try_into).transpose()?, key)
            }
        }
    }
}

pub enum AnyDecodedStatusMapIter<'a> {
    BitstringStatusList(bitstring_status_list::BitStringIter<'a>),
    TokenStatusList(token_status_list::BitStringIter<'a>),
}

impl<'a> Iterator for AnyDecodedStatusMapIter<'a> {
    type Item = u8;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::BitstringStatusList(i) => i.next(),
            Self::TokenStatusList(i) => i.next(),
        }
    }
}

#[derive(Debug, thiserror::Error)]
pub enum EntrySetFromBytesError {
    #[error(transparent)]
    TokenStatusList(#[from] token_status_list::EntrySetFromBytesError),

    #[error(transparent)]
    BitstringStatusList(#[from] bitstring_status_list::FromBytesError),

    #[error("unexpected media type `{0}`")]
    UnexpectedMediaType(String),
}

pub enum AnyEntrySet {
    BitstringStatusList(BitstringStatusListEntrySetCredential),
    TokenStatusList(token_status_list::AnyStatusListEntrySet),
}

impl<V> FromBytes<V> for AnyEntrySet
where
    V: ResolverProvider + DateTimeProvider + JsonLdLoaderProvider + Eip712TypesLoaderProvider,
    V::Resolver: JWKResolver + VerificationMethodResolver<Method = AnyMethod>,
{
    type Error = EntrySetFromBytesError;

    async fn from_bytes_with(
        bytes: &[u8],
        media_type: &str,
        params: &V,
        options: FromBytesOptions,
    ) -> Result<Self, Self::Error> {
        match media_type {
            "application/json" | "application/jwt" | "application/cbor" | "application/cwt" => {
                token_status_list::AnyStatusListEntrySet::from_bytes_with(
                    bytes, media_type, params, options,
                )
                .await
                .map(Self::TokenStatusList)
                .map_err(Into::into)
            }
            "application/vc+ld+json+jwt"
            | "application/vc+ld+json+sd-jwt"
            | "application/vc+ld+json+cose"
            | "application/vc+ld+json" => {
                bitstring_status_list::BitstringStatusListEntrySetCredential::from_bytes_with(
                    bytes, media_type, params, options,
                )
                .await
                .map(Self::BitstringStatusList)
                .map_err(Into::into)
            }
            other => Err(EntrySetFromBytesError::UnexpectedMediaType(
                other.to_owned(),
            )),
        }
    }
}

impl StatusMapEntrySet for AnyEntrySet {
    type Entry<'a> = AnyStatusMapEntryRef<'a> where Self: 'a;

    fn get_entry(&self, purpose: crate::StatusPurpose<&str>) -> Option<Self::Entry<'_>> {
        match self {
            Self::BitstringStatusList(s) => s
                .get_entry(purpose)
                .map(AnyStatusMapEntryRef::BitstringStatusList),
            Self::TokenStatusList(s) => s
                .get_entry(purpose)
                .map(AnyStatusMapEntryRef::TokenStatusList),
        }
    }
}

pub enum AnyStatusMapEntryRef<'a> {
    BitstringStatusList(&'a BitstringStatusListEntry),
    TokenStatusList(token_status_list::AnyStatusListReference<'a>),
}

impl<'a> StatusMapEntry for AnyStatusMapEntryRef<'a> {
    type Key = usize;
    type StatusSize = u8;

    fn status_list_url(&self) -> &Uri {
        match self {
            Self::BitstringStatusList(e) => e.status_list_url(),
            Self::TokenStatusList(e) => e.status_list_url(),
        }
    }

    fn key(&self) -> Self::Key {
        match self {
            Self::BitstringStatusList(e) => e.key(),
            Self::TokenStatusList(e) => e.key(),
        }
    }

    fn status_size(&self) -> Option<Self::StatusSize> {
        match self {
            Self::BitstringStatusList(e) => e.status_size().map(Into::into),
            Self::TokenStatusList(e) => e.status_size().map(Into::into),
        }
    }
}