use std::fmt;
use std::future::Future;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum BlobError {
Backend(String),
Unsupported(String),
}
impl fmt::Display for BlobError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BlobError::Backend(m) => write!(f, "blob backend: {m}"),
BlobError::Unsupported(m) => write!(f, "blob backend does not support: {m}"),
}
}
}
impl std::error::Error for BlobError {}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Entry {
pub key: String,
pub len: u64,
}
impl Entry {
pub fn new(key: impl Into<String>, len: u64) -> Self {
Entry {
key: key.into(),
len,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Page {
pub entries: Vec<Entry>,
pub next: Option<String>,
}
impl Page {
pub fn new(entries: Vec<Entry>, next: Option<String>) -> Self {
Page { entries, next }
}
}
pub trait BlobBackend: Send + Sync {
fn backend(&self) -> &str;
fn version(&self) -> &str;
fn read(&self, key: &str) -> impl Future<Output = Result<Option<Vec<u8>>, BlobError>> + Send;
fn write(
&self,
key: &str,
bytes: Vec<u8>,
) -> impl Future<Output = Result<(), BlobError>> + Send;
fn delete(&self, key: &str) -> impl Future<Output = Result<(), BlobError>> + Send;
fn stat(&self, key: &str) -> impl Future<Output = Result<Option<Entry>, BlobError>> + Send;
fn list(
&self,
prefix: &str,
after: Option<&str>,
limit: usize,
) -> impl Future<Output = Result<Page, BlobError>> + Send;
fn create_exclusive(
&self,
key: &str,
bytes: Vec<u8>,
) -> impl Future<Output = Result<bool, BlobError>> + Send {
let _ = (key, bytes);
async { Err(BlobError::Unsupported("create_exclusive".to_string())) }
}
fn replace_atomic(
&self,
key: &str,
bytes: Vec<u8>,
) -> impl Future<Output = Result<(), BlobError>> + Send {
let _ = (key, bytes);
async { Err(BlobError::Unsupported("replace_atomic".to_string())) }
}
fn append(
&self,
key: &str,
bytes: Vec<u8>,
) -> impl Future<Output = Result<(), BlobError>> + Send {
let _ = (key, bytes);
async { Err(BlobError::Unsupported("append".to_string())) }
}
}