multibucket 0.1.1

A unified async object-storage trait with a from-scratch Qiniu Kodo backend (upload / download / delete / stat / list / signed URLs).
Documentation
//! Backend-agnostic data types used by the [`ObjectStorage`](crate::ObjectStorage) trait.

use std::time::SystemTime;

use bytes::Bytes;

/// Metadata describing a single stored object.
#[derive(Debug, Clone, Default)]
pub struct ObjectMetadata {
    /// Object key (path) inside the bucket.
    pub key: String,
    /// Size in bytes.
    pub size: u64,
    /// Content type, when the backend reports it.
    pub content_type: Option<String>,
    /// Entity tag / hash reported by the backend (Qiniu `hash`).
    pub etag: Option<String>,
    /// Last-modification time, when the backend reports it.
    pub last_modified: Option<SystemTime>,
}

/// Options accepted when uploading an object.
#[derive(Debug, Clone, Default)]
pub struct PutOptions {
    /// Explicit content type. Backends may infer it from the key when `None`.
    pub content_type: Option<String>,
    /// Whether the object should be treated as immutable (Qiniu `insertOnly`).
    pub insert_only: bool,
}

impl PutOptions {
    /// Create options with the given content type.
    pub fn with_content_type(content_type: impl Into<String>) -> Self {
        PutOptions {
            content_type: Some(content_type.into()),
            insert_only: false,
        }
    }
}

/// Result of a listing operation.
#[derive(Debug, Clone, Default)]
pub struct ListOutcome {
    /// Objects returned for this page.
    pub items: Vec<ObjectMetadata>,
    /// Marker to continue from; `None` when the listing is complete.
    pub next_marker: Option<String>,
}

/// A body that can be uploaded. Accepts in-memory bytes and owned buffers.
#[derive(Debug, Clone)]
pub enum PutBody {
    /// In-memory bytes (cheap to clone).
    Bytes(Bytes),
}

impl PutBody {
    /// Materialize the body as [`Bytes`].
    pub fn into_bytes(self) -> Bytes {
        match self {
            PutBody::Bytes(b) => b,
        }
    }

    /// Length in bytes.
    pub fn len(&self) -> usize {
        match self {
            PutBody::Bytes(b) => b.len(),
        }
    }

    /// Whether the body is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl From<Vec<u8>> for PutBody {
    fn from(v: Vec<u8>) -> Self {
        PutBody::Bytes(Bytes::from(v))
    }
}

impl From<Bytes> for PutBody {
    fn from(b: Bytes) -> Self {
        PutBody::Bytes(b)
    }
}

impl From<&'static [u8]> for PutBody {
    fn from(s: &'static [u8]) -> Self {
        PutBody::Bytes(Bytes::from_static(s))
    }
}