serde_request_envelope/lib.rs
1#![deny(missing_docs, missing_debug_implementations, rust_2024_compatibility)]
2//! # Serde Request Envelope
3//!
4//! This crate provides the `Request` struct, which is a newtype wrapper that takes
5//! any given serde friendly type and turns it into a request envelope that includes
6//! the type name of the given type. This lets you do tagged structures without having
7//! to manually curate enums.
8//!
9//! For example:
10//!
11//! ```rust
12//! use serde::{Deserialize, Serialize};
13//! use serde_request_envelope::Request;
14//!
15//! #[derive(Serialize, Deserialize, Debug)]
16//! struct MyStruct {
17//! field: String,
18//! }
19//!
20//! # fn main() {
21//! let my_struct = MyStruct {
22//! field: "Hello, World!".to_string(),
23//! };
24//!
25//! let request = Request::new(my_struct);
26//!
27//! let serialized = serde_json::to_string(&request).unwrap();
28//! // serialized is now: {"type":"MyStruct","data":{"field":"Hello, World!"}}
29//!
30//! let deserialized: Request<MyStruct> = serde_json::from_str(&serialized).unwrap();
31//! assert_eq!(deserialized.0.field, "Hello, World!");
32//! # }
33//! ```
34//!
35
36mod request;
37mod support;
38
39pub use request::Request;
40pub use support::type_name;