cortex-mem-service 2.7.0

HTTP REST API service for Cortex Memory
use axum::{
    http::StatusCode,
    response::{IntoResponse, Response},
    Json,
};
use serde_json::json;

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

#[derive(Debug)]
#[allow(dead_code)]
pub enum AppError {
    Internal(String),
    NotFound(String),
    BadRequest(String),
    Core(cortex_mem_core::Error),
}

impl From<cortex_mem_core::Error> for AppError {
    fn from(err: cortex_mem_core::Error) -> Self {
        AppError::Core(err)
    }
}

impl From<anyhow::Error> for AppError {
    fn from(err: anyhow::Error) -> Self {
        AppError::Internal(err.to_string())
    }
}

impl From<serde_json::Error> for AppError {
    fn from(err: serde_json::Error) -> Self {
        AppError::Internal(err.to_string())
    }
}

impl IntoResponse for AppError {
    fn into_response(self) -> Response {
        let (status, error_message) = match self {
            AppError::Internal(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg),
            AppError::NotFound(msg) => (StatusCode::NOT_FOUND, msg),
            AppError::BadRequest(msg) => (StatusCode::BAD_REQUEST, msg),
            AppError::Core(err) => match err {
                cortex_mem_core::Error::NotFound { uri } => {
                    (StatusCode::NOT_FOUND, format!("Not found: {}", uri))
                }
                _ => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()),
            },
        };

        let body = Json(json!({
            "error": error_message,
            "status": status.as_u16(),
        }));

        (status, body).into_response()
    }
}