Next

Struct Next 

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

Represents the next step in the middleware chain.

Next is a continuation that encapsulates the remaining middleware and the final handler. When called via run(), it executes the next middleware (or handler) in the chain and returns the response.

§Cloning

Next is cheap to clone as it uses Arc internally for shared ownership.

§Examples

§Basic Usage

use ignitia::middleware::{Middleware, Next};
use ignitia::{Request, Response};

#[derive(Clone)]
struct LoggingMiddleware;

#[async_trait::async_trait]
impl Middleware for LoggingMiddleware {
    async fn handle(&self, req: Request, next: Next) -> Response {
        println!("Processing request...");

        // Call next middleware/handler
        let response = next.run(req).await;

        println!("Request processed");
        response
    }
}

§Conditional Next Execution

use ignitia::middleware::{Middleware, Next};
use ignitia::{Request, Response, StatusCode};

#[derive(Clone)]
struct AuthMiddleware;

#[async_trait::async_trait]
impl Middleware for AuthMiddleware {
    async fn handle(&self, req: Request, next: Next) -> Response {
        if req.header("authorization").is_some() {
            // Authorized - continue chain
            next.run(req).await
        } else {
            // Not authorized - don't call next
            Response::new(StatusCode::UNAUTHORIZED)
        }
    }
}

Implementations§

Source§

impl Next

Source

pub fn new<F>(f: F) -> Self
where F: Fn(Request) -> BoxFuture<'static, Response> + Send + Sync + 'static,

Create a new Next continuation.

This method is used internally by the framework to build the middleware chain. It wraps a function that takes a request and returns a future resolving to a response.

§Arguments
  • func - Function representing the next step in the chain
§Returns

Returns a new Next instance.

§Examples
use ignitia::middleware::Next;
use ignitia::{Request, Response};

let next = Next::new(|req: Request| {
    Box::pin(async move {
        Response::text("Hello from next")
    })
});
Source

pub async fn run(self, req: Request) -> Response

Execute the next step in the middleware chain.

This method invokes the next middleware or handler in the chain with the given request and awaits its completion, returning the resulting response.

§Arguments
  • req - The HTTP request to pass to the next step
§Returns

Returns the HTTP Response from the next step in the chain.

§Examples
§Simple Pass-through
use ignitia::middleware::{Middleware, Next};
use ignitia::{Request, Response};

#[derive(Clone)]
struct PassThroughMiddleware;

#[async_trait::async_trait]
impl Middleware for PassThroughMiddleware {
    async fn handle(&self, req: Request, next: Next) -> Response {
        // Simply pass request to next step
        next.run(req).await
    }
}
§Measuring Execution Time
use ignitia::middleware::{Middleware, Next};
use ignitia::{Request, Response};
use std::time::Instant;

#[derive(Clone)]
struct TimerMiddleware;

#[async_trait::async_trait]
impl Middleware for TimerMiddleware {
    async fn handle(&self, req: Request, next: Next) -> Response {
        let start = Instant::now();

        // Run next middleware/handler
        let response = next.run(req).await;

        let duration = start.elapsed();
        println!("Request took: {:?}", duration);

        response
    }
}

Trait Implementations§

Source§

impl Clone for Next

Source§

fn clone(&self) -> Next

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Next

§

impl !RefUnwindSafe for Next

§

impl Send for Next

§

impl Sync for Next

§

impl Unpin for Next

§

impl !UnwindSafe for Next

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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,