toggl-track 0.1.0

General-purpose async Rust library for the Toggl Track API v9
Documentation
use reqwest::StatusCode;
use thiserror::Error;

use crate::QuotaInfo;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Error)]
pub enum Error {
    #[error("failed to build HTTP client: {0}")]
    ClientBuild(#[source] reqwest::Error),

    #[error("invalid base URL: {0}")]
    InvalidBaseUrl(String),

    #[error("request failed: {0}")]
    Request(#[from] reqwest::Error),

    #[error("Toggl API returned {status}: {body}")]
    Api {
        status: StatusCode,
        body: String,
        quota: Option<QuotaInfo>,
    },

    #[error("authentication failed; check your Toggl API token")]
    Authentication,

    #[error("not found: {0}")]
    NotFound(String),

    #[error("invalid input: {0}")]
    InvalidInput(String),
}

impl Error {
    pub(crate) fn api(status: StatusCode, body: String, quota: Option<QuotaInfo>) -> Self {
        match status {
            StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => Self::Authentication,
            StatusCode::NOT_FOUND => Self::NotFound(body),
            _ => Self::Api {
                status,
                body,
                quota,
            },
        }
    }
}

#[derive(Debug, Clone, Error)]
#[error("Toggl API returned {status}: {body}")]
pub struct ApiError {
    pub status: StatusCode,
    pub body: String,
    pub quota: Option<QuotaInfo>,
}