[go: up one dir, main page]

Struct url::Url

source ·
pub struct Url {
    pub scheme: String,
    pub scheme_data: SchemeData,
    pub query: Option<String>,
    pub fragment: Option<String>,
}
Expand description

The parsed representation of an absolute URL.

Fields§

§scheme: String

The scheme (a.k.a. protocol) of the URL, in ASCII lower case.

§scheme_data: SchemeData

The components of the URL whose representation depends on where the scheme is relative.

§query: Option<String>

The query string of the URL.

None if the ? delimiter character was not part of the parsed input, otherwise a possibly empty, percent-encoded string.

Percent encoded strings are within the ASCII range.

See also the query_pairs, set_query_from_pairs, and lossy_percent_decode_query methods.

§fragment: Option<String>

The fragment identifier of the URL.

None if the # delimiter character was not part of the parsed input, otherwise a possibly empty, percent-encoded string.

Percent encoded strings are within the ASCII range.

See also the lossy_percent_decode_fragment method.

Implementations§

source§

impl Url

source

pub fn parse(input: &str) -> ParseResult<Url>

Parse an URL with the default UrlParser parameters.

In particular, relative URL references are parse errors since no base URL is provided.

source

pub fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Url, ()>

Convert a file name as std::path::Path into an URL in the file scheme.

This returns Err if the given path is not absolute or, with a Windows path, if the prefix is not a disk prefix (e.g. C:).

source

pub fn from_directory_path<P: AsRef<Path>>(path: P) -> Result<Url, ()>

Convert a directory name as std::path::Path into an URL in the file scheme.

This returns Err if the given path is not absolute or, with a Windows path, if the prefix is not a disk prefix (e.g. C:).

Compared to from_file_path, this adds an empty component to the path (or, in terms of URL syntax, adds a trailing slash) so that the entire path is considered when using this URL as a base URL.

For example:

  • "index.html" parsed with Url::from_directory_path(Path::new("/var/www")) as the base URL is file:///var/www/index.html
  • "index.html" parsed with Url::from_file_path(Path::new("/var/www/")) as the base URL is file:///var/index.html, which might not be what was intended.

(Note that Path::new removes any trailing slash.)

source

pub fn to_file_path(&self) -> Result<PathBuf, ()>

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.

The return type (when Ok()) is generic and can be either std::path::posix::Path or std::path::windows::Path. (Use std::path::Path to pick one of them depending on the local system.) If the compiler can not infer the desired type from context, you may have to specify it:

let path = url.to_file_path::<std::path::posix::Path>();

Returns Err if the host is neither empty nor "localhost", 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.)

source

pub fn serialize(&self) -> String

Return the serialization of this URL as a string.

source

pub fn origin(&self) -> Origin

source

pub fn serialize_no_fragment(&self) -> String

Return the serialization of this URL, without the fragment identifier, as a string

source

pub fn non_relative_scheme_data<'a>(&'a self) -> Option<&'a str>

If the URL is non-relative, return the string scheme data.

source

pub fn non_relative_scheme_data_mut<'a>(&'a mut self) -> Option<&'a mut String>

If the URL is non-relative, return a mutable reference to the string scheme data.

source

pub fn relative_scheme_data<'a>(&'a self) -> Option<&'a RelativeSchemeData>

If the URL is in a relative scheme, return the structured scheme data.

source

pub fn relative_scheme_data_mut<'a>( &'a mut self, ) -> Option<&'a mut RelativeSchemeData>

If the URL is in a relative scheme, return a mutable reference to the structured scheme data.

source

pub fn username<'a>(&'a self) -> Option<&'a str>

If the URL is in a relative scheme, return its username.

source

pub fn username_mut<'a>(&'a mut self) -> Option<&'a mut String>

If the URL is in a relative scheme, return a mutable reference to its username.

source

pub fn lossy_percent_decode_username(&self) -> Option<String>

Percent-decode the URL’s username, if any.

This is “lossy”: invalid UTF-8 percent-encoded byte sequences will be replaced � U+FFFD, the replacement character.

source

pub fn password<'a>(&'a self) -> Option<&'a str>

If the URL is in a relative scheme, return its password, if any.

source

pub fn password_mut<'a>(&'a mut self) -> Option<&'a mut String>

If the URL is in a relative scheme, return a mutable reference to its password, if any.

