pub struct Url { /* private fields */ }
Implementations§
source§impl Url
impl Url
sourcepub fn parse(input: &str, base: Option<&str>) -> Result<Url, Error>
pub fn parse(input: &str, base: Option<&str>) -> Result<Url, Error>
Parses the input with an optional base
use ada_url::Url;
let out = Url::parse("https://ada-url.github.io/ada", None)
.expect("This is a valid URL. Should have parsed it.");
assert_eq!(out.protocol(), "https:");
sourcepub fn can_parse(input: &str, base: Option<&str>) -> bool
pub fn can_parse(input: &str, base: Option<&str>) -> bool
Returns whether or not the URL can be parsed or not.
For more information, read WHATWG URL spec
use ada_url::Url;
assert!(Url::can_parse("https://ada-url.github.io/ada", None));
assert!(Url::can_parse("/pathname", Some("https://ada-url.github.io/ada")));
sourcepub fn origin(&mut self) -> &str
pub fn origin(&mut self) -> &str
Return the origin of this URL
For more information, read WHATWG URL spec
use ada_url::Url;
let mut url = Url::parse("blob:https://example.com/foo", None).expect("Invalid URL");
assert_eq!(url.origin(), "https://example.com");
sourcepub fn href(&self) -> &str
pub fn href(&self) -> &str
Return the parsed version of the URL with all components. For more information, read WHATWG URL spec
pub fn set_href(&mut self, input: &str) -> bool
sourcepub fn username(&self) -> &str
pub fn username(&self) -> &str
Return the username for this URL as a percent-encoded ASCII string.
For more information, read WHATWG URL spec
use ada_url::Url;
let url = Url::parse("ftp://rms:secret123@example.com", None).expect("Invalid URL");
assert_eq!(url.username(), "rms");
pub fn set_username(&mut self, input: &str) -> bool
sourcepub fn password(&self) -> &str
pub fn password(&self) -> &str
Return the password for this URL, if any, as a percent-encoded ASCII string.
For more information, read WHATWG URL spec
use ada_url::Url;
let url = Url::parse("ftp://rms:secret123@example.com", None).expect("Invalid URL");
assert_eq!(url.password(), "secret123");
pub fn set_password(&mut self, input: &str) -> bool
sourcepub fn port(&self) -> &str
pub fn port(&self) -> &str
Return the port number for this URL, or an empty string.
For more information, read WHATWG URL spec
use ada_url::Url;
let url = Url::parse("https://example.com", None).expect("Invalid URL");
assert_eq!(url.port(), "");
let url = Url::parse("https://example.com:8080", None).expect("Invalid URL");
assert_eq!(url.port(), "8080");
pub fn set_port(&mut self, input: &str) -> bool
sourcepub fn hash(&self) -> &str
pub fn hash(&self) -> &str
Return this URL’s fragment identifier, or an empty string. A fragment is the part of the URL with the # symbol. The fragment is optional and, if present, contains a fragment identifier that identifies a secondary resource, such as a section heading of a document. In HTML, the fragment identifier is usually the id attribute of a an element that is scrolled to on load. Browsers typically will not send the fragment portion of a URL to the server.
For more information, read WHATWG URL spec
use ada_url::Url;
let url = Url::parse("https://example.com/data.csv#row=4", None).expect("Invalid URL");
assert_eq!(url.hash(), "#row=4");
assert!(url.has_hash());
pub fn set_hash(&mut self, input: &str)
sourcepub fn host(&self) -> &str
pub fn host(&self) -> &str
Return the parsed representation of the host for this URL with an optional port number.
For more information, read WHATWG URL spec
use ada_url::Url;
let url = Url::parse("https://127.0.0.1:8080/index.html", None).expect("Invalid URL");
assert_eq!(url.host(), "127.0.0.1:8080");
pub fn set_host(&mut self, input: &str) -> bool
sourcepub fn hostname(&self) -> &str
pub fn hostname(&self) -> &str
Return the parsed representation of the host for this URL. Non-ASCII domain labels are punycode-encoded per IDNA if this is the host of a special URL, or percent encoded for non-special URLs.
Hostname does not contain port number.
For more information, read WHATWG URL spec
use ada_url::Url;
let url = Url::parse("https://127.0.0.1:8080/index.html", None).expect("Invalid URL");
assert_eq!(url.hostname(), "127.0.0.1");
pub fn set_hostname(&mut self, input: &str) -> bool
sourcepub fn pathname(&self) -> &str
pub fn pathname(&self) -> &str
Return the path for this URL, as a percent-encoded ASCII string.
For more information, read WHATWG URL spec
use ada_url::Url;
let url = Url::parse("https://example.com/api/versions?page=2", None).expect("Invalid URL");
assert_eq!(url.pathname(), "/api/versions");
pub fn set_pathname(&mut self, input: &str) -> bool
sourcepub fn search(&self) -> &str
pub fn search(&self) -> &str
Return this URL’s query string, if any, as a percent-encoded ASCII string.
For more information, read WHATWG URL spec
use ada_url::Url;
let url = Url::parse("https://example.com/products?page=2", None).expect("Invalid URL");
assert_eq!(url.search(), "?page=2");
let url = Url::parse("https://example.com/products", None).expect("Invalid URL");
assert_eq!(url.search(), "");
pub fn set_search(&mut self, input: &str)
sourcepub fn protocol(&self) -> &str
pub fn protocol(&self) -> &str
Return the scheme of this URL, lower-cased, as an ASCII string with the ‘:’ delimiter.
For more information, read WHATWG URL spec
use ada_url::Url;
let url = Url::parse("file:///tmp/foo", None).expect("Invalid URL");
assert_eq!(url.protocol(), "file:");