Struct Authority

Source
pub struct Authority<'authority> { /* private fields */ }
Expand description

The authority component as defined in [RFC3986, Section 3.2].

Any conversions to a string will not hide the password component of the authority. Be careful if you decide to perform logging.

Implementations§

Source§

impl<'authority> Authority<'authority>

Source

pub fn as_borrowed(&self) -> Authority<'_>

Source

pub fn from_parts<'new_authority, TUsername, TPassword, THost, TUsernameError, TPasswordError, THostError>( username: Option<TUsername>, password: Option<TPassword>, host: THost, port: Option<u16>, ) -> Result<Authority<'new_authority>, AuthorityError>
where Username<'new_authority>: TryFrom<TUsername, Error = TUsernameError>, Password<'new_authority>: TryFrom<TPassword, Error = TPasswordError>, Host<'new_authority>: TryFrom<THost, Error = THostError>, AuthorityError: From<TUsernameError> + From<TPasswordError> + From<THostError>,

Constructs a new Authority from the individual parts: username, password, host, and port.

The lifetime used by the resulting value will be the lifetime of the part that is most restricted in scope.

§Examples
use std::convert::TryFrom;

use uriparse::Authority;

let authority = Authority::from_parts(
    Some("username"),
    Some("password"),
    "example.com",
    Some(80)
).unwrap();
assert_eq!(authority.to_string(), "username:password@example.com:80");
Source

pub fn has_password(&self) -> bool

Returns whether there is a password in the authority as defined in [RFC3986, Section 3.2.1].

There will only be a password if the URI has a user information component and the component contains the ':' delimiter.

§Examples
use std::convert::TryFrom;

use uriparse::Authority;

let authority = Authority::try_from("username:password@example.com").unwrap();
assert!(authority.has_password());
Source

pub fn has_port(&self) -> bool

Returns whether there is a password in the authority as defined in [RFC3986, Section 3.2.1].

§Examples
use std::convert::TryFrom;

use uriparse::Authority;

let authority = Authority::try_from("example.com:8080").unwrap();
assert!(authority.has_port());
Source

pub fn has_username(&self) -> bool

Returns whether there is a username in the authority as defined in [RFC3986, Section 3.2.1].

There will always be a username as long as there is a '@' delimiter present in the authority.

§Examples
use std::convert::TryFrom;

use uriparse::Authority;

let authority = Authority::try_from("username@example.com").unwrap();
assert!(authority.has_username());
Source

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

The host component of the authority as defined in [RFC3986, Section 3.2.2].

An authority component always has a host, though it may be an empty registered name.

§Examples
use std::convert::TryFrom;

use uriparse::Authority;

let authority = Authority::try_from("username:password@example.com").unwrap();
assert_eq!(authority.host().to_string().as_str(), "example.com");
Source

pub fn into_owned(self) -> Authority<'static>

Converts the Authority into an owned copy.

If you construct the authority from a source with a non-static lifetime, you may run into lifetime problems due to the way the struct is designed. Calling this function will ensure that the returned value has a static lifetime.

This is different from just cloning. Cloning the authority will just copy the eferences, and thus the lifetime will remain the same.

Source

