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 解析器实现
每月下载量 1,264次
在 aim 中使用
72KB
1.5K SLoC
url-parse
URL解析库。
为什么使用?
url-parse
提供了一些在其他URL解析crate中不可用的方案(例如 sftp
、ssh
、s3
)并在解析前允许用户指定自定义方案。
用法
基本用法
使用 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