Skip to main content

ProductOSRouter

Struct ProductOSRouter 

Source
pub struct ProductOSRouter<S = ()> { /* private fields */ }
Expand description

Main router struct for Product OS Router

Provides a high-level API for defining routes, handlers, and middleware. Supports both stateful and stateless routing.

§Type Parameters

  • S - The state type (defaults to () for stateless routing)

§Examples

§Stateless Router

use product_os_router::ProductOSRouter;

async fn handler() -> &'static str {
    "Hello!"
}

let mut router = ProductOSRouter::new();
router.add_get_no_state("/", handler);

§Stateful Router

use product_os_router::ProductOSRouter;
use axum::extract::State;

#[derive(Clone)]
struct AppState {
    message: String,
}

async fn handler(State(state): State<AppState>) -> String {
    state.message
}

let state = AppState { message: "Hello!".to_string() };
let mut router = ProductOSRouter::new_with_state(state);
router.add_get("/", handler);

Implementations§

Source§

impl ProductOSRouter

Source

pub fn new() -> ProductOSRouter

Create a new stateless router

Creates a router without any shared state. Use new_with_state if you need to share state between handlers.

§Examples
use product_os_router::ProductOSRouter;

let router = ProductOSRouter::new();
Source§

impl ProductOSRouter

Source

pub fn param_to_field(path: &str) -> String

Convert path parameters from Express-style to Axum format

Transforms route paths with :param and *wildcard syntax into Axum’s {param} format.

§Arguments
  • path - The path with Express-style parameters
§Returns

The path converted to Axum format

§Examples
use product_os_router::ProductOSRouter;

assert_eq!(ProductOSRouter::param_to_field("/users/:id"), "/users/{id}");
assert_eq!(ProductOSRouter::param_to_field("/files/*path"), "/files/{path}");
Source

pub fn add_service_no_state<Z>(&mut self, path: &str, service: Z)
where Z: Service<Request<Body>, Response = Response<Body>, Error = Box<dyn Error + Sync + Send>> + Clone + Send + Sync + 'static, <Z as Service<Request<Body>>>::Future: Send + 'static,

Add a Tower service as a route handler without state.

The service must accept Request<Body> and return Response<Body>.

Source

pub fn add_middleware_no_state<L, ResBody>(&mut self, middleware: L)
where L: Layer<Route> + Clone + Send + Sync + 'static, <L as Layer<Route>>::Service: Service<Request<Body>, Response = Response<ResBody>> + Clone + Send + Sync + 'static, ResBody: Body<Data = Bytes> + Send + 'static, <ResBody as Body>::Error: Into<Box<dyn Error + Sync + Send>>, <<L as Layer<Route>>::Service as Service<Request<Body>>>::Future: Send + 'static, <<L as Layer<Route>>::Service as Service<Request<Body>>>::Error: Into<Box<dyn Error + Sync + Send>> + 'static,

Add a Tower middleware layer to the stateless router.

The layer wraps every route and is applied in LIFO order (last added runs first).

Source

pub fn add_default_header_no_state( &mut self, header_name: &str, header_value: &str, )

Add a default header to all responses from this stateless router.

Headers added this way will not overwrite headers already present in the response.

Source

pub fn not_implemented() -> Response<Body>

Returns a 501 Not Implemented JSON response.

Use this to indicate that the server does not support the functionality required to fulfill the request.

Source

pub fn not_implemented_legacy() -> Response<Body>

👎Deprecated since 0.0.41: Use not_implemented() which correctly returns 501, or build a 404 response explicitly

Returns a 404 Not Found JSON response.

This was the original behavior of not_implemented() prior to v0.0.41. Use not_implemented for the correct 501 status code, or use this method if you specifically need a 404 response.

Source

pub fn add_route_no_state(&mut self, path: &str, service_handler: MethodRouter)

Add a pre-built MethodRouter at the given path without state.

Source

pub fn set_fallback_no_state(&mut self, service_handler: MethodRouter)

Set a fallback MethodRouter for unmatched routes without state.

Source

pub fn add_get_no_state<H, T>(&mut self, path: &str, handler: H)
where H: Handler<T, ()>, T: 'static,

Add a GET route handler without state

§Arguments
  • path - The route path (supports :param and *wildcard)
  • handler - The async function to handle requests
§Examples
use product_os_router::ProductOSRouter;

async fn hello() -> &'static str {
    "Hello, World!"
}

let mut router = ProductOSRouter::new();
router.add_get_no_state("/", hello);
Source

pub fn add_post_no_state<H, T>(&mut self, path: &str, handler: H)
where H: Handler<T, ()>, T: 'static,

Add a POST route handler without state

§Arguments
  • path - The route path
  • handler - The async function to handle requests
§Examples
use product_os_router::{ProductOSRouter, Json};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
struct User {
    name: String,
}

async fn create_user(Json(user): Json<User>) -> Json<User> {
    Json(user)
}

let mut router = ProductOSRouter::new();
router.add_post_no_state("/users", create_user);
Source

