Response

Struct Response 

Source
pub struct Response { /* private fields */ }
Expand description

Response builder for creating HTTP responses with fluent API

Implementations§

Source§

impl ElifResponse

Source

pub fn new() -> ElifResponse

Create new response with OK status

Source

pub fn with_status(status: ElifStatusCode) -> ElifResponse

Create response with specific status code

Source

pub fn status(self, status: ElifStatusCode) -> ElifResponse

Set response status code (consuming)

Source

pub fn set_status(&mut self, status: ElifStatusCode)

Set response status code (borrowing - for middleware use)

Source

pub fn status_code(&self) -> ElifStatusCode

Get response status code

Source

pub fn headers(&self) -> &ElifHeaderMap

Get response headers

Source

pub fn headers_mut(&mut self) -> &mut ElifHeaderMap

Get mutable reference to response headers

Source

pub fn has_header<K>(&self, key: K) -> bool
where K: AsRef<str>,

Check if response has a specific header

Source

pub fn get_header<K>(&self, key: K) -> Option<&ElifHeaderValue>
where K: AsRef<str>,

Get header value by name

Source

pub fn with_header<K, V>(self, key: K, value: V) -> ElifResponse
where K: AsRef<str>, V: AsRef<str>,

Add header to response (Simple - never panics)

Simple equivalent: $response->header('X-Custom', 'value') Returns 500 error response on invalid header names/values

Source

pub fn with_json<T>(self, data: &T) -> ElifResponse
where T: Serialize,

Set JSON body (Simple - never panics)

Simple equivalent: $response->json($data) Returns 500 error response on serialization failure

Source

pub fn with_text<S>(self, content: S) -> ElifResponse
where S: AsRef<str>,

Set text body (Simple - never fails)

Simple equivalent: response($text)

Source

pub fn with_html<S>(self, content: S) -> ElifResponse
where S: AsRef<str>,

Set HTML body with content-type (Simple - never fails)

Simple equivalent: response($html)->header('Content-Type', 'text/html')

Source

pub fn header<K, V>(self, key: K, value: V) -> Result<ElifResponse, HttpError>
where K: AsRef<str>, V: AsRef<str>,

Add header to response (consuming)

Source

pub fn add_header<K, V>(&mut self, key: K, value: V) -> Result<(), HttpError>
where K: AsRef<str>, V: AsRef<str>,

Add header to response (borrowing - for middleware use)

Source

pub fn remove_header<K>(&mut self, key: K) -> Option<ElifHeaderValue>
where K: AsRef<str>,

Remove header from response

Source

pub fn content_type(self, content_type: &str) -> Result<ElifResponse, HttpError>

Set Content-Type header (consuming)

Source

pub fn set_content_type(&mut self, content_type: &str) -> Result<(), HttpError>

Set Content-Type header (borrowing - for middleware use)

Source

pub fn text<S>(self, text: S) -> ElifResponse
where S: Into<String>,

Set response body as text (consuming)

Source

pub fn set_text<S>(&mut self, text: S)
where S: Into<String>,

Set response body as text (borrowing - for middleware use)

Source

pub fn bytes(self, bytes: Bytes) -> ElifResponse

Set response body as bytes (consuming)

Source

pub fn set_bytes(&mut self, bytes: Bytes)

Set response body as bytes (borrowing - for middleware use)

Source

pub fn json<T>(self, data: &T) -> Result<ElifResponse, HttpError>
where T: Serialize,

Set response body as JSON (consuming)

Source

pub fn set_json<T>(&mut self, data: &T) -> Result<(), HttpError>
where T: Serialize,

Set response body as JSON (borrowing - for middleware use)

Source

pub fn json_value(self, value: Value) -> ElifResponse

Set response body as raw JSON value (consuming)

Source

pub fn set_json_value(&mut self, value: Value)

Set response body as raw JSON value (borrowing - for middleware use)

Source

pub fn build(self) -> Result<Response<Body>, HttpError>

Build the response

Source§

impl ElifResponse

Convenience methods for common response types

Source

pub fn ok() -> ElifResponse

Create 200 OK response

Source

pub fn created() -> ElifResponse

Create 201 Created response

Source

pub fn no_content() -> ElifResponse

Create 204 No Content response

Source

pub fn bad_request() -> ElifResponse

Create 400 Bad Request response

Source

pub fn unauthorized() -> ElifResponse

Create 401 Unauthorized response

Source

pub fn forbidden() -> ElifResponse

Create 403 Forbidden response

Source

pub fn not_found() -> ElifResponse

Create 404 Not Found response

Source

pub fn unprocessable_entity() -> ElifResponse

Create 422 Unprocessable Entity response

Source

pub fn internal_server_error() -> ElifResponse

Create 500 Internal Server Error response

Source

pub fn json_ok<T>(data: &T) -> Result<Response<Body>, HttpError>
where T: Serialize,

Create JSON response with data

Source

pub fn json_error( status: ElifStatusCode, message: &str, ) -> Result<Response<Body>, HttpError>

Create JSON error response

Source

pub fn validation_error<T>(errors: &T) -> Result<Response<Body>, HttpError>
where T: Serialize,

Create validation error response

Source§

impl ElifResponse

Redirect response builders

Source

pub fn redirect_permanent(location: &str) -> Result<ElifResponse, HttpError>

