use crate::jsonld_store::{AsyncJsonLdStore, JsonLdStore};
use crate::store::Store;
use crate::AsyncStore;
use oxilite_core::{AsyncBackend, SyncBackend};
use oxilite_jsonld::{DocumentInput, JsonLdError, StoredDocument};
use oxilite_vc::{
bundled_contexts, credential_input, presentation_inputs, BundledContexts, CredentialFilter,
CredentialOptions, PresentationKeys,
};
type R<T> = Result<T, JsonLdError>;
pub struct CredentialStore<'a, B: SyncBackend> {
docs: JsonLdStore<'a, B, BundledContexts>,
options: CredentialOptions,
}
impl<B: SyncBackend + Send + Sync + 'static> Store<B> {
pub fn credentials(&self) -> R<CredentialStore<'_, B>> {
self.credentials_with(CredentialOptions::default())
}
pub fn credentials_with(&self, options: CredentialOptions) -> R<CredentialStore<'_, B>> {
Ok(CredentialStore {
docs: self.jsonld_with_loader(options.jsonld.clone(), bundled_contexts())?,
options,
})
}
}
impl<B: SyncBackend + Send + Sync + 'static> CredentialStore<'_, B> {
pub fn documents(&self) -> &JsonLdStore<'_, B, BundledContexts> {
&self.docs
}
pub fn put_credential(&self, json: &str) -> R<String> {
self.put(credential_input(json, None)?)
}
pub fn put_credential_with_key(&self, key: &str, json: &str) -> R<String> {
self.put(credential_input(json, Some(key.to_owned()))?)
}
fn put(&self, input: DocumentInput) -> R<String> {
Ok(self.docs.put_documents(vec![input])?.keys.remove(0))
}
pub fn put_presentation(&self, json: &str) -> R<PresentationKeys> {
let inputs = presentation_inputs(json, None, &self.options)?;
let mut keys = self.docs.put_documents(inputs)?.keys;
let key = keys.remove(0);
Ok(PresentationKeys {
key,
credentials: keys,
})
}
pub fn get_credential(&self, key: &str) -> R<Option<StoredDocument>> {
self.docs.get_document(key)
}
pub fn remove_credential(&self, key: &str) -> R<bool> {
self.docs.remove_document(key)
}
pub fn find_credentials(&self, filter: &CredentialFilter) -> R<Vec<StoredDocument>> {
self.docs.find_documents(filter)
}
}
pub struct AsyncCredentialStore<'a, B: AsyncBackend> {
docs: AsyncJsonLdStore<'a, B, BundledContexts>,
options: CredentialOptions,
}
impl<B: AsyncBackend> AsyncStore<B> {
pub async fn credentials(&self) -> R<AsyncCredentialStore<'_, B>> {
self.credentials_with(CredentialOptions::default()).await
}
pub async fn credentials_with(
&self,
options: CredentialOptions,
) -> R<AsyncCredentialStore<'_, B>> {
Ok(AsyncCredentialStore {
docs: self
.jsonld_with_loader(options.jsonld.clone(), bundled_contexts())
.await?,
options,
})
}
}
impl<B: AsyncBackend> AsyncCredentialStore<'_, B> {
pub fn documents(&self) -> &AsyncJsonLdStore<'_, B, BundledContexts> {
&self.docs
}
pub async fn put_credential(&self, json: &str) -> R<String> {
let input = credential_input(json, None)?;
Ok(self.docs.put_documents(vec![input]).await?.keys.remove(0))
}
pub async fn put_credential_with_key(&self, key: &str, json: &str) -> R<String> {
let input = credential_input(json, Some(key.to_owned()))?;
Ok(self.docs.put_documents(vec![input]).await?.keys.remove(0))
}
pub async fn put_presentation(&self, json: &str) -> R<PresentationKeys> {
let inputs = presentation_inputs(json, None, &self.options)?;
let mut keys = self.docs.put_documents(inputs).await?.keys;
let key = keys.remove(0);
Ok(PresentationKeys {
key,
credentials: keys,
})
}
pub async fn get_credential(&self, key: &str) -> R<Option<StoredDocument>> {
self.docs.get_document(key).await
}
pub async fn remove_credential(&self, key: &str) -> R<bool> {
self.docs.remove_document(key).await
}
pub async fn find_credentials(&self, filter: &CredentialFilter) -> R<Vec<StoredDocument>> {
self.docs.find_documents(filter).await
}
}