pub mod factory;
pub mod fs_store;
pub use factory::{BackendFuture, BackendOpener, BackendRegistry, open_store};
pub use fs_store::FsStore;
use std::time::Duration;
use async_trait::async_trait;
use crate::fs::{BoxRead, FsError};
use crate::model::{ContentKey, ItemId, ItemMeta, ModelError, NewItem};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PutOutcome {
pub meta: ItemMeta,
pub created: bool,
}
#[async_trait]
pub trait Store: Send + Sync {
async fn put(&self, item: NewItem, content: BoxRead) -> Result<PutOutcome, StoreError>;
async fn list(&self) -> Result<Vec<ItemMeta>, StoreError>;
async fn list_after(&self, after: Option<&ItemId>) -> Result<Vec<ItemMeta>, StoreError>;
async fn newest_id(&self) -> Result<Option<ItemId>, StoreError> {
Ok(self
.list_after(None)
.await?
.into_iter()
.next()
.map(|meta| meta.id))
}
async fn get(&self, id: &ItemId) -> Result<(ItemMeta, BoxRead), StoreError>;
async fn exists(&self, id: &ItemId) -> Result<bool, StoreError>;
async fn find_by_content_key(&self, key: &ContentKey) -> Result<Option<ItemMeta>, StoreError>;
async fn resolve(&self, input: &str) -> Result<ItemId, StoreError>;
async fn delete(&self, id: &ItemId) -> Result<ItemMeta, StoreError>;
async fn clean_staging(&self, older_than: Duration) -> Result<usize, StoreError>;
}
#[derive(Debug, thiserror::Error)]
pub enum StoreError {
#[error("no item matches `{0}`")]
NotFound(String),
#[error("`{input}` matches {} items: {}", candidates.len(), join_ids(candidates))]
Ambiguous {
input: String,
candidates: Vec<ItemId>,
},
#[error("`{0}` is too short: give at least 4 characters of the id")]
InvalidPrefix(String),
#[error("unsupported storage backend `{0}`")]
UnsupportedBackend(String),
#[error("storage configuration: {0}")]
Config(String),
#[error(transparent)]
Fs(#[from] FsError),
#[error("reading the content failed: {0}")]
Content(String),
#[error("item {id} is corrupt: {reason}")]
Corrupt {
id: String,
reason: String,
},
#[error(transparent)]
Model(#[from] ModelError),
#[error("{0}")]
Backend(String),
}
fn join_ids(ids: &[ItemId]) -> String {
ids.iter()
.map(ItemId::as_str)
.collect::<Vec<_>>()
.join(", ")
}