pub struct Uri(/* private fields */);Expand description
Newtype struct around fluent_uri::Uri<String> with serialization implementations that use as_str() and ‘from_str()’ respectively.
Implementations§
Source§impl Uri
Provide methods to Uri to fill blanks left by
fluent_uri (the underlying type) especially when converting to and from file paths.
impl Uri
Provide methods to Uri to fill blanks left by
fluent_uri (the underlying type) especially when converting to and from file paths.
Sourcepub fn to_file_path(&self) -> Option<Cow<'_, Path>>
pub fn to_file_path(&self) -> Option<Cow<'_, Path>>
Assuming the URL is in the file scheme or similar,
convert its path to an absolute std::path::Path.
Note: This does not actually check the URL’s scheme, and may
give nonsensical results for other schemes. It is the user’s
responsibility to check the URL’s scheme before calling this.
e.g. Uri("file:///etc/passwd") becomes PathBuf("/etc/passwd")
Methods from Deref<Target = Uri<String>>§
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(), "");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
selfcontains 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
selfcontains 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/"
);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/");