regex_router

Crates.ioregex_router
lib.rsregex_router
version1.0.2
sourcesrc
created_at2021-11-23 15:10:04.509622
updated_at2021-11-25 19:56:05.136282
descriptionAn implementation of request routing via a singular grouped regex (with support for path parameter extraction).
homepagehttps://github.com/Majored/rs-regex-router
repositoryhttps://github.com/Majored/rs-regex-router
max_upload_size
id486277
size29,267
Harry (Majored)

documentation

https://docs.rs/regex_router/

README

rs-regex-router

GitHub license Crates.io docs.rs

An implementation of request routing via a singular grouped regex (with support for path parameter extraction).

Features

  • Design approach based upon this article.
  • Implemented over a generic handler type allowing use with varying webserver crates.
  • Extraction of path parameters which are then mapped as key/value pairs.
  • A convenience macro for easily declaring routes.

Installation & Basic Usage

[dependencies]
regex_router = "1.0.2"

An example using a unit handler type:

use regex_router::{RouterBuilder, route};
...

let mut builder = RouterBuilder::<()>::new();

route!(builder; r"/example";; "GET" => ());
route!(builder; r"/test";; "GET" => ());

let router = builder.build().unwrap();

An example declaring path parameters:

let mut builder = RouterBuilder::<()>::new();

route!(builder; r"/example/(\d+)"; "var1"; "GET" => ());
route!(builder; r"/test/(\d+)/(\d+)"; "var1", "var2"; "GET" => ());

let router = builder.build().unwrap();

Dispatching against a router:

match router.dispatch("GET", "/example/500") {
  Some(route_match) => {
    // Call handler and return response.
  }
  None => {
    // No route match. Return 404.
  }
};

An example implementation for hyper can be found here.

Issues & Support

Whether you're wanting to report a bug you've come across during use of this crate or are seeking general help/assistance, please utilise the issues tracker and provide as much detail as possible (eg. recreation steps).

I try to respond to issues within a reasonable timeframe.

Commit count: 0

cargo fmt