Skip to main content

kellnr_storage/
storage.rs

1use async_trait::async_trait;
2use bytes::Bytes;
3use chrono::{DateTime, Utc};
4
5use crate::storage_error::StorageError;
6
7/// An object's bytes alongside the metadata needed for HTTP conditional-GET
8/// (`ETag`/`Last-Modified`) support.
9pub struct ObjectWithMeta {
10    pub bytes: Bytes,
11    pub e_tag: Option<String>,
12    pub last_modified: DateTime<Utc>,
13}
14
15#[async_trait]
16pub trait Storage {
17    async fn get(&self, key: &str) -> Result<Bytes, StorageError>;
18    /// Like [`Storage::get`], but also returns the `ETag`/`Last-Modified`
19    /// metadata `object_store` already computes for every backend, so
20    /// callers can support conditional-GET without a second round-trip.
21    async fn get_with_meta(&self, key: &str) -> Result<ObjectWithMeta, StorageError>;
22    async fn put(&self, key: &str, object: Bytes) -> Result<(), StorageError>;
23    /// Like [`Storage::put`], but overwrites an existing key instead of
24    /// failing. Needed for content that is republished in place (e.g. docs),
25    /// unlike crate tarballs/toolchain archives which are immutable.
26    async fn put_overwrite(&self, key: &str, object: Bytes) -> Result<(), StorageError>;
27    async fn delete(&self, key: &str) -> Result<(), StorageError>;
28    async fn exists(&self, key: &str) -> Result<bool, StorageError>;
29    /// Size of the object in bytes, read from its metadata without
30    /// downloading it.
31    async fn size(&self, key: &str) -> Result<u64, StorageError>;
32    /// Recursively list every key stored under `prefix`.
33    async fn list(&self, prefix: &str) -> Result<Vec<String>, StorageError>;
34}