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>
impl<'a, UserinfoE: Encoder, RegNameE: Encoder> Authority<'a, UserinfoE, RegNameE>
sourcepub fn as_str(&self) -> &'a str
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");
sourcepub fn userinfo(&self) -> Option<&'a EStr<UserinfoE>>
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);
sourcepub fn host(&self) -> &'a str
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]");
sourcepub fn host_parsed(&self) -> Host<'a, RegNameE>
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"));
sourcepub fn port(&self) -> Option<&'a EStr<Port>>
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")));
sourcepub fn port_to_u16(&self) -> Result<Option<u16>, ParseIntError>
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());
sourcepub fn socket_addrs(
&self,
default_port: u16,
) -> Result<impl Iterator<Item = SocketAddr>>
Available on crate features net
and std
only.
pub fn socket_addrs( &self, default_port: u16, ) -> Result<impl Iterator<Item = SocketAddr>>
net
and std
only.Converts the host and the port subcomponent to an iterator of resolved SocketAddr
s.
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.
sourcepub fn has_userinfo(&self) -> bool
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());
sourcepub fn has_port(&self) -> bool
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§
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>
impl<'a, UserinfoE, RegNameE> Sync for Authority<'a, UserinfoE, RegNameE>
impl<'a, UserinfoE, RegNameE> Unpin for Authority<'a, UserinfoE, RegNameE>
impl<'a, UserinfoE, RegNameE> UnwindSafe for Authority<'a, UserinfoE, RegNameE>where
UserinfoE: UnwindSafe,
RegNameE: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)