pub struct Parser { /* private fields */ }
Implementationsยง
Sourceยงimpl Parser
impl Parser
Sourcepub fn anchor<'a>(&self, input: &'a str) -> Option<&'a str>
pub fn anchor<'a>(&self, input: &'a str) -> Option<&'a str>
Extract the anchor from the url.
ยงExample
use url_parse::core::Parser;
let input = "https://www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
let result = Parser::new(None).anchor(input).unwrap();
assert_eq!(result, "dayone");
Sourceยงimpl Parser
impl Parser
Sourcepub fn domain<'a>(&self, input: &'a str) -> Domain<'a>
pub fn domain<'a>(&self, input: &'a str) -> Domain<'a>
Extract the domain fields from the url.
ยงExample
use url_parse::core::Parser;
use url_parse::core::global::Domain;
let input = "https://www.example.com:443/blog/article/search?docid=720&hl=en#dayone";
let expected = Domain {
subdomain: Some("www"),
domain: Some("example"),
top_level_domain: Some("com"),
};
let result = Parser::new(None).domain(input);
assert_eq!(result, expected);
Sourceยงimpl Parser
impl Parser
Sourcepub fn login<'a>(&self, input: &'a str) -> (Option<&'a str>, Option<&'a str>)
pub fn login<'a>(&self, input: &'a str) -> (Option<&'a str>, Option<&'a str>)
Extract the domain fields from the url.
ยงExample
use url_parse::core::Parser;
let input = "https://user:pass@www.example.co.uk";
let expected = (Some("user"), Some("pass"));
let result = Parser::new(None).login(input);
assert_eq!(result, expected);
Sourceยงimpl Parser
impl Parser
Sourcepub fn path<'a>(&self, input: &'a str) -> Option<Vec<&'a str>>
pub fn path<'a>(&self, input: &'a str) -> Option<Vec<&'a str>>
Extract the path as a vector from the url.
ยงExample
use url_parse::core::Parser;
let input = "https://www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
let result = Parser::new(None).path(input).unwrap();
let expected = vec!["blog", "article", "search"];
assert_eq!(result, expected);
Sourceยงimpl Parser
impl Parser
Sourcepub fn port(&self, input: &str) -> Option<u32>
pub fn port(&self, input: &str) -> Option<u32>
Extract the port from the url. If no port is present, it will be deduced from the scheme.
The default scheme provides well-known ports. The user can specify new schemes when constructing the Parser object with new()
.
ยงExample
use url_parse::core::Parser;
let input = "https://www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
let port = Parser::new(None).port(input);
assert_eq!(port.unwrap(), 443);
Sourceยงimpl Parser
impl Parser
Sourcepub fn query<'a>(&self, input: &'a str) -> Option<&'a str>
pub fn query<'a>(&self, input: &'a str) -> Option<&'a str>
Extract the query from the url.
ยงExample
use url_parse::core::Parser;
let input = "https://www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
let result = Parser::new(None).query(input).unwrap();
assert_eq!(result, "docid=720&hl=en");
Sourceยงimpl Parser
impl Parser
Sourcepub fn scheme<'a>(&self, input: &'a str) -> Option<(&'a str, SchemeSeparator)>
pub fn scheme<'a>(&self, input: &'a str) -> Option<(&'a str, SchemeSeparator)>
Extract the query from the url.
ยงExample
use url_parse::core::Parser;
use url_parse::core::scheme_separator::SchemeSeparator;
let input = "https://www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
let scheme = Parser::new(None).scheme(input);
assert_eq!(scheme.unwrap(), ("https", SchemeSeparator::ColonSlashSlash));
Schemas can also have a simple colon instead ot the โ://โ pattern.
ยงExample
use url_parse::core::Parser;
use url_parse::core::scheme_separator::SchemeSeparator;
let input = "https:www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
let scheme = Parser::new(None).scheme(input);
assert_eq!(scheme.unwrap(), ("https", SchemeSeparator::Colon));
Sourceยงimpl Parser
impl Parser
Sourcepub fn new(
port_mappings: Option<HashMap<&'static str, (u32, &'static str)>>,
) -> Self
pub fn new( port_mappings: Option<HashMap<&'static str, (u32, &'static str)>>, ) -> Self
Create a new parser object. Optionally pass in a hash map of default port mappings. Its fields are then directly accessible.
ยงExample
use url_parse::core::Parser;
let parser = Parser::new(None);
Sourcepub fn parse(&self, url: &str) -> Result<Url, ParseError>
pub fn parse(&self, url: &str) -> Result<Url, ParseError>
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.
ยงExample
use url_parse::core::Parser;
use url_parse::url::Url;
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()),
}
)