Router

Struct Router 

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

Main router

Implementations§

Source§

impl Router

Source

pub fn new() -> Router

Create a new router

Source

pub fn route(self, path: &str, method_router: MethodRouter) -> Router

Add a route

Source

pub fn state<S>(self, state: S) -> Router
where S: Clone + Send + Sync + 'static,

Add application state

Source

pub fn has_state<S>(&self) -> bool
where S: 'static,

Check if state of a given type exists

Source

pub fn state_type_ids(&self) -> &[TypeId]

Get state type IDs (for testing and debugging)

Source

pub fn nest(self, prefix: &str, router: Router) -> Router

Nest another router under a prefix

All routes from the nested router will be registered with the prefix prepended to their paths. State from the nested router is merged into the parent router (parent state takes precedence for type conflicts).

§State Merging

When nesting routers with state:

  • If the parent router has state of type T, it is preserved (parent wins)
  • If only the nested router has state of type T, it is added to the parent
  • State type tracking is merged to enable proper conflict detection

Note: Due to limitations of http::Extensions, automatic state merging requires using the merge_state method for specific types.

§Example
use rustapi_core::{Router, get};

async fn list_users() -> &'static str { "List users" }
async fn get_user() -> &'static str { "Get user" }

let users_router = Router::new()
    .route("/", get(list_users))
    .route("/{id}", get(get_user));

let app = Router::new()
    .nest("/api/users", users_router);

// Routes are now:
// GET /api/users/
// GET /api/users/{id}
Source

pub fn merge_state<S>(self, other: &Router) -> Router
where S: Clone + Send + Sync + 'static,

Merge state from another router into this one

This method allows explicit state merging when nesting routers. Parent state takes precedence - if the parent already has state of type S, the nested state is ignored.

§Example
#[derive(Clone)]
struct DbPool(String);

let nested = Router::new().state(DbPool("nested".to_string()));
let parent = Router::new()
    .merge_state::<DbPool>(&nested); // Adds DbPool from nested
Source

pub fn registered_routes(&self) -> &HashMap<String, RouteInfo>

Get registered routes (for testing and debugging)

Source

pub fn method_routers(&self) -> &HashMap<String, MethodRouter>

Get method routers (for OpenAPI integration during nesting)

Trait Implementations§

Source§

impl Default for Router

Source§

fn default() -> Router

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

Auto Trait Implementations§

§

impl !Freeze for Router

§

impl !RefUnwindSafe for Router

§

impl Send for Router

§

impl Sync for Router

§

impl Unpin for Router

§

impl !UnwindSafe for Router

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, 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