Coder Social home page Coder Social logo

routem's Introduction

routem: a type-aware route parsing library

routem is a Rust crate which allows you to match paths to specs for different routes. The main use is to allow you to specify that a path only matches if its parameters are of the correct type.

Installation

To add routem to your Rust project, install it using Cargo:

cargo add routem

It compiles with stable and has been tested with Cargo 1.74.1.

Usage and examples

The core structs in routem are Route, Routes, and Parser.

  • Route is a spec for a particular route in your application. It contains a name (useful to finding associated data after you find the matching route) and a spec for the route. You can use it to query whether or not a particular path matches this spec.
  • Routes contains multiple Routes and can be used to check which of all of your configured routes matches a path. Useful for finding a handler for incoming requests, or simply verifying that a match exists at all.
  • Parser is used to construct routes. By default it is configured with three parameter types (string, 64-bit int, and UUID)

Here's an end-to-end example of creating a parser, configuring routes, then finding the matching route (if any) for a given path.

use routem::Parser;

let parser = Parser::default();

let user_route = parser.route("user-by-id", "/user/<id:uuid>/").unwrap();
let club_route = parser.route("club-by-id", "/user/<id:int>/").unwrap();

routes.add(user_route);
routes.add(club_route);


routes.find("/user/36be8705-6c31-45d7-9321-d56cc07b50d9/") // Some(user_route)
routes.find("/club/123/"); // Some(club_route)

routes.find("/user/123/"); // None
routes.find("/club/36be8705-6c31-45d7-9321-d56cc07b50d9/") // None
routes.find("/club/123"); // None

Here's an example where we add a custom parameter type. Adding custom types is optional.

use routem::{Parser, ParamType};

fn is_palindrome(ident: &str) -> bool {
    ident.chars().rev().collect::<String>() == ident
}

let custom_type = ParamType::new("palindrome", is_palindrome);

let parser = Parser::default();
parser.add_param_type(custom_type);


let club_route = parser.route("club-by-id", "/club/<id:palindrome>/").unwrap();

assert_eq!(None, routes.find("/club/myclub/"));
assert_eq!(Some(&club_route), routes.find("/club/radar/"));

Roadmap

There are a few nice-to-have features currently missing from routem. Here's what is currently planned to be developed eventually:

  • Query parameter validation and parsing
  • Union types in route and query parameters

License

Copyright on the initial code is held by Remesh. Any external contributors retain their copyright; we do not seek copyright assignment.

This software is dual-licensed under the Apache v2 and MIT licenses.

See LICENSE-APACHE and LICENSE-MIT for details.

routem's People

Contributors

ntietz avatar

Watchers

Dan Reich avatar  avatar Ivy Cheung avatar Stanley Sarama avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.