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
Uri
s
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},
encoding::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.strip_input())?;
// 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>
Sourcepub fn parse<I>(input: I) -> Result<Uri<T>, <I as Parse>::Err>where
I: Parse<Val = T>,
pub fn parse<I>(input: I) -> Result<Uri<T>, <I as Parse>::Err>where
I: Parse<Val = T>,
Parses a URI from a string into a Uri
.
The return type is
Result<Uri<&str>, ParseError>
forI = &str
;Result<Uri<String>, ParseError<String>>
forI = String
.
§Errors
Returns Err
if the string does not match the
URI
ABNF rule from RFC 3986.
From a ParseError<String>
, you may recover or strip the input
by calling into_input
or strip_input
on it.
Source§impl<'i, 'o, T> Uri<T>where
T: BorrowOrShare<'i, 'o, str>,
impl<'i, 'o, T> Uri<T>where
T: BorrowOrShare<'i, 'o, str>,
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<'i, 'o, T> Uri<T>
impl<'i, 'o, T> Uri<T>
Sourcepub fn normalize(&self) -> Uri<String>
pub fn normalize(&self) -> Uri<String>
Normalizes the URI.
This method applies the syntax-based normalization described in Section 6.2.2 of RFC 3986 and Section 5.3.2 of RFC 3987, which 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, remove its
':'
delimiter. - If
self
contains a scheme and an absolute path, apply theremove_dot_segments
algorithm to the path, taking account of percent-encoded dot segments as described atUriRef::resolve_against
. - If
self
contains no authority and its path would start with"//"
, prepend"/."
to the path.
This method is idempotent: self.normalize()
equals self.normalize().normalize()
.
§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 with_fragment(&self, opt: Option<&EStr<Fragment>>) -> Uri<String>
pub fn with_fragment(&self, opt: Option<&EStr<Fragment>>) -> Uri<String>
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::{encoding::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>>)
pub fn set_fragment(&mut self, opt: Option<&EStr<Fragment>>)
Replaces the fragment component of self
with the given one.
The fragment component is removed when opt.is_none()
.
§Examples
use fluent_uri::{encoding::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>
impl<'de> Deserialize<'de> for Uri<&'de str>
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Uri<&'de str>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Uri<&'de str>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl<'de> Deserialize<'de> for Uri<String>
impl<'de> Deserialize<'de> for Uri<String>
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Uri<String>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Uri<String>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl<T> Ord for Uri<T>
impl<T> Ord for Uri<T>
Source§impl<T> PartialOrd for Uri<T>
impl<T> PartialOrd for Uri<T>
Source§impl<T> Serialize for Uri<T>
impl<T> Serialize for Uri<T>
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl<T> Copy for Uri<T>where
T: Copy,
impl<T> Eq for Uri<T>
Auto Trait Implementations§
impl<T> Freeze for Uri<T>where
T: Freeze,
impl<T> RefUnwindSafe for Uri<T>where
T: RefUnwindSafe,
impl<T> Send for Uri<T>where
T: Send,
impl<T> Sync for Uri<T>where
T: Sync,
impl<T> Unpin for Uri<T>where
T: Unpin,
impl<T> UnwindSafe for Uri<T>where
T: 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
)