accept-header

Crates.ioaccept-header
lib.rsaccept-header
version0.2.3
sourcesrc
created_at2022-11-14 04:52:04.612226
updated_at2023-02-14 04:21:18.241425
descriptionA simple library for parsing HTTP Accept headers for content negotiation.
homepagehttps://github.com/tyrchen/accept-header
repositoryhttps://github.com/tyrchen/accept-header
max_upload_size
id714712
size31,169
Tyr Chen (tyrchen)

documentation

https://docs.rs/accept-header

README

HTTP accept header

A very basic & naive implementation of the HTTP Accept header. It uses http crate and mime crate to parse the accept header. Basic data structure:

#[derive(Debug, Clone, PartialEq)]
pub struct Accept {
    pub wildcard: Option<MediaType>,
    pub types: Vec<MediaType>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MediaType {
    pub mime: Mime,
    pub weight: Option<f32>,
}

Usage:

// parse accept header
let accept: Accept = "application/json, text/html;q=0.9, text/plain;q=0.8, */*;q=0.7"
    .parse()
    .unwrap();

// prepare a list of supported media types
let available = vec![
    Mime::from_str("text/html").unwrap(),
    Mime::from_str("application/json").unwrap(),
];

// content negotiation
let negotiated = accept.negotiate(&available).unwrap();

// "application/json" shall be chosen since it is available and has the highest weight
assert_eq!(negotiated, Mime::from_str("application/json").unwrap());
Commit count: 14

cargo fmt