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§
- Accept
Header - Extracts the
Acceptheader value from the request. - CsvFormat
- CSV representation.
- Html
Format - HTML representation. Uses
Display(intended for types that produce HTML). - Json
Format - JSON representation. Serializes via
serde_json. - Negotiated
Response - A response that holds a domain value and negotiates the best content type
based on the
Acceptheader. - Text
Format - Plain text representation. Uses
Display. - XmlFormat
- XML representation. Requires explicit
RenderAsXmlimpls per type.
Traits§
- Negotiate
Formats - Select the best format from a tuple of formats based on the
Acceptheader and render the domain value. - Render
As - Convert a domain type into bytes for a specific content format.
- Render
AsXml - 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.