Crate jsend

Source
Expand description

The jsend crate provides an implementation of the JSend specification for API responses in Rust applications.

§Usage

Add jsend to your Cargo.toml:

[dependencies]
jsend = "1.0"
serde = { version = "1.0", features = ["derive"] }

§Basic Example

use jsend::JSendResponse;
use std::collections::HashMap;

// Success response with data
let data = Some(HashMap::from([("key", "value")]));
let response = JSendResponse::success(data);
println!("{}", serde_json::to_string(&response).unwrap());

// Error response
let error_response = JSendResponse::error("An error occurred".to_string(), Some(100), None::<String>);
println!("{}", serde_json::to_string(&error_response).unwrap());

A more in-depth example of how this crate could be used with a framework like axum can be found in the examples/ directory.

§Features

Enums§

JSendResponse
The JSendResponse enum provides a way to model JSend compliant responses.