34 releases (10 stable)

1.0.10 Aug 4, 2024
1.0.8 May 12, 2024
1.0.7 Aug 5, 2023
1.0.6 Apr 30, 2023
0.5.6 Jul 21, 2022

#306 in Parser implementations

Download history 257/week @ 2024-08-16 209/week @ 2024-08-23 295/week @ 2024-08-30 219/week @ 2024-09-06 286/week @ 2024-09-13 309/week @ 2024-09-20 295/week @ 2024-09-27 170/week @ 2024-10-04 253/week @ 2024-10-11 244/week @ 2024-10-18 263/week @ 2024-10-25 218/week @ 2024-11-01 207/week @ 2024-11-08 227/week @ 2024-11-15 177/week @ 2024-11-22 148/week @ 2024-11-29

793 downloads per month
Used in aim

MIT license

72KB
1.5K SLoC

url-parse

CI CD Security Audit codecov crates.io Documentation

A library for parsing URLs.

Why?

url-parse provides some schemes unavailable in other url parsing crates (i.e. sftp, ssh, s3) and enables the user to specify custom schemes before parsing.

Usage

Basic

Create a new parser object with Parser::new(). You can then use parser.parse(url) which will return a public Url parsed structure back.

Its fields are then directly accessible:

let input = "https://user:pass@www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
let result = Parser::new(None).parse(input).unwrap();
assert_eq!(
    result,
    Url {
        scheme: Some("https".to_string()),
        user_pass: (Some("user".to_string()), Some("pass".to_string())),
        subdomain: Some("www".to_string()),
        domain: Some("example.co".to_string()),
        top_level_domain: Some("uk".to_string()),
        port: Some(443),
        path: Some(vec![
            "blog".to_string(),
            "article".to_string(),
            "search".to_string(),
        ]),
        query: Some("docid=720&hl=en#dayone".to_string()),
        anchor: Some("dayone".to_string()),
    }
)

Custom schemes

Passing a Some(HashMap) to Parser::new() can be used to create custom schemes.

The hashmap is a key,value pair representing the scheme name (key) to a port and description mapping (value).

let input = "myschema://user:pass@example.co.uk/path/to/file.txt";
let mut myport_mappings = HashMap::new();
myport_mappings.insert("myschema", (8888, "My custom schema"));
let result = Parser::new(Some(myport_mappings)).parse(input).unwrap();
assert_eq!(
    result,
    Url {
        scheme: Some("myschema".to_string()),
        user_pass: (Some("user".to_string()), Some("pass".to_string())),
        subdomain: Some("www".to_string()),
        domain: Some("example.co".to_string()),
        top_level_domain: Some("uk".to_string()),
        port: Some(8888),
        path: Some(vec![
            "path".to_string(),
            "to".to_string(),
            "file.txt".to_string(),
        ]),
        query: None,
        anchor: None,
    }
);

Dependencies

~2.7–4MB
~66K SLoC