Url

Struct Url 

Source
pub struct Url<'url> { /* private fields */ }
Expand description

A parsed URL with support for hostnames, IPv4/IPv6 addresses, userinfo, ports, paths, queries, and fragments.

This struct represents a URL parsed from a string, with all components accessible individually. It supports both ASCII and UTF-8 characters in all components, and properly handles subdomains, custom TLDs, and internationalized domain names (IDNs).

§Examples

use faup_rs::Url;

// Parse a simple URL
let url = Url::parse("https://example.com").unwrap();
assert_eq!(url.scheme(), "https");
assert_eq!(url.host().as_hostname().unwrap().full_name(), "example.com");

// Parse a URL with all components
let url = Url::parse("https://user:pass@sub.example.com:8080/path?query=value#fragment").unwrap();
assert_eq!(url.scheme(), "https");
assert_eq!(url.userinfo().unwrap().username(), "user");
assert_eq!(url.port(), Some(8080));
assert_eq!(url.path(), Some("/path"));
assert_eq!(url.query(), Some("query=value"));
assert_eq!(url.fragment(), Some("fragment"));

Implementations§

Source§

impl<'url> Url<'url>

Source

pub fn into_owned<'owned>(self) -> Url<'owned>

Converts this borrowed Url into an owned Url.

This is useful when you need to store the Url for longer than the lifetime of the input string.

§Performance

When using this method strings will be cloned.

§Returns
  • Url<'owned> - An owned version of the URL.
§Examples
use faup_rs::Url;

let url = Url::parse("https://example.com").unwrap();
let owned_url = url.into_owned();
Source

pub fn parse(s: &'url str) -> Result<Self, Error>

Creates a new Url by parsing a string slice.

§Arguments
  • s - A string slice containing the URL to parse.
§Returns
  • Result<Url, Error> - A parsed Url if successful, or an Error if parsing fails.
§Examples
use faup_rs::Url;

let url = Url::parse("https://example.com").unwrap();
assert_eq!(url.scheme(), "https");
assert_eq!(url.domain(), Some("example.com"));
assert_eq!(url.suffix(), Some("com"));
Source

pub fn as_str(&self) -> &str

Returns the original URL string.

§Returns
  • &str - The original URL string.
§Examples
use faup_rs::Url;

let url = Url::parse("https://example.com").unwrap();
assert_eq!(url.as_str(), "https://example.com");
Source

pub fn scheme(&self) -> &str

Returns the scheme of the URL.

§Returns
  • &str - The URL scheme (e.g., “http”, “https”).
§Examples
use faup_rs::Url;

let url = Url::parse("https://example.com").unwrap();
assert_eq!(url.scheme(), "https");
Source

pub fn userinfo(&self) -> Option<&UserInfo<'_>>

Returns the user information component of the URL, if present.

§Returns
  • Option<&UserInfo> - The user information, or None if not present.
§Examples
use faup_rs::Url;

let url = Url::parse("https://user:pass@example.com").unwrap();
assert_eq!(url.userinfo().unwrap().username(), "user");
assert_eq!(url.userinfo().unwrap().password(), Some("pass"));
Source

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

Returns the host component of the URL.

§Returns
  • &Host - The host, which can be either a hostname or an IP address.
§Examples
use faup_rs::Url;

let url = Url::parse("https://sub2.sub1.example.com").unwrap();
let hostname = url.host().as_hostname().unwrap();
assert_eq!(hostname.full_name(), "sub2.sub1.example.com");
assert_eq!(hostname.domain(), Some("example.com"));
assert_eq!(hostname.suffix(), Some("com"));
assert_eq!(hostname.subdomain(), Some("sub2.sub1"));
Source

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

Returns the domain part of the hostname, if present.

This is a convenience method that directly accesses the domain component of the hostname, if the host is a hostname (not an IP address).

§Returns
  • Option<&str> - The domain part of the hostname, or None if:
    • The host is an IP address
    • The hostname doesn’t have a recognized domain
§Examples
use faup_rs::Url;

// With a domain name
let url = Url::parse("https://sub.example.com").unwrap();
assert_eq!(url.domain(), Some("example.com"));

// With an IP address
let url = Url::parse("https://127.0.0.1").unwrap();
assert_eq!(url.domain(), None);
Source

pub fn subdomain(&self) -> Option<&str>

Returns the subdomain part of the hostname, if present.

This is a convenience method that directly accesses the subdomain component of the hostname, if the host is a hostname (not an IP address).

§Returns
  • Option<&str> - The subdomain part of the hostname, or None if:
    • The host is an IP address
    • The hostname doesn’t have a subdomain
§Examples
use faup_rs::Url;

// With a subdomain
let url = Url::parse("https://sub.example.com").unwrap();
assert_eq!(url.subdomain(), Some("sub"));

// Without a subdomain
let url = Url::parse("https://example.com").unwrap();
assert_eq!(url.subdomain(), None);

// With an IP address
let url = Url::parse("https://127.0.0.1").unwrap();
assert_eq!(url.subdomain(), None);
Source

pub fn suffix(&self) -> Option<&str>

Returns the suffix (top-level domain) of the hostname, if present.

This is a convenience method that directly accesses the suffix component of the hostname, if the host is a hostname (not an IP address).

§Returns
  • Option<&str> - The suffix (TLD) of the hostname, or None if:
    • The host is an IP address
    • The hostname doesn’t have a recognized suffix
§Examples
use faup_rs::Url;

// With a standard TLD
let url = Url::parse("https://example.com").unwrap();
assert_eq!(url.suffix(), Some("com"));

// With a custom TLD
let url = Url::parse("http://example.b32.i2p").unwrap();
assert_eq!(url.suffix(), Some("b32.i2p"));

// With an IP address
let url = Url::parse("https://127.0.0.1").unwrap();
assert_eq!(url.suffix(), None);
Source

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

Returns the port number of the URL, if present.

§Returns
  • Option<u16> - The port number, or None if not specified.
§Examples
use faup_rs::Url;

let url = Url::parse("https://example.com:8080").unwrap();
assert_eq!(url.port(), Some(8080));
Source

pub fn path(&self) -> Option<&str>

Returns the path component of the URL, if present.

§Returns
  • Option<&str> - The path, or None if not present.
§Examples
use faup_rs::Url;

let url = Url::parse("https://example.com/path").unwrap();
assert_eq!(url.path(), Some("/path"));
Source

pub fn query(&self) -> Option<&str>

Returns the query component of the URL, if present.

§Returns
  • Option<&str> - The query string, or None if not present.
§Examples
use faup_rs::Url;

let url = Url::parse("https://example.com?query=value").unwrap();
assert_eq!(url.query(), Some("query=value"));
Source

pub fn fragment(&self) -> Option<&str>

Returns the fragment component of the URL, if present.

§Returns
  • Option<&str> - The fragment, or None if not present.
§Examples
use faup_rs::Url;

let url = Url::parse("https://example.com#fragment").unwrap();
assert_eq!(url.fragment(), Some("fragment"));

Trait Implementations§

Source§

impl<'url> Debug for Url<'url>

Source§

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

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

impl Display for Url<'_>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'url> Freeze for Url<'url>

§

impl<'url> RefUnwindSafe for Url<'url>

§

impl<'url> Send for Url<'url>

§

impl<'url> Sync for Url<'url>

§

impl<'url> Unpin for Url<'url>

§

impl<'url> UnwindSafe for Url<'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> 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> ToString for T
where T: Display + ?Sized,

Source§

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>,

Source§

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>,

Source§

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.