multibucket 0.2.0

A unified async object-storage trait with a from-scratch Qiniu Kodo backend (upload / download / delete / stat / list / signed URLs).
Documentation
//! Crate-level error type.

use thiserror::Error;

/// Convenient result alias used across the crate.
pub type Result<T> = std::result::Result<T, Error>;

/// All errors produced by `multibucket`.
#[derive(Debug, Error)]
pub enum Error {
    /// Low-level HTTP transport error from `reqwest`.
    #[error("http request failed: {0}")]
    Http(#[from] reqwest::Error),

    /// (De)serialization error from `serde_json`.
    #[error("serialization error: {0}")]
    Serde(#[from] serde_json::Error),

    /// Invalid configuration or arguments.
    #[error("invalid input: {0}")]
    InvalidInput(String),

    /// The remote backend returned a non-success status.
    ///
    /// `status` is the HTTP status code; `body` is the (possibly truncated)
    /// response body; `qiniu_code` is Qiniu's business error code when present.
    #[error("backend error (http {status}, qiniu code {qiniu_code:?}): {body}")]
    Api {
        /// HTTP status code returned by the backend.
        status: u16,
        /// Qiniu business error code, when derivable (usually equals `status`).
        qiniu_code: Option<i64>,
        /// (Possibly truncated) response body.
        body: String,
    },

    /// A signature / crypto primitive failed.
    #[error("crypto error: {0}")]
    Crypto(String),
}

impl Error {
    /// Build an [`Error::Api`] from a response.
    pub fn api(status: u16, qiniu_code: Option<i64>, body: impl Into<String>) -> Self {
        Error::Api {
            status,
            qiniu_code,
            body: body.into(),
        }
    }
}