pub struct Negotiate<T> {
pub data: T,
pub format: OutputFormat,
}Expand description
Content-negotiated response wrapper
Automatically serializes to JSON or TOON based on the client’s Accept header.
If the client prefers TOON (Accept: application/toon), returns TOON format.
Otherwise, defaults to JSON.
§Example
ⓘ
use rustapi_rs::prelude::*;
use rustapi_rs::toon::{Negotiate, AcceptHeader};
#[derive(Serialize)]
struct User {
id: u64,
name: String,
}
// Automatic negotiation via extractor
async fn get_user(accept: AcceptHeader) -> Negotiate<User> {
Negotiate::new(
User { id: 1, name: "Alice".to_string() },
accept.preferred,
)
}
// Or explicitly choose format
async fn get_user_toon() -> Negotiate<User> {
Negotiate::toon(User { id: 1, name: "Alice".to_string() })
}Fields§
§data: TThe data to serialize
format: OutputFormatThe output format to use
Implementations§
Source§impl<T> Negotiate<T>
impl<T> Negotiate<T>
Sourcepub fn new(data: T, format: OutputFormat) -> Self
pub fn new(data: T, format: OutputFormat) -> Self
Create a new negotiated response with the specified format
Sourcepub fn format(&self) -> OutputFormat
pub fn format(&self) -> OutputFormat
Get the output format
Trait Implementations§
Source§impl<T: Serialize> IntoResponse for Negotiate<T>
impl<T: Serialize> IntoResponse for Negotiate<T>
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Convert self into a Response
Source§impl<T: Send> OperationModifier for Negotiate<T>
impl<T: Send> OperationModifier for Negotiate<T>
Source§fn update_operation(_op: &mut Operation)
fn update_operation(_op: &mut Operation)
Update the operation
Source§impl<T: Serialize> ResponseModifier for Negotiate<T>
impl<T: Serialize> ResponseModifier for Negotiate<T>
Source§fn update_response(op: &mut Operation)
fn update_response(op: &mut Operation)
Update the operation with response information
Auto Trait Implementations§
impl<T> Freeze for Negotiate<T>where
T: Freeze,
impl<T> RefUnwindSafe for Negotiate<T>where
T: RefUnwindSafe,
impl<T> Send for Negotiate<T>where
T: Send,
impl<T> Sync for Negotiate<T>where
T: Sync,
impl<T> Unpin for Negotiate<T>where
T: Unpin,
impl<T> UnwindSafe for Negotiate<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more