Coder Social home page Coder Social logo

url-match's Introduction

url-match

url-match on crates.io

URL match patterns library.

Demo

$ cargo run --example matcher
$ cargo run --example matchers
$ cargo run --example uri_checker

Unit Test

$ cargo test

Install

Add this to your Cargo.toml:

[dependencies]
url-match = "0.1"

and this to your crate root:

extern crate url_match;

use url_match::matcher;

Usage

extern crate url_match;

use url_match::matcher;


fn main() {

    matcher("http://example.com/settings/:type", "http://example.com/settings/profile"); /*
    {
        type : profile
    }
    */
    matcher("http://example.com/settings/:type", "http://example.com/settings/admin"); /*
    {
        type : admin
    }
    */
    matcher("http://example.com/users/:name/:type", "http://example.com/users/u4bi/repos"); /*
    {
        name : u4bi,
        type : repos
    }
    */
    matcher("http://example.com/users/:name/:type", "http://example.com/users/u4bi/orgs"); /*
    {
        name : u4bi,
        type : orgs
    }
    */
    matcher("http://example.com/:name?tab=:panel", "http://example.com/u4bi?tab=stars"); /*
    {
        name  : u4bi,
        panel : stars
    }
    */
    matcher("http://example.com/:name/?tab=:panel", "http://example.com/u4bi/?tab=stars"); /*
    {
        name  : u4bi,
        panel : starts
    }
    */


    matcher(
        "http://example.com/?name=:NAME&level=:LEVEL&weapon=:WEAPON",
        "http://example.com/?name=u4bi&level=17&weapon=ak-47"); /*
    {
        NAME   : 'u4bi', 
        LEVEL  : '17', 
        WEAPON : 'ak-47'
    }
    */

    matcher("https://:SUB_DOMAIN.github.com", "https://api.github.com"); /*
    {
        SUB_DOMAIN : 'api'
    }
    */


/*
    running 8 tests
    test exam2 ... ok
    test exam3 ... ok
    test exam1 ... ok
    test exam4 ... ok
    test exam5 ... ok
    test exam6 ... ok
    test exam8 ... ok
    test exam7 ... ok

    test result: ok. 8 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

*/


    let patterns = vec![
                    "/settings/:type",
                    "/settings/:type",
                    "/users/:name/:type",
                    "/users/:name/:type",
                    "/:name?tab=:panel",
                    "/:name/?tab=:panel",
                    "/?name=:NAME&level=:LEVEL&weapon=:WEAPON"
                ];


    let uri = "/?name=u4bi&level=17&weapon=ak-47";


    for p in patterns {

        matcher(p, uri); /*
        {
            NAME   : 'u4bi',
            LEVEL  : '17',
            WEAPON : 'ak-47'
        }
        */

    }

}

Library URL
url-match https://crates.io/crates/url-match

License

MIT

url-match's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

url-match's Issues

Urls with periods in the path

I have a URL Scheme that uses periods in the path. But simple matching using url-match doesn't seem to work, as periods are used as delimiters:

    let pattern = "/:foo/:bar";
    let path = "/v1.0/people";
    let map = matcher(pattern, path);
    assert!(map.is_some()); // fails w/ None
    let map = map.unwrap();
    assert_eq!(map.get("foo").map(|e| *e), Some("v1.0"));

Is there a special trick to getting periods to match? Or are they currently unsupported?

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.