34个版本 (10个稳定版本)

1.0.10 2024年8月4日
1.0.8 2024年5月12日
1.0.7 2023年8月5日
1.0.6 2023年4月30日
0.5.6 2022年7月21日

#218 in 解析器实现

Download history 297/week @ 2024-05-01 380/week @ 2024-05-08 523/week @ 2024-05-15 336/week @ 2024-05-22 355/week @ 2024-05-29 295/week @ 2024-06-05 249/week @ 2024-06-12 307/week @ 2024-06-19 286/week @ 2024-06-26 367/week @ 2024-07-03 321/week @ 2024-07-10 299/week @ 2024-07-17 291/week @ 2024-07-24 380/week @ 2024-07-31 320/week @ 2024-08-07 222/week @ 2024-08-14

每月下载量 1,264次
aim 中使用

MIT 许可证

72KB
1.5K SLoC

url-parse

CI CD Security Audit codecov crates.io Documentation

URL解析库。

为什么使用?

url-parse 提供了一些在其他URL解析crate中不可用的方案(例如 sftpsshs3)并在解析前允许用户指定自定义方案。

用法

基本用法

使用 Parser::new() 创建一个新的解析器对象。然后,您可以使用 parser.parse() 返回一个已解析的结构。

它的字段可以直接访问

let input = "https://user:[email protected]: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()),
    }
)

自定义方案

Some(HashMap) 传递到 Parser::new() 可以创建自定义方案。

哈希表是一个键值对,表示方案名称(键)到端口和描述映射(值)。

let input = "myschema://user:[email protected]/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,
    }
);

依赖项

~2.7–4MB
~66K SLoC