pub struct Uri<T> { /* private fields */ }Expand description
A URI.
See the crate-level documentation for an explanation of the above term(s).
§Variants
Two variants of Uri are available:
Uri<&str> (borrowed) and Uri<String> (owned).
Uri<&'a str>
outputs references with lifetime 'a where possible
(thanks to borrow-or-share):
use fluent_uri::Uri;
// Keep a reference to the path after dropping the `Uri`.
let path = Uri::parse("foo:bar")?.path();
assert_eq!(path, "bar");§Comparison
Uris
are compared lexicographically
by their byte values. Normalization is not performed prior to comparison.
§Examples
Parse and extract components from a URI:
use fluent_uri::{
component::{Host, Scheme},
pct_enc::EStr,
Uri,
};
const SCHEME_FOO: &Scheme = Scheme::new_or_panic("foo");
let s = "foo://user@example.com:8042/over/there?name=ferret#nose";
let uri = Uri::parse(s)?;
assert_eq!(uri.scheme(), SCHEME_FOO);
let auth = uri.authority().unwrap();
assert_eq!(auth.as_str(), "user@example.com:8042");
assert_eq!(auth.userinfo().unwrap(), "user");
assert_eq!(auth.host(), "example.com");
assert!(matches!(auth.host_parsed(), Host::RegName(name) if name == "example.com"));
assert_eq!(auth.port().unwrap(), "8042");
assert_eq!(auth.port_to_u16(), Ok(Some(8042)));
assert_eq!(uri.path(), "/over/there");
assert_eq!(uri.query().unwrap(), "name=ferret");
assert_eq!(uri.fragment().unwrap(), "nose");Parse into and convert between
Uri<&str> and Uri<String>:
use fluent_uri::Uri;
let s = "http://example.com/";
// Parse into a `Uri<&str>` from a string slice.
let uri: Uri<&str> = Uri::parse(s)?;
// Parse into a `Uri<String>` from an owned string.
let uri_owned: Uri<String> = Uri::parse(s.to_owned()).map_err(|e| e.0)?;
// Convert a `Uri<&str>` to `Uri<String>`.
let uri_owned: Uri<String> = uri.to_owned();
// Borrow a `Uri<String>` as `Uri<&str>`.
let uri: Uri<&str> = uri_owned.borrow();Implementations§
Source§impl<T> Uri<T>
impl<T> Uri<T>
Source§impl Uri<String>
impl Uri<String>
Sourcepub fn builder() -> Builder<Self, NonRefStart>
Available on crate feature alloc only.
pub fn builder() -> Builder<Self, NonRefStart>
alloc only.Creates a new builder for URI.
Sourcepub fn borrow(&self) -> Uri<&str>
Available on crate feature alloc only.
pub fn borrow(&self) -> Uri<&str>
alloc only.Borrows this Uri<String> as Uri<&str>.
Sourcepub fn into_string(self) -> String
Available on crate feature alloc only.
pub fn into_string(self) -> String
alloc only.Consumes this Uri<String> and yields the underlying String.
Source§impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Uri<T>
impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Uri<T>
Sourcepub fn scheme(&'i self) -> &'o Scheme
pub fn scheme(&'i self) -> &'o Scheme
Returns the scheme component.
Note that the scheme component is case-insensitive.
See the documentation of Scheme for more details on comparison.
§Examples
use fluent_uri::{component::Scheme, Uri};
const SCHEME_HTTP: &Scheme = Scheme::new_or_panic("http");
let uri = Uri::parse("http://example.com/")?;
assert_eq!(uri.scheme(), SCHEME_HTTP);Sourcepub fn path(&'i self) -> &'o EStr<Path>
pub fn path(&'i self) -> &'o EStr<Path>
Returns the path component.
The path component is always present, although it may be empty.
The returned EStr slice has extension methods for the path component.
§Examples
use fluent_uri::Uri;
let uri = Uri::parse("http://example.com/")?;
assert_eq!(uri.path(), "/");
let uri = Uri::parse("mailto:user@example.com")?;
assert_eq!(uri.path(), "user@example.com");
let uri = Uri::parse("http://example.com")?;
assert_eq!(uri.path(), "");Source§impl<T: Bos<str>> Uri<T>
impl<T: Bos<str>> Uri<T>
Sourcepub fn normalize(&self) -> Uri<String>
Available on crate feature alloc only.
pub fn normalize(&self) -> Uri<String>
alloc only.Normalizes the URI.
This method applies syntax-based normalization described in Section 6.2.2 of RFC 3986 and Section 5.3.2 of RFC 3987, along with IPv6 address and default port normalization. This is effectively equivalent to taking the following steps in order:
- Decode any percent-encoded octets that correspond to an allowed character which is not reserved.
- Uppercase the hexadecimal digits within all percent-encoded octets.
- Lowercase all ASCII characters within the scheme and the host except the percent-encoded octets.
- Turn any IPv6 literal address into its canonical form as per RFC 5952.
- If the port is empty or equals the scheme’s default, remove it along with the
':'delimiter. - If
selfhas a scheme and an absolute path, apply theremove_dot_segmentsalgorithm to the path, taking account of percent-encoded dot segments as described atUriRef::resolve_against. - If
selfhas no authority and its path would start with"//", prepend"/."to the path.
This method is idempotent: self.normalize() equals self.normalize().normalize().
If you need to configure the behavior of normalization, consider using Normalizer instead.
§Examples
use fluent_uri::Uri;
let uri = Uri::parse("eXAMPLE://a/./b/../b/%63/%7bfoo%7d")?;
assert_eq!(uri.normalize(), "example://a/b/c/%7Bfoo%7D");Checks whether an authority component is present.
§Examples
use fluent_uri::Uri;
assert!(Uri::parse("http://example.com/")?.has_authority());
assert!(!Uri::parse("mailto:user@example.com")?.has_authority());Sourcepub fn has_query(&self) -> bool
pub fn has_query(&self) -> bool
Checks whether a query component is present.
§Examples
use fluent_uri::Uri;
assert!(Uri::parse("http://example.com/?lang=en")?.has_query());
assert!(!Uri::parse("ftp://192.0.2.1/")?.has_query());Sourcepub fn has_fragment(&self) -> bool
pub fn has_fragment(&self) -> bool
Checks whether a fragment component is present.
§Examples
use fluent_uri::Uri;
assert!(Uri::parse("http://example.com/#usage")?.has_fragment());
assert!(!Uri::parse("ftp://192.0.2.1/")?.has_fragment());Sourcepub fn strip_fragment(&self) -> Uri<&str>
pub fn strip_fragment(&self) -> Uri<&str>
Returns a slice of this URI with the fragment component removed.
§Examples
use fluent_uri::Uri;
let uri = Uri::parse("http://example.com/#fragment")?;
assert_eq!(uri.strip_fragment(), "http://example.com/");Sourcepub fn with_fragment(&self, opt: Option<&EStr<Fragment>>) -> Uri<String>
Available on crate feature alloc only.
pub fn with_fragment(&self, opt: Option<&EStr<Fragment>>) -> Uri<String>
alloc only.Creates a new URI
by replacing the fragment component of self with the given one.
The fragment component is removed when opt.is_none().
§Examples
use fluent_uri::{pct_enc::EStr, Uri};
let uri = Uri::parse("http://example.com/")?;
assert_eq!(
uri.with_fragment(Some(EStr::new_or_panic("fragment"))),
"http://example.com/#fragment"
);
let uri = Uri::parse("http://example.com/#fragment")?;
assert_eq!(uri.with_fragment(None), "http://example.com/");Source§impl Uri<String>
impl Uri<String>
Sourcepub fn set_fragment(&mut self, opt: Option<&EStr<Fragment>>)
Available on crate feature alloc only.
pub fn set_fragment(&mut self, opt: Option<&EStr<Fragment>>)
alloc only.Replaces the fragment component of self with the given one.
The fragment component is removed when opt.is_none().
§Examples
use fluent_uri::{pct_enc::EStr, Uri};
let mut uri = Uri::parse("http://example.com/")?.to_owned();
uri.set_fragment(Some(EStr::new_or_panic("fragment")));
assert_eq!(uri, "http://example.com/#fragment");
uri.set_fragment(None);
assert_eq!(uri, "http://example.com/");Trait Implementations§
Source§impl<'de> Deserialize<'de> for Uri<&'de str>
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Uri<&'de str>
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<'de> Deserialize<'de> for Uri<String>
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Uri<String>
serde only.