#http-request #http-response #serde #request-response #serialization #deserialize #url

http-serde-ext

serde support for http crate types Request, Response, Uri, StatusCode, HeaderMap, Method, in Option or other collections

12 releases (3 stable)

1.0.2 Feb 19, 2024
1.0.1 Feb 11, 2024
1.0.0 Nov 26, 2023
0.1.8 Nov 9, 2023
0.1.6 Oct 27, 2023

#202 in Encoding

Download history 145/week @ 2024-07-15 89/week @ 2024-07-22 245/week @ 2024-07-29 152/week @ 2024-08-05 105/week @ 2024-08-12 222/week @ 2024-08-19 221/week @ 2024-08-26 161/week @ 2024-09-02 299/week @ 2024-09-09 248/week @ 2024-09-16 441/week @ 2024-09-23 433/week @ 2024-09-30 285/week @ 2024-10-07 383/week @ 2024-10-14 530/week @ 2024-10-21 294/week @ 2024-10-28

1,514 downloads per month
Used in 8 crates (4 directly)

MIT license

59KB
1K SLoC

serde extensions for the http crate types

Allows serializing and deserializing the following types from http:

Allows serializing and deserializing the above types wrapped in the following std container types:

  • Option
  • Result in the Ok position
  • Vec
  • VecDeque
  • LinkedList
  • HashMap as the Key for all except HeaderMap, Request, and Response. As the Value for all types.
  • BTreeMap as the Key only for HeaderValue, StatusCode, and Version. As the Value for all types.
  • HashSet for all except HeaderMap, Request, and Response
  • BTreeSet only for HeaderValue, StatusCode, and Version

Installation

Run the following Cargo command in your project directory:

cargo add http-serde-ext

Or add the following line to your Cargo.toml:

http-serde-ext = "1.0.2"

Usage

This library is intended to be used with serde's derive feature. Fields should use the appropriate #[serde(with = "...")] annotation for that type. Full examples are provided in each module section of the docs.

use std::collections::*;

use http::*;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct MyStruct {
    #[serde(with = "http_serde_ext::response")]
    base: Response<Vec<u8>>,

    #[serde(with = "http_serde_ext::request::option", default)]
    option: Option<Request<String>>,

    #[serde(with = "http_serde_ext::method::vec")]
    vec: Vec<Method>,

    #[serde(with = "http_serde_ext::uri::vec_deque")]
    vec_deque: VecDeque<Uri>,

    #[serde(with = "http_serde_ext::header_map::linked_list")]
    linked_list: LinkedList<HeaderMap>,

    #[serde(with = "http_serde_ext::header_map_generic::hash_map")]
    hash_map: HashMap<String, HeaderMap<String>>,

    #[serde(with = "http_serde_ext::status_code::btree_map_key")]
    btree_map: BTreeMap<StatusCode, i32>,

    #[serde(with = "http_serde_ext::authority::hash_set")]
    hash_set: HashSet<uri::Authority>,
}

This library can also be used to manually De/Serialize types if given a De/Serializer. For example, when using serde_json:

let uri = http::Uri::default();
let serialized = http_serde_ext::uri::serialize(&uri, serde_json::value::Serializer).unwrap();
let deserialized = http_serde_ext::uri::deserialize(serialized).unwrap();
assert_eq!(uri, deserialized);

let mut responses: Vec<http::Response<()>> = vec![http::Response::default()];
let serialized =
    http_serde_ext::response::vec::serialize(&responses, serde_json::value::Serializer)
        .unwrap();
let mut deserialized: Vec<http::Response<()>> =
    http_serde_ext::response::vec::deserialize(serialized).unwrap();

let original = responses.remove(0).into_parts();
let deserialized = deserialized.remove(0).into_parts();

assert_eq!(original.0.status, deserialized.0.status);
assert_eq!(original.0.version, deserialized.0.version);
assert_eq!(original.0.headers, deserialized.0.headers);
assert_eq!(original.1, deserialized.1);

Acknowledgements

This crate is heavily inspired by Kornel's http-serde.

Dependencies

~0.9–1.6MB
~32K SLoC