use std::sync::Arc;
use ping_mls_store::{AsyncBlobStore, BlobFuture};
use crate::storage::Storage;
const BLOB_NAMESPACE: &str = "__mls";
const BLOB_KEY: &str = "snapshot";
pub fn storage_as_blob_store(storage: Arc<dyn Storage>) -> Arc<dyn AsyncBlobStore> {
Arc::new(StorageAsBlob { storage })
}
struct StorageAsBlob {
storage: Arc<dyn Storage>,
}
impl std::fmt::Debug for StorageAsBlob {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("StorageAsBlob")
}
}
impl AsyncBlobStore for StorageAsBlob {
fn read_blob(&self) -> BlobFuture<'_, std::result::Result<Option<Vec<u8>>, String>> {
let fut = self.storage.get(BLOB_NAMESPACE, BLOB_KEY);
Box::pin(async move {
match fut.await {
Ok(v) => Ok(v),
Err(e) => Err(format!("Storage.get(__mls, snapshot): {e}")),
}
})
}
fn write_blob(&self, bytes: Vec<u8>) -> BlobFuture<'_, std::result::Result<(), String>> {
let fut = self.storage.put(BLOB_NAMESPACE, BLOB_KEY, bytes);
Box::pin(async move {
match fut.await {
Ok(()) => Ok(()),
Err(e) => Err(format!("Storage.put(__mls, snapshot): {e}")),
}
})
}
}