fluent_uri::component

Struct Authority

source
pub struct Authority<'a, UserinfoE = Userinfo, RegNameE = RegName> { /* private fields */ }
Expand description

An authority component.

Implementations§

source§

impl<'a, UserinfoE: Encoder, RegNameE: Encoder> Authority<'a, UserinfoE, RegNameE>

source

pub const EMPTY: Authority<'static, UserinfoE, RegNameE> = _

An empty authority component.

source

pub fn as_str(&self) -> &'a str

Returns the authority component as a string slice.

§Examples
use fluent_uri::Uri;

let uri = Uri::parse("http://user@example.com:8080/")?;
let auth = uri.authority().unwrap();
assert_eq!(auth.as_str(), "user@example.com:8080");
source

pub fn userinfo(&self) -> Option<&'a EStr<UserinfoE>>

Returns the optional userinfo subcomponent.

§Examples
use fluent_uri::{encoding::EStr, Uri};

let uri = Uri::parse("http://user@example.com/")?;
let auth = uri.authority().unwrap();
assert_eq!(auth.userinfo(), Some(EStr::new_or_panic("user")));

let uri = Uri::parse("http://example.com/")?;
let auth = uri.authority().unwrap();
assert_eq!(auth.userinfo(), None);
source

pub fn host(&self) -> &'a str

Returns the host subcomponent as a string slice.

The host subcomponent is always present, although it may be empty.

The square brackets enclosing an IPv6 or IPvFuture address are included.

Note that ASCII characters within a host are case-insensitive.

§Examples
use fluent_uri::Uri;

let uri = Uri::parse("http://user@example.com:8080/")?;
let auth = uri.authority().unwrap();
assert_eq!(auth.host(), "example.com");

let uri = Uri::parse("file:///path/to/file")?;
let auth = uri.authority().unwrap();
assert_eq!(auth.host(), "");

let uri = Uri::parse("http://[::1]")?;
let auth = uri.authority().unwrap();
assert_eq!(auth.host(), "[::1]");
source

pub fn host_parsed(&self) -> Host<'a, RegNameE>

Returns the parsed host subcomponent.

Note that ASCII characters within a host are case-insensitive.

§Examples
use fluent_uri::{component::Host, encoding::EStr, Uri};
use std::net::{Ipv4Addr, Ipv6Addr};

let uri = Uri::parse("foo://127.0.0.1")?;
let auth = uri.authority().unwrap();
assert!(matches!(auth.host_parsed(), Host::Ipv4(Ipv4Addr::LOCALHOST)));

let uri = Uri::parse("foo://[::1]")?;
let auth = uri.authority().unwrap();
assert!(matches!(auth.host_parsed(), Host::Ipv6(Ipv6Addr::LOCALHOST)));

let uri = Uri::parse("foo://[v1.addr]")?;
let auth = uri.authority().unwrap();
// The API design for IPvFuture addresses is to be determined.
assert!(matches!(auth.host_parsed(), Host::IpvFuture { .. }));

let uri = Uri::parse("foo://localhost")?;
let auth = uri.authority().unwrap();
assert!(matches!(auth.host_parsed(), Host::RegName(name) if name == "localhost"));
source

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

Returns the optional port subcomponent.

A scheme may define a default port to use when the port is not present or is empty.

Note that the port may be empty, with leading zeros, or larger than u16::MAX. It is up to you to decide whether to deny such ports, fallback to the scheme’s default if it is empty, ignore the leading zeros, or use a special addressing mechanism that allows ports larger than u16::MAX.

§Examples
use fluent_uri::{encoding::EStr, Uri};

let uri = Uri::parse("foo://localhost:4673/")?;
let auth = uri.authority().unwrap();
assert_eq!(auth.port(), Some(EStr::new_or_panic("4673")));

let uri = Uri::parse("foo://localhost:/")?;
let auth = uri.authority().unwrap();
assert_eq!(auth.port(), Some(EStr::EMPTY));

let uri = Uri::parse("foo://localhost/")?;
let auth = uri.authority().unwrap();
assert_eq!(auth.port(), None);

let uri = Uri::parse("foo://localhost:123456/")?;
let auth = uri.authority().unwrap();
assert_eq!(auth.port(), Some(EStr::new_or_panic("123456")));
source

