pub enum HandlerResponse {
Response(Response<Body>),
Stream {
stream: Pin<Box<dyn Stream<Item = Result<Bytes, BoxError>> + Send + 'static>>,
status: StatusCode,
headers: HeaderMap,
},
}Expand description
Unified response type that can represent either a ready response or a streaming body.
This enum allows handlers to return either:
- A complete response that’s ready to send (
Responsevariant) - A streaming response with potentially unbounded data (
Streamvariant)
§Variants
-
Response- A complete Axum response ready to send to the client. Use this for responses where you have all the data ready (files, JSON bodies, HTML, etc.) -
Stream- A streaming response that produces data chunks over time. Use this for:- Large files (avoid loading entire file in memory)
- Server-Sent Events (SSE)
- Long-polling responses
- Real-time data feeds
- Any unbounded or very large responses
§Examples
// Regular response
let response = AxumResponse::builder()
.status(StatusCode::OK)
.body(Body::from("Hello"))
.unwrap();
let handler_response = HandlerResponse::from(response);
// Streaming response
let stream = futures::stream::iter(vec![
Ok::<_, Box<dyn std::error::Error>>(Bytes::from("chunk1")),
Ok(Bytes::from("chunk2")),
]);
let response = HandlerResponse::stream(stream)
.with_status(StatusCode::OK);Variants§
Response(Response<Body>)
A complete response ready to send
Stream
A streaming response with custom status and headers
Implementations§
Source§impl HandlerResponse
impl HandlerResponse
Sourcepub fn into_response(self) -> AxumResponse<Body>
pub fn into_response(self) -> AxumResponse<Body>
Convert the handler response into an Axum response.
Consumes the HandlerResponse and produces an AxumResponse<Body> ready
to be sent to the client. For streaming responses, wraps the stream in an
Axum Body.
§Returns
An AxumResponse<Body> ready to be returned from an Axum handler
Sourcepub fn stream<S, E>(stream: S) -> Self
pub fn stream<S, E>(stream: S) -> Self
Create a streaming response from any async stream of byte chunks.
Wraps an async stream of byte chunks into a HandlerResponse::Stream.
This is useful for large files, real-time data, or any unbounded response.
§Type Parameters
S- The stream type implementingStream<Item = Result<Bytes, E>>E- The error type that can be converted toBoxError
§Arguments
stream- An async stream that yields byte chunks or errors
§Returns
A HandlerResponse with 200 OK status and empty headers (customize with
with_status() and with_header())
§Example
use futures::stream;
use spikard_http::HandlerResponse;
use bytes::Bytes;
let stream = stream::iter(vec![
Ok::<_, Box<dyn std::error::Error>>(Bytes::from("Hello ")),
Ok(Bytes::from("World")),
]);
let response = HandlerResponse::stream(stream)
.with_status(StatusCode::OK);Sourcepub fn with_status(self, status: StatusCode) -> Self
pub fn with_status(self, status: StatusCode) -> Self
Override the HTTP status code for the streaming response.
Sets the HTTP status code to be used in the response. This only affects
Stream variants; regular responses already have their status set.
§Arguments
status- The HTTP status code to use (e.g.,StatusCode::OK)
§Returns
Self for method chaining
§Example
let response = HandlerResponse::stream(my_stream)
.with_status(StatusCode::PARTIAL_CONTENT);Sourcepub fn with_header(self, name: HeaderName, value: HeaderValue) -> Self
pub fn with_header(self, name: HeaderName, value: HeaderValue) -> Self
Insert or replace a header on the streaming response.
Adds an HTTP header to the response. This only affects Stream variants;
regular responses already have their headers set. If a header with the same
name already exists, it will be replaced.
§Arguments
name- The header name (e.g.,HeaderName::from_static("content-type"))value- The header value
§Returns
Self for method chaining
§Example
use axum::http::{HeaderName, HeaderValue};
let response = HandlerResponse::stream(my_stream)
.with_header(
HeaderName::from_static("content-type"),
HeaderValue::from_static("application/octet-stream")
);Trait Implementations§
Auto Trait Implementations§
impl Freeze for HandlerResponse
impl !RefUnwindSafe for HandlerResponse
impl Send for HandlerResponse
impl !Sync for HandlerResponse
impl Unpin for HandlerResponse
impl !UnwindSafe for HandlerResponse
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);