Skip to main content

Module negotiate

Module negotiate 

Source
Expand description

Content negotiation engine and format wrappers.

Provides format marker types (JsonFormat, TextFormat, HtmlFormat, CsvFormat), the RenderAs trait for converting domain types into specific formats, and NegotiatedResponse which inspects the Accept header to pick the best representation.

§Example

use typeway_server::negotiate::*;

#[derive(serde::Serialize)]
struct User { id: u32, name: String }

impl std::fmt::Display for User {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "User({}, {})", self.id, self.name)
    }
}

async fn get_user(accept: AcceptHeader) -> NegotiatedResponse<User, (JsonFormat, TextFormat)> {
    NegotiatedResponse::new(User { id: 1, name: "Alice".into() }, accept.0)
}

Structs§

AcceptHeader
Extracts the Accept header value from the request.
CsvFormat
CSV representation.
HtmlFormat
HTML representation. Uses Display (intended for types that produce HTML).
JsonFormat
JSON representation. Serializes via serde_json.
NegotiatedResponse
A response that holds a domain value and negotiates the best content type based on the Accept header.
TextFormat
Plain text representation. Uses Display.
XmlFormat
XML representation. Requires explicit RenderAsXml impls per type.

Traits§

NegotiateFormats
Select the best format from a tuple of formats based on the Accept header and render the domain value.
RenderAs
Convert a domain type into bytes for a specific content format.
RenderAsXml
Trait for types that can render as XML. Unlike JsonFormat/TextFormat which have blanket impls, XML rendering requires an explicit impl per type since there’s no standard XML serialization trait.

Functions§

negotiated
Wrap a domain value for content negotiation with the given Accept header.