pub fn add_handler_no_state<H, T>( &mut self, path: &str, method: Method, handler: H, )
where H: Handler<T, ()>, T: 'static,

Add a handler for the given HTTP method and path without state.

Source

pub fn set_fallback_handler_no_state<H, T>(&mut self, handler: H)
where H: Handler<T, ()>, T: 'static,

Set a fallback handler for unmatched routes without state.

Source

pub fn add_handlers_no_state<H, T>( &mut self, path: &str, handlers: BTreeMap<Method, H>, )
where H: Handler<T, ()>, T: 'static,

Add multiple HTTP method handlers at a single path without state.

Source§

impl<S> ProductOSRouter<S>
where S: Clone + Send + Sync + 'static,

Source

pub fn get_router(&self) -> Router

Build and return the finalized Router with the stored state applied.

The returned Router can be used with a server (e.g., axum::serve).

Source

pub fn get_router_mut(&mut self) -> &mut Router<S>

Return a mutable reference to the underlying Router for advanced configuration.

Source

pub fn new_with_state(state: S) -> ProductOSRouter<S>
where S: Clone + Send + 'static,

Create a new router with the given shared state.

The state is made available to handlers via the State extractor.

Source

pub fn add_service<Z>(&mut self, path: &str, service: Z)
where Z: Service<Request<Body>, Response = Response<Body>, Error = Box<dyn Error + Sync + Send>> + Clone + Send + Sync + 'static, <Z as Service<Request<Body>>>::Response: IntoResponse, <Z as Service<Request<Body>>>::Future: Send + 'static,

Add a Tower service as a route handler with state.

Source

pub fn add_middleware<L, ResBody>(&mut self, middleware: L)
where L: Layer<Route> + Clone + Send + Sync + 'static, <L as Layer<Route>>::Service: Service<Request<Body>, Response = Response<ResBody>> + Clone + Send + Sync + 'static, ResBody: Body<Data = Bytes> + Send + 'static, <ResBody as Body>::Error: Into<Box<dyn Error + Sync + Send>>, <<L as Layer<Route>>::Service as Service<Request<Body>>>::Future: Send + 'static, <<L as Layer<Route>>::Service as Service<Request<Body>>>::Error: Into<Box<dyn Error + Sync + Send>> + 'static,

Add a Tower middleware layer to the router.

Source

pub fn add_default_header(&mut self, header_name: &str, header_value: &str)

Add a default header to all responses from this router.

Headers added this way will not overwrite headers already present in the response.

§Arguments
  • header_name - The header name
  • header_value - The header value
Source

pub fn add_default_header_owned( &mut self, header_name: String, header_value: String, )

👎Deprecated since 0.0.41: Use add_default_header(&str, &str) instead

Add a default header to all responses from this router.

This method accepts owned String parameters for backward compatibility. Prefer add_default_header which takes &str instead.

Source

pub fn add_route(&mut self, path: &str, service_handler: MethodRouter<S>)
where S: Clone + Send + 'static,

Add a pre-built MethodRouter at the given path.

Source

pub fn set_fallback(&mut self, service_handler: MethodRouter<S>)
where S: Clone + Send + 'static,

Set a fallback MethodRouter for unmatched routes.

Source

pub fn add_get<H, T>(&mut self, path: &str, handler: H)
where H: Handler<T, S>, T: 'static,

Add a GET route handler with state.

Source

pub fn add_post<H, T>(&mut self, path: &str, handler: H)
where H: Handler<T, S>, T: 'static,

Add a POST route handler with state.

Source

pub fn add_handler<H, T>(&mut self, path: &str, method: Method, handler: H)
where H: Handler<T, S>, T: 'static,

Add a handler for the given HTTP method and path with state.

Source

pub fn add_state(&mut self, state: S)

Replace the router’s shared state.

Source

pub fn set_fallback_handler<H, T>(&mut self, handler: H)
where H: Handler<T, S>, T: 'static,

Set a fallback handler for unmatched routes with state.

Source

pub fn add_handlers<H, T>(&mut self, path: &str, handlers: BTreeMap<Method, H>)
where H: Handler<T, S>, T: 'static,

Add multiple HTTP method handlers at a single path with state.

Trait Implementations§

Source§

impl Default for ProductOSRouter

Source§

fn default() -> ProductOSRouter

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

Auto Trait Implementations§

§

impl<S> Freeze for ProductOSRouter<S>
where S: Freeze,

§

impl<S = ()> !RefUnwindSafe for ProductOSRouter<S>

§

impl<S> Send for ProductOSRouter<S>
where S: Send,

§

impl<S> Sync for ProductOSRouter<S>
where S: Sync,

§

impl<S> Unpin for ProductOSRouter<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for ProductOSRouter<S>
where S: UnsafeUnpin,

§

impl<S = ()> !UnwindSafe for ProductOSRouter<S>

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> CastSlice<'_, T> for T

Source§

fn cast_slice(selves: &[T]) -> &[T]

Source§

impl<T> CastSliceMut<'_, T> for T

Source§

fn cast_slice_mut(selves: &mut [T]) -> &mut [T]

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