//! What the parent needs from Supabase (STORAGE.md §2.2 RPCs and §3.3 Storage calls), //! behind one trait so the job and sweep logic is tested on an in-memory fake and the HTTP //! implementation stays thin. use std::fmt; use serde::Deserialize; /// One claimed row of `assets` (`claim_avatar_jobs`). #[derive(Debug, Clone, Deserialize, PartialEq, Eq)] pub struct Job { pub asset_id: String, pub owner_id: String, pub bucket_id: String, pub object_key: String, pub quarantine_key: Option, pub lock_token: String, pub attempts: i64, } /// One claimed purge (`claim_purges`). #[derive(Debug, Clone, Deserialize, PartialEq, Eq)] pub struct Purge { pub purge_id: String, pub bucket_id: String, pub object_name: String, } /// One quarantine object without a live row (`claim_quarantine_leftovers`). #[derive(Debug, Clone, Deserialize, PartialEq, Eq)] pub struct Leftover { pub bucket_id: String, pub object_name: String, pub asset_id: Option, } /// Closed failure kinds of a backend call. `Refused` carries the closed code a `P0001` /// refusal names (`invalid_state`, `invalid_reason`…); every other kind is transport-side. #[derive(Debug, Clone, PartialEq, Eq)] pub enum BackendError { Network(String), Status(u16), Refused(String), Decode(String), /// The downloaded body exceeded the input cap. Overflow, Internal(String), } impl fmt::Display for BackendError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Network(detail) => write!(f, "network: {detail}"), Self::Status(status) => write!(f, "http status {status}"), Self::Refused(code) => write!(f, "refused: {code}"), Self::Decode(detail) => write!(f, "decode: {detail}"), Self::Overflow => f.write_str("body over the cap"), Self::Internal(detail) => write!(f, "internal: {detail}"), } } } impl std::error::Error for BackendError {} impl BackendError { /// Stable name for the `code` log field. pub fn kind(&self) -> &'static str { match self { Self::Network(_) => "network", Self::Status(_) => "status", Self::Refused(_) => "refused", Self::Decode(_) => "decode", Self::Overflow => "overflow", Self::Internal(_) => "internal", } } } pub type BackendResult = Result; /// The eleven operations of STORAGE.md §2.2 and §3.3. pub trait Backend { fn claim_jobs(&self, max_jobs: u32) -> BackendResult>; /// `Status(404)` when the object never landed; `Overflow` past the input cap. fn download_quarantine(&self, bucket: &str, key: &str) -> BackendResult>; /// Upsert of the clean bytes at the final key (idempotent on retry). fn put_clean(&self, bucket: &str, key: &str, bytes: &[u8]) -> BackendResult<()>; fn complete( &self, asset_id: &str, lock_token: &str, byte_size: usize, sha256: &str, ) -> BackendResult<()>; fn reject(&self, asset_id: &str, lock_token: &str, reason: &str) -> BackendResult<()>; /// A missing object counts as deleted. fn delete_object(&self, bucket: &str, key: &str) -> BackendResult<()>; fn mark_quarantine_purged(&self, asset_id: &str) -> BackendResult<()>; fn claim_purges(&self, max_items: u32) -> BackendResult>; fn ack_purge(&self, purge_id: &str) -> BackendResult<()>; fn claim_quarantine_leftovers( &self, older_than_seconds: u64, max_items: u32, ) -> BackendResult>; fn expire_pending_uploads(&self, max_age_seconds: u64) -> BackendResult; }