[−][src]Struct librepology::v1::types::Www
Methods
impl Www
[src]
Methods from Deref<Target = Url>
pub fn join(&self, input: &str) -> Result<Url, ParseError>
[src]
Parse a string as an URL, with this URL as the base URL.
Note: a trailing slash is significant. Without it, the last path component is considered to be a “file” name to be removed to get at the “directory” that is used as the base:
Examples
use url::Url; let base = Url::parse("https://example.net/a/b.html")?; let url = base.join("c.png")?; assert_eq!(url.as_str(), "https://example.net/a/c.png"); // Not /a/b.html/c.png let base = Url::parse("https://example.net/a/b/")?; let url = base.join("c.png")?; assert_eq!(url.as_str(), "https://example.net/a/b/c.png");
Errors
If the function can not parse an URL from the given string
with this URL as the base URL, a ParseError
variant will be returned.
pub fn as_str(&self) -> &str
[src]
Return the serialization of this URL.
This is fast since that serialization is already stored in the Url
struct.
Examples
use url::Url; let url_str = "https://example.net/"; let url = Url::parse(url_str)?; assert_eq!(url.as_str(), url_str);
pub fn origin(&self) -> Origin
[src]
Return the origin of this URL (https://url.spec.whatwg.org/#origin)
Note: this returns an opaque origin for file:
URLs, which causes
url.origin() != url.origin()
.
Examples
URL with ftp
scheme:
use url::{Host, Origin, Url}; let url = Url::parse("ftp://example.com/foo")?; assert_eq!(url.origin(), Origin::Tuple("ftp".into(), Host::Domain("example.com".into()), 21));
URL with blob
scheme:
use url::{Host, Origin, Url}; let url = Url::parse("blob:https://example.com/foo")?; assert_eq!(url.origin(), Origin::Tuple("https".into(), Host::Domain("example.com".into()), 443));
URL with file
scheme:
use url::{Host, Origin, Url}; let url = Url::parse("file:///tmp/foo")?; assert!(!url.origin().is_tuple()); let other_url = Url::parse("file:///tmp/foo")?; assert!(url.origin() != other_url.origin());
URL with other scheme:
use url::{Host, Origin, Url}; let url = Url::parse("foo:bar")?; assert!(!url.origin().is_tuple());
pub fn scheme(&self) -> &str
[src]
Return the scheme of this URL, lower-cased, as an ASCII string without the ':' delimiter.
Examples
use url::Url; let url = Url::parse("file:///tmp/foo")?; assert_eq!(url.scheme(), "file");
pub fn has_authority(&self) -> bool
[src]
Return whether the URL has an 'authority', which can contain a username, password, host, and port number.
URLs that do not are either path-only like unix:/run/foo.socket
or cannot-be-a-base like data:text/plain,Stuff
.
Examples
use url::Url; let url = Url::parse("ftp://rms@example.com")?; assert!(url.has_authority()); let url = Url::parse("unix:/run/foo.socket")?; assert!(!url.has_authority()); let url = Url::parse("data:text/plain,Stuff")?; assert!(!url.has_authority());
pub fn cannot_be_a_base(&self) -> bool
[src]
Return whether this URL is a cannot-be-a-base URL, meaning that parsing a relative URL string with this URL as the base will return an error.
This is the case if the scheme and :
delimiter are not followed by a /
slash,
as is typically the case of data:
and mailto:
URLs.
Examples
use url::Url; let url = Url::parse("ftp://rms@example.com")?; assert!(!url.cannot_be_a_base()); let url = Url::parse("unix:/run/foo.socket")?; assert!(!url.cannot_be_a_base()); let url = Url::parse("data:text/plain,Stuff")?; assert!(url.cannot_be_a_base());
pub fn username(&self) -> &str
[src]
Return the username for this URL (typically the empty string) as a percent-encoded ASCII string.
Examples
use url::Url; let url = Url::parse("ftp://rms@example.com")?; assert_eq!(url.username(), "rms"); let url = Url::parse("ftp://:secret123@example.com")?; assert_eq!(url.username(), ""); let url = Url::parse("https://example.com")?; assert_eq!(url.username(), "");
pub fn password(&self) -> Option<&str>
[src]
Return the password for this URL, if any, as a percent-encoded ASCII string.
Examples
use url::Url; let url = Url::parse("ftp://rms:secret123@example.com")?; assert_eq!(url.password(), Some("secret123")); let url = Url::parse("ftp://:secret123@example.com")?; assert_eq!(url.password(), Some("secret123")); let url = Url::parse("ftp://rms@example.com")?; assert_eq!(url.password(), None); let url = Url::parse("https://example.com")?; assert_eq!(url.password(), None);
pub fn has_host(&self) -> bool
[src]
Equivalent to url.host().is_some()
.
Examples
use url::Url; let url = Url::parse("ftp://rms@example.com")?; assert!(url.has_host()); let url = Url::parse("unix:/run/foo.socket")?; assert!(!url.has_host()); let url = Url::parse("data:text/plain,Stuff")?; assert!(!url.has_host());
pub fn host_str(&self) -> Option<&str>
[src]
Return the string representation of the host (domain or IP address) for this URL, if any.
Non-ASCII domains are punycode-encoded per IDNA.
IPv6 addresses are given between [
and ]
brackets.
Cannot-be-a-base URLs (typical of data:
and mailto:
) and some file:
URLs
don’t have a host.
See also the host
method.
Examples
use url::Url; let url = Url::parse("https://127.0.0.1/index.html")?; assert_eq!(url.host_str(), Some("127.0.0.1")); let url = Url::parse("ftp://rms@example.com")?; assert_eq!(url.host_str(), Some("example.com")); let url = Url::parse("unix:/run/foo.socket")?; assert_eq!(url.host_str(), None); let url = Url::parse("data:text/plain,Stuff")?; assert_eq!(url.host_str(), None);
pub fn host(&self) -> Option<Host<&str>>
[src]
Return the parsed representation of the host for this URL. Non-ASCII domain labels are punycode-encoded per IDNA.
Cannot-be-a-base URLs (typical of data:
and mailto:
) and some file:
URLs
don’t have a host.
See also the host_str
method.
Examples
use url::Url; let url = Url::parse("https://127.0.0.1/index.html")?; assert!(url.host().is_some()); let url = Url::parse("ftp://rms@example.com")?; assert!(url.host().is_some()); let url = Url::parse("unix:/run/foo.socket")?; assert!(url.host().is_none()); let url = Url::parse("data:text/plain,Stuff")?; assert!(url.host().is_none());
pub fn domain(&self) -> Option<&str>
[src]
If this URL has a host and it is a domain name (not an IP address), return it.
Examples
use url::Url; let url = Url::parse("https://127.0.0.1/")?; assert_eq!(url.domain(), None); let url = Url::parse("mailto:rms@example.net")?; assert_eq!(url.domain(), None); let url = Url::parse("https://example.com/")?; assert_eq!(url.domain(), Some("example.com"));
pub fn port(&self) -> Option<u16>
[src]
Return the port number for this URL, if any.
Examples
use url::Url; let url = Url::parse("https://example.com")?; assert_eq!(url.port(), None); let url = Url::parse("ssh://example.com:22")?; assert_eq!(url.port(), Some(22));
pub fn port_or_known_default(&self) -> Option<u16>
[src]
Return the port number for this URL, or the default port number if it is known.
This method only knows the default port number
of the http
, https
, ws
, wss
, ftp
, and gopher
schemes.
For URLs in these schemes, this method always returns Some(_)
.
For other schemes, it is the same as Url::port()
.
Examples
use url::Url; let url = Url::parse("foo://example.com")?; assert_eq!(url.port_or_known_default(), None); let url = Url::parse("foo://example.com:1456")?; assert_eq!(url.port_or_known_default(), Some(1456)); let url = Url::parse("https://example.com")?; assert_eq!(url.port_or_known_default(), Some(443));
pub fn with_default_port<F>(&self, f: F) -> Result<HostAndPort<&str>, Error> where
F: FnOnce(&Url) -> Result<u16, ()>,
[src]
F: FnOnce(&Url) -> Result<u16, ()>,
If the URL has a host, return something that implements ToSocketAddrs
.
If the URL has no port number and the scheme’s default port number is not known
(see Url::port_or_known_default
),
the closure is called to obtain a port number.
Typically, this closure can match on the result Url::scheme
to have per-scheme default port numbers,
and panic for schemes it’s not prepared to handle.
For example:
fn connect(url: &Url) -> io::Result<TcpStream> { TcpStream::connect(url.with_default_port(default_port)?) } fn default_port(url: &Url) -> Result<u16, ()> { match url.scheme() { "git" => Ok(9418), "git+ssh" => Ok(22), "git+https" => Ok(443), "git+http" => Ok(80), _ => Err(()), } }
pub fn path(&self) -> &str
[src]
Return the path for this URL, as a percent-encoded ASCII string. For cannot-be-a-base URLs, this is an arbitrary string that doesn’t start with '/'. For other URLs, this starts with a '/' slash and continues with slash-separated path segments.
Examples
use url::{Url, ParseError}; let url = Url::parse("https://example.com/api/versions?page=2")?; assert_eq!(url.path(), "/api/versions"); let url = Url::parse("https://example.com")?; assert_eq!(url.path(), "/"); let url = Url::parse("https://example.com/countries/việt nam")?; assert_eq!(url.path(), "/countries/vi%E1%BB%87t%20nam");
pub fn path_segments(&self) -> Option<Split<char>>
[src]
Unless this URL is cannot-be-a-base, return an iterator of '/' slash-separated path segments, each as a percent-encoded ASCII string.
Return None
for cannot-be-a-base URLs.
When Some
is returned, the iterator always contains at least one string
(which may be empty).
Examples
use url::Url; let url = Url::parse("https://example.com/foo/bar")?; let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?; assert_eq!(path_segments.next(), Some("foo")); assert_eq!(path_segments.next(), Some("bar")); assert_eq!(path_segments.next(), None); let url = Url::parse("https://example.com")?; let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?; assert_eq!(path_segments.next(), Some("")); assert_eq!(path_segments.next(), None); let url = Url::parse("data:text/plain,HelloWorld")?; assert!(url.path_segments().is_none()); let url = Url::parse("https://example.com/countries/việt nam")?; let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?; assert_eq!(path_segments.next(), Some("countries")); assert_eq!(path_segments.next(), Some("vi%E1%BB%87t%20nam"));
pub fn query(&self) -> Option<&str>
[src]
Return this URL’s query string, if any, as a percent-encoded ASCII string.
Examples
use url::Url; fn run() -> Result<(), ParseError> { let url = Url::parse("https://example.com/products?page=2")?; let query = url.query(); assert_eq!(query, Some("page=2")); let url = Url::parse("https://example.com/products")?; let query = url.query(); assert!(query.is_none()); let url = Url::parse("https://example.com/?country=español")?; let query = url.query(); assert_eq!(query, Some("country=espa%C3%B1ol"));
pub fn query_pairs(&self) -> Parse
[src]
Parse the URL’s query string, if any, as application/x-www-form-urlencoded
and return an iterator of (key, value) pairs.
Examples
use std::borrow::Cow; use url::Url; let url = Url::parse("https://example.com/products?page=2&sort=desc")?; let mut pairs = url.query_pairs(); assert_eq!(pairs.count(), 2); assert_eq!(pairs.next(), Some((Cow::Borrowed("page"), Cow::Borrowed("2")))); assert_eq!(pairs.next(), Some((Cow::Borrowed("sort"), Cow::Borrowed("desc"))));
pub fn fragment(&self) -> Option<&str>
[src]
Return this URL’s fragment identifier, if any.
A fragment is the part of the URL after 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.
Note: the parser did not percent-encode this component, but the input may have been percent-encoded already.
Examples
use url::Url; let url = Url::parse("https://example.com/data.csv#row=4")?; assert_eq!(url.fragment(), Some("row=4")); let url = Url::parse("https://example.com/data.csv#cell=4,1-6,2")?; assert_eq!(url.fragment(), Some("cell=4,1-6,2"));
pub fn to_file_path(&self) -> Result<PathBuf, ()>
[src]
Assuming the URL is in the file
scheme or similar,
convert its path to an absolute std::path::Path
.
Note: This does not actually check the URL’s scheme
,
and may give nonsensical results for other schemes.
It is the user’s responsibility to check the URL’s scheme before calling this.
let path = url.to_file_path();
Returns Err
if the host is neither empty nor "localhost"
(except on Windows, where
file:
URLs may have a non-local host),
or if Path::new_opt()
returns None
.
(That is, if the percent-decoded path contains a NUL byte or,
for a Windows path, is not UTF-8.)
Trait Implementations
impl Eq for Www
[src]
impl PartialEq<Www> for Www
[src]
impl Clone for Www
[src]
fn clone(&self) -> Www
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl Debug for Www
[src]
impl Deref for Www
[src]
impl Serialize for Www
[src]
fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error> where
__S: Serializer,
[src]
__S: Serializer,
impl<'de> Deserialize<'de> for Www
[src]
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
[src]
__D: Deserializer<'de>,
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> DeserializeOwned for T where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,