source

pub fn lossy_percent_decode_password(&self) -> Option<String>

Percent-decode the URL’s password, if any.

This is “lossy”: invalid UTF-8 percent-encoded byte sequences will be replaced � U+FFFD, the replacement character.

source

pub fn serialize_userinfo<'a>(&'a mut self) -> Option<String>

Serialize the URL’s username and password, if any.

Format: “:@”

source

pub fn host<'a>(&'a self) -> Option<&'a Host>

If the URL is in a relative scheme, return its structured host.

source

pub fn host_mut<'a>(&'a mut self) -> Option<&'a mut Host>

If the URL is in a relative scheme, return a mutable reference to its structured host.

source

pub fn domain<'a>(&'a self) -> Option<&'a str>

If the URL is in a relative scheme and its host is a domain, return the domain as a string.

source

pub fn domain_mut<'a>(&'a mut self) -> Option<&'a mut String>

If the URL is in a relative scheme and its host is a domain, return a mutable reference to the domain string.

source

pub fn serialize_host(&self) -> Option<String>

If the URL is in a relative scheme, serialize its host as a string.

A domain a returned as-is, an IPv6 address between [] square brackets.

source

pub fn port<'a>(&'a self) -> Option<u16>

If the URL is in a relative scheme and has a port number, return it.

source

pub fn port_mut<'a>(&'a mut self) -> Option<&'a mut Option<u16>>

If the URL is in a relative scheme, return a mutable reference to its port.

source

pub fn port_or_default(&self) -> Option<u16>

If the URL is in a relative scheme that is not a file-like, return its port number, even if it is the default.

source

pub fn path<'a>(&'a self) -> Option<&'a [String]>

If the URL is in a relative scheme, return its path components.

source

pub fn path_mut<'a>(&'a mut self) -> Option<&'a mut Vec<String>>

If the URL is in a relative scheme, return a mutable reference to its path components.

source

pub fn serialize_path(&self) -> Option<String>

If the URL is in a relative scheme, serialize its path as a string.

The returned string starts with a “/” slash, and components are separated by slashes. A trailing slash represents an empty last component.

source

pub fn query_pairs(&self) -> Option<Vec<(String, String)>>

Parse the URL’s query string, if any, as application/x-www-form-urlencoded and return a vector of (key, value) pairs.

source

pub fn set_query_from_pairs<I, K, V>(&mut self, pairs: I)
where I: IntoIterator, I::Item: Borrow<(K, V)>, K: AsRef<str>, V: AsRef<str>,

Serialize an iterator of (key, value) pairs as application/x-www-form-urlencoded and set it as the URL’s query string.

source

pub fn lossy_percent_decode_query(&self) -> Option<String>

Percent-decode the URL’s query string, if any.

This is “lossy”: invalid UTF-8 percent-encoded byte sequences will be replaced � U+FFFD, the replacement character.

source

pub fn lossy_percent_decode_fragment(&self) -> Option<String>

Percent-decode the URL’s fragment identifier, if any.

This is “lossy”: invalid UTF-8 percent-encoded byte sequences will be replaced � U+FFFD, the replacement character.

Trait Implementations§

source§

impl Clone for Url

source§

fn clone(&self) -> Url

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Url

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Decodable for Url

source§

fn decode<D: Decoder>(decoder: &mut D) -> Result<Url, D::Error>

Deserialize a value using a Decoder.
source§

impl Display for Url

source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Encodable for Url

source§

fn encode<S: Encoder>(&self, encoder: &mut S) -> Result<(), S::Error>

Serialize a value using an Encoder.
source§

impl FromStr for Url

§

type Err = ParseError

The associated error which can be returned from parsing.
source§

fn from_str(url: &str) -> ParseResult<Url>

Parses a string s to return a value of this type. Read more
source§

impl Hash for Url

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Url

source§

fn cmp(&self, other: &Url) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Url

source§

fn eq(&self, other: &Url) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Url

source§

fn partial_cmp(&self, other: &Url) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Eq for Url

source§

impl StructuralPartialEq for Url

Auto Trait Implementations§

§

impl Freeze for Url

§

impl RefUnwindSafe for Url

§

impl Send for Url

§

impl Sync for Url

§

impl Unpin for Url

§

impl UnwindSafe for Url

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.