Create 301 Moved Permanently redirect

Source

pub fn redirect_temporary(location: &str) -> Result<ElifResponse, HttpError>

Create 302 Found (temporary) redirect

Source

pub fn redirect_see_other(location: &str) -> Result<ElifResponse, HttpError>

Create 303 See Other redirect

Source§

impl ElifResponse

File download response builders

Source

pub fn download( filename: &str, content: Bytes, ) -> Result<ElifResponse, HttpError>

Create file download response

Source

pub fn file_inline( filename: &str, content_type: &str, content: Bytes, ) -> Result<ElifResponse, HttpError>

Create inline file response (display in browser)

Source

pub fn file<P>(path: P) -> Result<ElifResponse, HttpError>
where P: AsRef<Path>,

Create file response from filesystem path

Source§

impl ElifResponse

Enhanced response helper methods for common patterns

Source

pub fn json_with_status<T>( status: ElifStatusCode, data: &T, ) -> Result<ElifResponse, HttpError>
where T: Serialize,

Create JSON response with data and optional status

Source

pub fn json_raw(value: Value) -> ElifResponse

Create JSON response from serde_json::Value

Source

pub fn json_raw_with_status( status: ElifStatusCode, value: Value, ) -> ElifResponse

Create JSON response from serde_json::Value with status

Source

pub fn text_with_type( content: &str, content_type: &str, ) -> Result<ElifResponse, HttpError>

Create text response with custom content type

Source

pub fn xml<S>(content: S) -> Result<ElifResponse, HttpError>
where S: AsRef<str>,

Create XML response

Source

pub fn csv<S>(content: S) -> Result<ElifResponse, HttpError>
where S: AsRef<str>,

Create CSV response

Source

pub fn javascript<S>(content: S) -> Result<ElifResponse, HttpError>
where S: AsRef<str>,

Create JavaScript response

Source

pub fn css<S>(content: S) -> Result<ElifResponse, HttpError>
where S: AsRef<str>,

Create CSS response

Source

pub fn stream() -> Result<ElifResponse, HttpError>

Create streaming response with chunked transfer encoding

Source

pub fn sse() -> Result<ElifResponse, HttpError>

Create Server-Sent Events (SSE) response

Source

pub fn jsonp<T>(callback: &str, data: &T) -> Result<ElifResponse, HttpError>
where T: Serialize,

Create JSONP response with callback

Source

pub fn image( content: Bytes, format: ImageFormat, ) -> Result<ElifResponse, HttpError>

Create image response from bytes with format detection

Source

pub fn binary( content: Bytes, mime_type: &str, ) -> Result<ElifResponse, HttpError>

Create binary response with custom MIME type

Source

pub fn cors_preflight( allowed_origins: &[&str], allowed_methods: &[&str], allowed_headers: &[&str], max_age: Option<u32>, ) -> Result<ElifResponse, HttpError>

Create CORS preflight response

Source

pub fn with_cors( self, origin: &str, credentials: bool, exposed_headers: Option<&[&str]>, ) -> Result<ElifResponse, HttpError>

Add CORS headers to existing response

Source

pub fn with_cache( self, max_age: u32, public: bool, ) -> Result<ElifResponse, HttpError>

Create response with caching headers

Source

pub fn conditional(self, request_etag: Option<&str>) -> ElifResponse

Create conditional response based on If-None-Match header

Source

pub fn with_security_headers(self) -> Result<ElifResponse, HttpError>

Add security headers to response

Source

pub fn with_performance_headers(self) -> Result<ElifResponse, HttpError>

Add performance headers

Source§

impl ElifResponse

Response transformation helpers

Source

pub fn transform_body<F>(self, transform: F) -> ElifResponse

Transform response body with a closure

Source

pub fn with_headers<I, K, V>( self, headers: I, ) -> Result<ElifResponse, HttpError>
where I: IntoIterator<Item = (K, V)>, K: AsRef<str>, V: AsRef<str>,

Add multiple headers at once

Source

pub fn build_axum(self) -> Response<Body>

Create response and immediately build to Axum Response

Source

pub fn is_error(&self) -> bool

Check if response is an error (4xx or 5xx status)

Source

pub fn is_success(&self) -> bool

Check if response is successful (2xx status)

Source

pub fn is_redirect(&self) -> bool

Check if response is a redirect (3xx status)

Source

pub fn body_size_estimate(&self) -> usize

Get response body size estimate

Trait Implementations§

Source§

impl Debug for ElifResponse

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for ElifResponse

Source§

fn default() -> ElifResponse

Returns the “default value” for a type. Read more
Source§

impl From<ResponseBuilder> for ElifResponse

Convert ResponseBuilder to ElifResponse

Source§

fn from(builder: ResponseBuilder) -> ElifResponse

Converts to this type from the input type.
Source§

impl HttpAssertions for ElifResponse

Source§

fn assert_ok(&self)

Source§

fn assert_status(&self, expected: ElifStatusCode)

Source§

fn assert_json_contains(&self, _key: &str, _value: &str)

Source§

impl IntoElifResponse for ElifResponse

Source§

impl IntoResponse for ElifResponse

Convert ElifResponse to Axum Response

Source§

fn into_response(self) -> Response<Body>

Create a response.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,