pub fn into_parts( self, ) -> (Option<Username<'authority>>, Option<Password<'authority>>, Host<'authority>, Option<u16>)

Consumes the Authority and returns its parts: username, password, host, and port.

§Examples
use std::convert::TryFrom;

use uriparse::Authority;

let authority = Authority::try_from("username:password@example.com:80").unwrap();
let (username, password, host, port) = authority.into_parts();

assert_eq!(username.unwrap(), "username");
assert_eq!(password.unwrap(), "password");
assert_eq!(host.to_string(), "example.com");
assert_eq!(port.unwrap(), 80);
Source

pub fn is_normalized(&self) -> bool

Returns whether the authority is normalized.

A normalized authority will have all of its sub-components normalized.

This function runs in constant-time.

§Examples
use std::convert::TryFrom;

use uriparse::Authority;

let authority = Authority::try_from("username:password@example.com").unwrap();
assert!(authority.is_normalized());

let mut authority = Authority::try_from("username:p%61ssword@EXAMPLE.COM").unwrap();
assert!(!authority.is_normalized());
authority.normalize();
assert!(authority.is_normalized());
Source

pub fn map_host<TMapper>(&mut self, mapper: TMapper) -> &Host<'authority>
where TMapper: FnOnce(Host<'authority>) -> Host<'authority>,

Maps the host using the given map function.

§Examples
use std::convert::TryFrom;

use uriparse::{Authority, Host};

let mut authority = Authority::try_from("example.com").unwrap();
authority.map_host(|_| Host::try_from("127.0.0.1").unwrap());
assert_eq!(authority.to_string(), "127.0.0.1");
Source

pub fn map_password<TMapper>( &mut self, mapper: TMapper, ) -> Option<&Password<'authority>>
where TMapper: FnOnce(Option<Password<'authority>>) -> Option<Password<'authority>>,

Maps the password using the given map function.

§Examples
use std::convert::TryFrom;

use uriparse::{Authority, Password};

let mut authority = Authority::try_from("example.com").unwrap();
authority.map_password(|_| Some(Password::try_from("password").unwrap()));
assert_eq!(authority.to_string(), ":password@example.com");
Source

pub fn map_port<TMapper>(&mut self, mapper: TMapper) -> Option<u16>
where TMapper: FnOnce(Option<u16>) -> Option<u16>,

Maps the port using the given map function.

§Examples
use std::convert::TryFrom;

use uriparse::Authority;

let mut authority = Authority::try_from("example.com").unwrap();
authority.map_port(|_| Some(8080));
assert_eq!(authority.to_string(), "example.com:8080");
Source

pub fn map_username<TMapper>( &mut self, mapper: TMapper, ) -> Option<&Username<'authority>>
where TMapper: FnOnce(Option<Username<'authority>>) -> Option<Username<'authority>>,

Maps the username using the given map function.

§Examples
use std::convert::TryFrom;

use uriparse::{Authority, Username};

let mut authority = Authority::try_from("example.com").unwrap();
authority.map_username(|_| Some(Username::try_from("username").unwrap()));
assert_eq!(authority.to_string(), "username@example.com");
Source

pub fn normalize(&mut self)

Normalizes the authority.

A normalized authority will have all of its sub-components normalized.

§Examples
use std::convert::TryFrom;

use uriparse::Authority;

let mut authority = Authority::try_from("username:password@example.com").unwrap();
authority.normalize();
assert_eq!(authority.to_string(), "username:password@example.com");

let mut authority = Authority::try_from("username:p%61ssword@EXAMPLE.COM").unwrap();
assert_eq!(authority.to_string(), "username:p%61ssword@EXAMPLE.COM");
authority.normalize();
assert_eq!(authority.to_string(), "username:password@example.com");
Source

pub fn password(&self) -> Option<&Password<'authority>>

The password component of the authority as defined in [RFC3986, Section 3.2.1].

The password will be None if the user information component of the authority did not contain a ':'. Otherwise, it will be whatever is after the ':' until the '@' character. It may be empty as well.

§Examples
use std::convert::TryFrom;

use uriparse::Authority;

let authority = Authority::try_from("username:password@example.com").unwrap();
assert_eq!(authority.password().unwrap(), "password");
Source

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

The port component of the authority as defined in [RFC3986, Section 3.2.3].

The port will be None if a port was not specified.

§Examples
use std::convert::TryFrom;

use uriparse::Authority;

let authority = Authority::try_from("example.com:80").unwrap();
assert_eq!(authority.port().unwrap(), 80);
Source

pub fn set_host<THost, THostError>( &mut self, host: THost, ) -> Result<&Host<'authority>, AuthorityError>
where Host<'authority>: TryFrom<THost, Error = THostError>, AuthorityError: From<THostError>,

Sets the host of the authority.

An error will be returned if the conversion to a Host fails.

§Examples
use std::convert::TryFrom;
use std::net::Ipv6Addr;

use uriparse::{Authority, Host};

let mut authority = Authority::try_from("example.com:8080").unwrap();
authority.set_host("127.0.0.1");
assert_eq!(authority.to_string(), "127.0.0.1:8080");
authority.set_host(Host::IPv6Address("::1".parse().unwrap()));
assert_eq!(authority.to_string(), "[::1]:8080");
Source

pub fn set_password<TPassword, TPasswordError>( &mut self, password: Option<TPassword>, ) -> Result<Option<&Password<'authority>>, AuthorityError>
where Password<'authority>: TryFrom<TPassword, Error = TPasswordError>, AuthorityError: From<TPasswordError>,

Sets the password of the authority.

An error will be returned if the conversion to a Password fails.

If the given password is not None, then the username will be set to "" if it is currently not set.

§Examples
use std::convert::TryFrom;

use uriparse::Authority;

let mut authority = Authority::try_from("example.com").unwrap();
authority.set_password(Some("secret"));
assert_eq!(authority.to_string(), ":secret@example.com");
Source

pub fn set_port(&mut self, port: Option<u16>) -> Option<u16>

Sets the port of the authority.

§Examples
use std::convert::TryFrom;

use uriparse::Authority;

let mut authority = Authority::try_from("example.com").unwrap();
authority.set_port(Some(8080));
assert_eq!(authority.to_string(), "example.com:8080");
Source

pub fn set_username<TUsername, TUsernameError>( &mut self, username: Option<TUsername>, ) -> Result<Option<&Username<'authority>>, AuthorityError>
where Username<'authority>: TryFrom<TUsername, Error = TUsernameError>, AuthorityError: From<TUsernameError>,

Sets the username of the authority.

An error will be returned if the conversion to a Username fails.

If the given username is None, this will also remove any set password.

§Examples
use std::convert::TryFrom;

use uriparse::{Authority, Username};

let mut authority = Authority::try_from("example.com").unwrap();
authority.set_username(Some("myname"));
assert_eq!(authority.to_string(), "myname@example.com");

let mut authority = Authority::try_from("user:pass@example.com").unwrap();
authority.set_username(None::<Username>);
assert_eq!(authority.to_string(), "example.com");
Source

pub fn username(&self) -> Option<&Username<'authority>>

The username component of the authority as defined in [RFC3986, Section 3.2.1].

The username will be None if the user information component of the authority did not contain a ':'. Otherwise, it will be whatever is after the ':' until the '@' character. It may be empty as well.

§Examples
use std::convert::TryFrom;

use uriparse::Authority;

let authority = Authority::try_from("username:password@example.com").unwrap();
assert_eq!(authority.password().unwrap(), "password");

Trait Implementations§

Source§

impl<'authority> Clone for Authority<'authority>

Source§

fn clone(&self) -> Authority<'authority>

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<'authority> Debug for Authority<'authority>

Source§

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

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

impl Display for Authority<'_>

Source§

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

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

impl<'authority> From<Authority<'authority>> for String

Source§

fn from(value: Authority<'authority>) -> String

Converts to this type from the input type.
Source§

impl<'authority> Hash for Authority<'authority>

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<'authority> PartialEq for Authority<'authority>

Source§

fn eq(&self, other: &Authority<'authority>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'authority> TryFrom<&'authority [u8]> for Authority<'authority>

Source§

type Error = AuthorityError

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

fn try_from(value: &'authority [u8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'authority> TryFrom<&'authority str> for Authority<'authority>

Source§

type Error = AuthorityError

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

fn try_from(value: &'authority str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'authority> Eq for Authority<'authority>

Source§

impl<'authority> StructuralPartialEq for Authority<'authority>

Auto Trait Implementations§

§

impl<'authority> Freeze for Authority<'authority>

§

impl<'authority> RefUnwindSafe for Authority<'authority>

§

impl<'authority> Send for Authority<'authority>

§

impl<'authority> Sync for Authority<'authority>

§

impl<'authority> Unpin for Authority<'authority>

§

impl<'authority> UnwindSafe for Authority<'authority>

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§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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§

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.