3 stable releases
1.0.2 | Mar 16, 2024 |
---|
#1630 in Web programming
11KB
108 lines
jsend
An implementation of the JSend specification in Rust.
See the crate documention, the examples, and the JSend specification for more information.
lib.rs
:
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
serde
: Enabled by default. Adds serde::Serialize and serde::Deserialize derives, along with attributes to serialize into JSON according to the JSend specification.
Dependencies
~0.3–1MB
~23K SLoC