Skip to main content

haste_document_storage/
traits.rs

1use std::{future::Future, pin::Pin};
2
3use crate::{
4    error::StorageError,
5    types::{Document, DocumentKey, DocumentMetadata, ListPage, ListRequest, StoredDocument},
6};
7
8/// Boxed future returned by [`DocumentStorage`] methods, so the trait stays
9/// object safe.
10pub type StorageFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T, StorageError>> + Send + 'a>>;
11
12/// Object storage for document bodies, keyed by [`DocumentKey`].
13///
14/// An implementation wraps one bucket (or container) and is shared across
15/// tasks behind an `Arc`. Keys are opaque to callers: how they map onto the
16/// backend, including any deployment-wide prefix, is the backend's concern.
17pub trait DocumentStorage: Send + Sync {
18    /// Stores `document` under `key`, replacing whatever was there.
19    fn put<'a>(
20        &'a self,
21        key: &'a DocumentKey,
22        document: Document,
23    ) -> StorageFuture<'a, DocumentMetadata>;
24
25    /// Reads a document's metadata without its body.
26    /// Fails with [`StorageError::NotFound`] when nothing is stored under `key`.
27    fn head<'a>(&'a self, key: &'a DocumentKey) -> StorageFuture<'a, DocumentMetadata>;
28
29    /// Reads a document, streaming its body.
30    /// Fails with [`StorageError::NotFound`] when nothing is stored under `key`.
31    fn get<'a>(&'a self, key: &'a DocumentKey) -> StorageFuture<'a, StoredDocument>;
32
33    /// Deletes the document under `key`. Deleting a missing document succeeds.
34    fn delete<'a>(&'a self, key: &'a DocumentKey) -> StorageFuture<'a, ()>;
35
36    /// Lists documents whose keys start with the request's prefix, one page at
37    /// a time; pass the returned token back to fetch the next page.
38    fn list(&self, request: ListRequest) -> StorageFuture<'_, ListPage>;
39}