any_type 0.5.0

A library for the Anytype API
Documentation
pub const API_VERSION: &str = "2025-11-08";

use serde::Deserialize;
use std::panic::Location;

pub const AND: &str = "and";
pub const OR: &str = "or";
pub const ASCENDING: &str = "asc";
pub const DESCENDING: &str = "desc";

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AnytypeColor {
    Grey,
    Yellow,
    Orange,
    Red,
    Pink,
    Purple,
    Blue,
    Ice,
    Teal,
    Lime,
    Undefined,
}
impl AnytypeColor {
    pub fn to_string(&self) -> String {
        match self {
            AnytypeColor::Grey => "grey".to_string(),
            AnytypeColor::Yellow => "yellow".to_string(),
            AnytypeColor::Orange => "orange".to_string(),
            AnytypeColor::Red => "red".to_string(),
            AnytypeColor::Pink => "pink".to_string(),
            AnytypeColor::Purple => "purple".to_string(),
            AnytypeColor::Blue => "blue".to_string(),
            AnytypeColor::Ice => "ice".to_string(),
            AnytypeColor::Teal => "teal".to_string(),
            AnytypeColor::Lime => "lime".to_string(),
            AnytypeColor::Undefined => "grey".to_string(),
        }
    }
    pub fn from_str(color: &str) -> Self {
        match color {
            "yellow" => AnytypeColor::Yellow,
            "orange" => AnytypeColor::Orange,
            "red" => AnytypeColor::Red,
            "pink" => AnytypeColor::Pink,
            "purple" => AnytypeColor::Purple,
            "blue" => AnytypeColor::Blue,
            "ice" => AnytypeColor::Ice,
            "teal" => AnytypeColor::Teal,
            "lime" => AnytypeColor::Lime,
            _ => AnytypeColor::Undefined, // Default value
        }
    }
}

#[derive(Deserialize, Debug)]
pub struct ApiError {
    pub code: String,
    pub message: String,
    pub object: String,
    pub status: u16, // HTTP code
}

#[derive(Debug, PartialEq)]
pub enum Failures {
    Api,
    BadApi,
    Http,
    Json,
    Reqwest,
}

#[derive(Debug)]
pub struct RequestFailure {
    pub status: i32,
    pub code: String,
    pub message: String,
    pub object: String,
    pub failure_type: Failures,
}
impl RequestFailure {
    pub fn to_string(&self) -> String {
        if self.failure_type == Failures::Api {
            return format!(
                "status: {} : code: {} : message: {} : object: {}",
                self.status, self.code, self.message, self.object
            );
        }
        format!(
            "status: {} : message {} : Debug : {}",
            self.status, self.message, self.object
        )
    }

    pub fn reqwest_error(e: reqwest::Error) -> Self {
        RequestFailure {
            status: 12,
            code: "reqwest_error".to_string(),
            message: "Call to reqwest library failed".to_string(),
            object: format!("{:?}", e),
            failure_type: Failures::Reqwest,
        }
    }

    pub fn json_error(loc: &Location, e: String) -> Self {
        let message = format!(
            "Failed to handle JSON conversion in file {} @ {}/{}",
            loc.file(),
            loc.line() - 2,
            loc.column()
        );
        RequestFailure {
            status: 8,
            code: "JSON_Error".to_string(),
            message,
            object: format!("{:?}", e),
            failure_type: Failures::Json,
        }
    }
    pub async fn api_error(
        response: reqwest::Response,
        possible_status: Vec<http::StatusCode>,
    ) -> Self {
        let status = response.status();
        if !possible_status.contains(&status) {
            return RequestFailure {
                status: 4,
                code: "HTTP".to_string(),
                message: "Unexpected HTTP code received".to_string(),
                object: format!("{:?}", response),
                failure_type: Failures::Reqwest,
            };
        }
        match response.json::<ApiError>().await {
            Ok(e) => RequestFailure {
                status: 16, // was e.status,
                code: e.code.to_string(),
                message: e.message.to_string(),
                object: e.object.to_string(),
                failure_type: Failures::Api,
            },
            Err(e) => RequestFailure {
                status: 4,
                code: "HTTP".to_string(),
                message: "Error format is incorrect".to_string(),
                object: format!("{:?}", e),
                failure_type: Failures::BadApi,
            },
        }
    }
}