pub struct RiStr<S> { /* private fields */ }
Expand description
A borrowed string of an absolute IRI possibly with fragment part.
This corresponds to IRI
rule in RFC 3987 (and URI
rule in RFC 3986).
The rule for IRI
is scheme ":" ihier-part [ "?" iquery ] [ "#" ifragment ]
.
In other words, this is RiAbsoluteStr
with fragment part allowed.
§Valid values
This type can have an IRI (which is absolute, and may have fragment part).
assert!(IriStr::new("https://user:pass@example.com:8080").is_ok());
assert!(IriStr::new("https://example.com/").is_ok());
assert!(IriStr::new("https://example.com/foo?bar=baz").is_ok());
assert!(IriStr::new("https://example.com/foo?bar=baz#qux").is_ok());
assert!(IriStr::new("foo:bar").is_ok());
assert!(IriStr::new("foo:").is_ok());
// `foo://.../` below are all allowed. See the crate documentation for detail.
assert!(IriStr::new("foo:/").is_ok());
assert!(IriStr::new("foo://").is_ok());
assert!(IriStr::new("foo:///").is_ok());
assert!(IriStr::new("foo:////").is_ok());
assert!(IriStr::new("foo://///").is_ok());
Relative IRI reference is not allowed.
// This is relative path.
assert!(IriStr::new("foo/bar").is_err());
// `/foo/bar` is an absolute path, but it is authority-relative.
assert!(IriStr::new("/foo/bar").is_err());
// `//foo/bar` is termed "network-path reference",
// or usually called "protocol-relative reference".
assert!(IriStr::new("//foo/bar").is_err());
// Same-document reference is relative.
assert!(IriStr::new("#foo").is_err());
// Empty string is not a valid absolute IRI.
assert!(IriStr::new("").is_err());
Some characters and sequences cannot used in an IRI.
// `<` and `>` cannot directly appear in an IRI.
assert!(IriStr::new("<not allowed>").is_err());
// Broken percent encoding cannot appear in an IRI.
assert!(IriStr::new("%").is_err());
assert!(IriStr::new("%GG").is_err());
Implementations§
source§impl<S: Spec> RiStr<S>
impl<S: Spec> RiStr<S>
sourcepub unsafe fn new_unchecked(s: &str) -> &Self
pub unsafe fn new_unchecked(s: &str) -> &Self
Creates a new string without validation.
This does not validate the given string, so it is caller’s responsibility to ensure the given string is valid.
§Safety
The given string must be syntactically valid as Self
type.
If not, any use of the returned value or the call of this
function itself may result in undefined behavior.
source§impl<S: Spec> RiStr<S>
impl<S: Spec> RiStr<S>
sourcepub fn to_absolute_and_fragment(
&self,
) -> (&RiAbsoluteStr<S>, Option<&RiFragmentStr<S>>)
pub fn to_absolute_and_fragment( &self, ) -> (&RiAbsoluteStr<S>, Option<&RiFragmentStr<S>>)
Splits the IRI into an absolute IRI part and a fragment part.
A leading #
character is truncated if the fragment part exists.
§Examples
If the IRI has a fragment part, Some(_)
is returned.
let iri = IriStr::new("foo://bar/baz?qux=quux#corge")?;
let (absolute, fragment) = iri.to_absolute_and_fragment();
let fragment_expected = IriFragmentStr::new("corge")?;
assert_eq!(absolute, "foo://bar/baz?qux=quux");
assert_eq!(fragment, Some(fragment_expected));
When the fragment part exists but is empty string, Some(_)
is returned.
let iri = IriStr::new("foo://bar/baz?qux=quux#")?;
let (absolute, fragment) = iri.to_absolute_and_fragment();
let fragment_expected = IriFragmentStr::new("")?;
assert_eq!(absolute, "foo://bar/baz?qux=quux");
assert_eq!(fragment, Some(fragment_expected));
If the IRI has no fragment, None
is returned.
let iri = IriStr::new("foo://bar/baz?qux=quux")?;
let (absolute, fragment) = iri.to_absolute_and_fragment();
assert_eq!(absolute, "foo://bar/baz?qux=quux");
assert_eq!(fragment, None);
sourcepub fn to_absolute(&self) -> &RiAbsoluteStr<S>
pub fn to_absolute(&self) -> &RiAbsoluteStr<S>
Strips the fragment part if exists, and returns &RiAbsoluteStr
.
§Examples
let iri = IriStr::new("foo://bar/baz?qux=quux#corge")?;
assert_eq!(iri.to_absolute(), "foo://bar/baz?qux=quux");
let iri = IriStr::new("foo://bar/baz?qux=quux")?;
assert_eq!(iri.to_absolute(), "foo://bar/baz?qux=quux");
sourcepub fn ensure_rfc3986_normalizable(&self) -> Result<(), Error>
pub fn ensure_rfc3986_normalizable(&self) -> Result<(), Error>
Returns Ok(())
if the IRI is normalizable by the RFC 3986 algorithm.
§Examples
use iri_string::types::IriStr;
let iri = IriStr::new("HTTP://example.COM/foo/%2e/bar/..")?;
assert!(iri.ensure_rfc3986_normalizable().is_ok());
let iri2 = IriStr::new("scheme:/..//bar")?;
// The normalization result would be `scheme://bar` according to RFC
// 3986, but it is unintended and should be treated as a failure.
// This crate automatically handles this case so that `.normalize()` won't fail.
assert!(!iri.ensure_rfc3986_normalizable().is_err());
sourcepub fn is_normalized(&self) -> bool
pub fn is_normalized(&self) -> bool
Returns true
if the IRI is already normalized.
This returns the same result as self.normalize().to_string() == self
,
but does this more efficiently without heap allocation.
§Examples
use iri_string::format::ToDedicatedString;
use iri_string::types::IriStr;
let iri = IriStr::new("HTTP://example.COM/foo/./bar/%2e%2e/../baz?query#fragment")?;
assert!(!iri.is_normalized());
let normalized = iri.normalize().to_dedicated_string();
assert_eq!(normalized, "http://example.com/baz?query#fragment");
assert!(normalized.is_normalized());
use iri_string::format::ToDedicatedString;
use iri_string::types::IriStr;
let iri = IriStr::new("scheme:/.///foo")?;
// Already normalized.
assert!(iri.is_normalized());
use iri_string::format::ToDedicatedString;
use iri_string::types::IriStr;
let iri = IriStr::new("scheme:relative/..//not-a-host")?;
// Default normalization algorithm assumes the path part to be NOT opaque.
assert!(!iri.is_normalized());
let normalized = iri.normalize().to_dedicated_string();
assert_eq!(normalized, "scheme:/.//not-a-host");
sourcepub fn is_normalized_rfc3986(&self) -> bool
pub fn is_normalized_rfc3986(&self) -> bool
Returns true
if the IRI is already normalized in the sense of RFC 3986.
This returns the same result as
self.ensure_rfc3986_normalizable() && (self.normalize().to_string() == self)
,
but does this more efficiently without heap allocation.
§Examples
use iri_string::format::ToDedicatedString;
use iri_string::types::IriStr;
let iri = IriStr::new("HTTP://example.COM/foo/./bar/%2e%2e/../baz?query#fragment")?;
assert!(!iri.is_normalized_rfc3986());
let normalized = iri.normalize().to_dedicated_string();
assert_eq!(normalized, "http://example.com/baz?query#fragment");
assert!(normalized.is_normalized_rfc3986());
use iri_string::format::ToDedicatedString;
use iri_string::types::IriStr;
let iri = IriStr::new("scheme:/.///foo")?;
// Not normalized in the sense of RFC 3986.
assert!(!iri.is_normalized_rfc3986());
use iri_string::format::ToDedicatedString;
use iri_string::types::IriStr;
let iri = IriStr::new("scheme:relative/..//not-a-host")?;
// RFC 3986 normalization algorithm assumes the path part to be NOT opaque.
assert!(!iri.is_normalized_rfc3986());
let normalized = iri.normalize().to_dedicated_string();
assert_eq!(normalized, "scheme:/.//not-a-host");
Returns true
if the IRI is already normalized in the sense of
normalize_but_preserve_authorityless_relative_path
method.
This returns the same result as
self.normalize_but_preserve_authorityless_relative_path().to_string() == self
,
but does this more efficiently without heap allocation.
§Examples
use iri_string::format::ToDedicatedString;
use iri_string::types::IriStr;
let iri = IriStr::new("HTTP://example.COM/foo/./bar/%2e%2e/../baz?query#fragment")?;
assert!(!iri.is_normalized_but_authorityless_relative_path_preserved());
let normalized = iri
.normalize_but_preserve_authorityless_relative_path()
.to_dedicated_string();
assert_eq!(normalized, "http://example.com/baz?query#fragment");
assert!(normalized.is_normalized());
use iri_string::format::ToDedicatedString;
use iri_string::types::IriStr;
let iri = IriStr::new("scheme:/.///foo")?;
// Already normalized in the sense of
// `normalize_but_opaque_authorityless_relative_path()` method.
assert!(iri.is_normalized_but_authorityless_relative_path_preserved());
use iri_string::format::ToDedicatedString;
use iri_string::types::IriStr;
let iri = IriStr::new("scheme:relative/..//not-a-host")?;
// Relative path is treated as opaque since the autority component is absent.
assert!(iri.is_normalized_but_authorityless_relative_path_preserved());
sourcepub fn normalize(&self) -> Normalized<'_, Self>
pub fn normalize(&self) -> Normalized<'_, Self>
Returns the normalized IRI.
§Notes
For some abnormal IRIs, the normalization can produce semantically incorrect string that looks syntactically valid. To avoid security issues by this trap, the normalization algorithm by this crate automatically applies the workaround.
If you worry about this, test by RiStr::ensure_rfc3986_normalizable
method or Normalized::ensure_rfc3986_normalizable
before using the
result string.
§Examples
use iri_string::format::ToDedicatedString;
use iri_string::types::IriStr;
let iri = IriStr::new("HTTP://example.COM/foo/./bar/%2e%2e/../baz?query#fragment")?;
let normalized = iri.normalize().to_dedicated_string();
assert_eq!(normalized, "http://example.com/baz?query#fragment");
Returns the normalized IRI, but preserving dot segments in relative path if the authority component is absent.
This normalization would be similar to that of WHATWG URL Standard while this implementation is not guaranteed to stricly follow the spec.
Note that this normalization algorithm is not compatible with RFC 3986 algorithm for some inputs.
Note that case normalization and percent-encoding normalization will still be applied to any path.
§Examples
use iri_string::format::ToDedicatedString;
use iri_string::types::IriStr;
let iri = IriStr::new("HTTP://example.COM/foo/./bar/%2e%2e/../baz?query#fragment")?;
let normalized = iri
.normalize_but_preserve_authorityless_relative_path()
.to_dedicated_string();
assert_eq!(normalized, "http://example.com/baz?query#fragment");
use iri_string::format::ToDedicatedString;
use iri_string::types::IriStr;
let iri = IriStr::new("scheme:relative/../f%6f%6f")?;
let normalized = iri
.normalize_but_preserve_authorityless_relative_path()
.to_dedicated_string();
assert_eq!(normalized, "scheme:relative/../foo");
// `.normalize()` would normalize this to `scheme:/foo`.
sourcepub fn mask_password(&self) -> PasswordMasked<'_, Self>
pub fn mask_password(&self) -> PasswordMasked<'_, Self>
Returns the proxy to the IRI with password masking feature.
§Examples
use iri_string::format::ToDedicatedString;
use iri_string::types::IriStr;
let iri = IriStr::new("http://user:password@example.com/path?query")?;
let masked = iri.mask_password();
assert_eq!(masked.to_dedicated_string(), "http://user:@example.com/path?query");
assert_eq!(
masked.replace_password("${password}").to_string(),
"http://user:${password}@example.com/path?query"
);
source§impl<S: Spec> RiStr<S>
impl<S: Spec> RiStr<S>
Components getters.
sourcepub fn scheme_str(&self) -> &str
pub fn scheme_str(&self) -> &str
Returns the scheme.
The following colon is truncated.
§Examples
use iri_string::types::IriStr;
let iri = IriStr::new("http://example.com/pathpath?queryquery#fragfrag")?;
assert_eq!(iri.scheme_str(), "http");
Returns the authority.
The leading //
is truncated.
§Examples
use iri_string::types::IriStr;
let iri = IriStr::new("http://example.com/pathpath?queryquery#fragfrag")?;
assert_eq!(iri.authority_str(), Some("example.com"));
use iri_string::types::IriStr;
let iri = IriStr::new("urn:uuid:10db315b-fcd1-4428-aca8-15babc9a2da2")?;
assert_eq!(iri.authority_str(), None);
sourcepub fn path_str(&self) -> &str
pub fn path_str(&self) -> &str
Returns the path.
§Examples
use iri_string::types::IriStr;
let iri = IriStr::new("http://example.com/pathpath?queryquery#fragfrag")?;
assert_eq!(iri.path_str(), "/pathpath");
use iri_string::types::IriStr;
let iri = IriStr::new("urn:uuid:10db315b-fcd1-4428-aca8-15babc9a2da2")?;
assert_eq!(iri.path_str(), "uuid:10db315b-fcd1-4428-aca8-15babc9a2da2");
sourcepub fn query(&self) -> Option<&RiQueryStr<S>>
pub fn query(&self) -> Option<&RiQueryStr<S>>
Returns the query.
The leading question mark (?
) is truncated.
§Examples
use iri_string::types::{IriQueryStr, IriStr};
let iri = IriStr::new("http://example.com/pathpath?queryquery#fragfrag")?;
let query = IriQueryStr::new("queryquery")?;
assert_eq!(iri.query(), Some(query));
use iri_string::types::IriStr;
let iri = IriStr::new("urn:uuid:10db315b-fcd1-4428-aca8-15babc9a2da2")?;
assert_eq!(iri.query(), None);
sourcepub fn query_str(&self) -> Option<&str>
pub fn query_str(&self) -> Option<&str>
Returns the query in a raw string slice.
The leading question mark (?
) is truncated.
§Examples
use iri_string::types::IriStr;
let iri = IriStr::new("http://example.com/pathpath?queryquery#fragfrag")?;
assert_eq!(iri.query_str(), Some("queryquery"));
use iri_string::types::IriStr;
let iri = IriStr::new("urn:uuid:10db315b-fcd1-4428-aca8-15babc9a2da2")?;
assert_eq!(iri.query_str(), None);
sourcepub fn fragment(&self) -> Option<&RiFragmentStr<S>>
pub fn fragment(&self) -> Option<&RiFragmentStr<S>>
Returns the fragment part if exists.
A leading #
character is truncated if the fragment part exists.
§Examples
let iri = IriStr::new("foo://bar/baz?qux=quux#corge")?;
let fragment = IriFragmentStr::new("corge")?;
assert_eq!(iri.fragment(), Some(fragment));
let iri = IriStr::new("foo://bar/baz?qux=quux#")?;
let fragment = IriFragmentStr::new("")?;
assert_eq!(iri.fragment(), Some(fragment));
let iri = IriStr::new("foo://bar/baz?qux=quux")?;
assert_eq!(iri.fragment(), None);
sourcepub fn fragment_str(&self) -> Option<&str>
pub fn fragment_str(&self) -> Option<&str>
Returns the fragment part as a raw string slice if exists.
A leading #
character is truncated if the fragment part exists.
§Examples
let iri = IriStr::new("foo://bar/baz?qux=quux#corge")?;
assert_eq!(iri.fragment_str(), Some("corge"));
let iri = IriStr::new("foo://bar/baz?qux=quux#")?;
assert_eq!(iri.fragment_str(), Some(""));
let iri = IriStr::new("foo://bar/baz?qux=quux")?;
assert_eq!(iri.fragment_str(), None);
Returns the authority components.
§Examples
use iri_string::types::IriStr;
let iri = IriStr::new("http://user:pass@example.com:8080/pathpath?queryquery")?;
let authority = iri.authority_components()
.expect("authority is available");
assert_eq!(authority.userinfo(), Some("user:pass"));
assert_eq!(authority.host(), "example.com");
assert_eq!(authority.port(), Some("8080"));
use iri_string::types::IriStr;
let iri = IriStr::new("urn:uuid:10db315b-fcd1-4428-aca8-15babc9a2da2")?;
assert_eq!(iri.authority_str(), None);
source§impl RiStr<IriSpec>
impl RiStr<IriSpec>
Conversion from an IRI into a URI.
sourcepub fn encode_to_uri(&self) -> MappedToUri<'_, Self>
pub fn encode_to_uri(&self) -> MappedToUri<'_, Self>
Percent-encodes the IRI into a valid URI that identifies the equivalent resource.
If you need more precise control over memory allocation and buffer
handling, use MappedToUri
type.
§Examples
use iri_string::format::ToDedicatedString;
use iri_string::types::{IriStr, UriString};
let iri = IriStr::new("http://example.com/?alpha=\u{03B1}")?;
// Type annotation here is not necessary.
let uri: UriString = iri.encode_to_uri().to_dedicated_string();
assert_eq!(uri, "http://example.com/?alpha=%CE%B1");
sourcepub fn as_uri(&self) -> Option<&UriStr>
pub fn as_uri(&self) -> Option<&UriStr>
Converts an IRI into a URI without modification, if possible.
This is semantically equivalent to
UriStr::new(self.as_str()).ok()
.
§Examples
use iri_string::types::{IriStr, UriStr};
let ascii_iri = IriStr::new("http://example.com/?alpha=%CE%B1")?;
assert_eq!(
ascii_iri.as_uri().map(AsRef::as_ref),
Some("http://example.com/?alpha=%CE%B1")
);
let nonascii_iri = IriStr::new("http://example.com/?alpha=\u{03B1}")?;
assert_eq!(nonascii_iri.as_uri(), None);
Trait Implementations§
source§impl<S: Spec> AsRef<RiReferenceStr<S>> for RiStr<S>
impl<S: Spec> AsRef<RiReferenceStr<S>> for RiStr<S>
source§fn as_ref(&self) -> &RiReferenceStr<S>
fn as_ref(&self) -> &RiReferenceStr<S>
source§impl<'de: 'a, 'a, S: 'de + Spec> Deserialize<'de> for &'a RiStr<S>
Available on crate feature serde
only.
impl<'de: 'a, 'a, S: 'de + Spec> Deserialize<'de> for &'a RiStr<S>
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<'a, S: Spec> From<&'a RiAbsoluteStr<S>> for &'a RiStr<S>
impl<'a, S: Spec> From<&'a RiAbsoluteStr<S>> for &'a RiStr<S>
source§fn from(s: &'a RiAbsoluteStr<S>) -> &'a RiStr<S>
fn from(s: &'a RiAbsoluteStr<S>) -> &'a RiStr<S>
source§impl<'a, S: Spec> From<&'a RiStr<S>> for &'a RiReferenceStr<S>
impl<'a, S: Spec> From<&'a RiStr<S>> for &'a RiReferenceStr<S>
source§fn from(s: &'a RiStr<S>) -> &'a RiReferenceStr<S>
fn from(s: &'a RiStr<S>) -> &'a RiReferenceStr<S>
source§impl<'a, S: Spec> From<&'a RiStr<S>> for Cow<'a, RiStr<S>>
Available on crate feature alloc
only.
impl<'a, S: Spec> From<&'a RiStr<S>> for Cow<'a, RiStr<S>>
alloc
only.