AkurAI Build
Menu

Public repository / master

AkurAI-Framework / crates/cli/src/json_api.rs

Latest change c6179dcf3abbf53272e4279e6740397ad2e75726 - AKURAI-FRAMEWORK-1..9 consolidate framework APIs by Ólafur Búi Ólafsson

use akurai_http::Response;
use akurai_json::{parse, Value};

/// Decode a request body while preserving the distinct UTF-8 and JSON errors.
pub(crate) fn parse_body(body: &[u8]) -> Result<Value, String> {
    let text = std::str::from_utf8(body).map_err(|_| "request body is not UTF-8".to_string())?;
    parse(text).map_err(|e| format!("invalid JSON: {e}"))
}

/// Serialize a JSON value with the framework's canonical response header.
pub(crate) fn response(status: u16, value: &Value) -> Response {
    Response::new(status).with_body(
        "application/json; charset=utf-8",
        value.to_json().into_bytes(),
    )
}