pub fn port_to_u16(&self) -> Result<Option<u16>, ParseIntError>

Converts the port subcomponent to u16, if present and nonempty.

Returns Ok(None) if the port is not present or is empty. Leading zeros are ignored.

§Errors

Returns Err if the port cannot be parsed into u16.

§Examples
use fluent_uri::Uri;

let uri = Uri::parse("foo://localhost:4673/")?;
let auth = uri.authority().unwrap();
assert_eq!(auth.port_to_u16(), Ok(Some(4673)));

let uri = Uri::parse("foo://localhost/")?;
let auth = uri.authority().unwrap();
assert_eq!(auth.port_to_u16(), Ok(None));

let uri = Uri::parse("foo://localhost:/")?;
let auth = uri.authority().unwrap();
assert_eq!(auth.port_to_u16(), Ok(None));

let uri = Uri::parse("foo://localhost:123456/")?;
let auth = uri.authority().unwrap();
assert!(auth.port_to_u16().is_err());
source

pub fn socket_addrs( &self, default_port: u16, ) -> Result<impl Iterator<Item = SocketAddr>>

Available on crate features net and std only.

Converts the host and the port subcomponent to an iterator of resolved SocketAddrs.

The default port is used if the port component is not present or is empty. A registered name is first decoded and then resolved with ToSocketAddrs. Punycode encoding is not performed prior to resolution.

§Errors

Returns Err if any of the following is true.

  • The port cannot be parsed into u16.
  • The host is an IPvFuture address.
  • A registered name does not decode to valid UTF-8 or fails to resolve.
source

pub fn has_userinfo(&self) -> bool

Checks whether a userinfo subcomponent is present.

§Examples
use fluent_uri::Uri;

let uri = Uri::parse("http://user@example.com/")?;
assert!(uri.authority().unwrap().has_userinfo());

let uri = Uri::parse("http://example.com/")?;
assert!(!uri.authority().unwrap().has_userinfo());
source

pub fn has_port(&self) -> bool

Checks whether a port subcomponent is present.

§Examples
use fluent_uri::Uri;

let uri = Uri::parse("foo://localhost:4673/")?;
assert!(uri.authority().unwrap().has_port());

// The port subcomponent can be empty.
let uri = Uri::parse("foo://localhost:/")?;
assert!(uri.authority().unwrap().has_port());

let uri = Uri::parse("foo://localhost/")?;
let auth = uri.authority().unwrap();
assert!(!uri.authority().unwrap().has_port());

Trait Implementations§

source§

impl<'a, UserinfoE: Clone, RegNameE: Clone> Clone for Authority<'a, UserinfoE, RegNameE>

source§

fn clone(&self) -> Authority<'a, UserinfoE, RegNameE>

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<UserinfoE: Encoder, RegNameE: Encoder> Debug for Authority<'_, UserinfoE, RegNameE>

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, f: &mut Formatter<'_>) -> Result

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

impl<'a, UserinfoE: Copy, RegNameE: Copy> Copy for Authority<'a, UserinfoE, RegNameE>

Auto Trait Implementations§

§

impl<'a, UserinfoE, RegNameE> Freeze for Authority<'a, UserinfoE, RegNameE>

§

impl<'a, UserinfoE, RegNameE> RefUnwindSafe for Authority<'a, UserinfoE, RegNameE>
where UserinfoE: RefUnwindSafe, RegNameE: RefUnwindSafe,

§

impl<'a, UserinfoE, RegNameE> Send for Authority<'a, UserinfoE, RegNameE>
where UserinfoE: Send, RegNameE: Send,

§

impl<'a, UserinfoE, RegNameE> Sync for Authority<'a, UserinfoE, RegNameE>
where UserinfoE: Sync, RegNameE: Sync,

§

impl<'a, UserinfoE, RegNameE> Unpin for Authority<'a, UserinfoE, RegNameE>
where UserinfoE: Unpin, RegNameE: Unpin,

§

impl<'a, UserinfoE, RegNameE> UnwindSafe for Authority<'a, UserinfoE, RegNameE>
where UserinfoE: UnwindSafe, RegNameE: UnwindSafe,

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

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§

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

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.