Struct RiRelativeStr

struct RiRelativeStr<S> { ... }

A borrowed slice of a relative IRI reference.

This corresponds to irelative-ref rule in RFC 3987 (and relative-ref rule in RFC 3986). The rule for irelative-ref is irelative-part [ "?" iquery ] [ "#" ifragment ].

Valid values

This type can have a relative IRI reference.

# use iri_string::types::IriRelativeStr;
assert!(IriRelativeStr::new("foo").is_ok());
assert!(IriRelativeStr::new("foo/bar").is_ok());
assert!(IriRelativeStr::new("/foo").is_ok());
assert!(IriRelativeStr::new("//foo/bar").is_ok());
assert!(IriRelativeStr::new("?foo").is_ok());
assert!(IriRelativeStr::new("#foo").is_ok());
assert!(IriRelativeStr::new("foo/bar?baz#qux").is_ok());
// The first path component can have colon if the path is absolute.
assert!(IriRelativeStr::new("/foo:bar/").is_ok());
// Second or following path components can have colon.
assert!(IriRelativeStr::new("foo/bar://baz/").is_ok());
assert!(IriRelativeStr::new("./foo://bar").is_ok());

Absolute form of a reference is not allowed.

# use iri_string::types::IriRelativeStr;
assert!(IriRelativeStr::new("https://example.com/").is_err());
// The first path component cannot have colon, if the path is not absolute.
assert!(IriRelativeStr::new("foo:bar").is_err());
assert!(IriRelativeStr::new("foo:").is_err());
assert!(IriRelativeStr::new("foo:/").is_err());
assert!(IriRelativeStr::new("foo://").is_err());
assert!(IriRelativeStr::new("foo:///").is_err());
assert!(IriRelativeStr::new("foo:////").is_err());
assert!(IriRelativeStr::new("foo://///").is_err());

Some characters and sequences cannot used in an IRI reference.

# use iri_string::types::IriRelativeStr;
// `<` and `>` cannot directly appear in a relative IRI reference.
assert!(IriRelativeStr::new("<not allowed>").is_err());
// Broken percent encoding cannot appear in a relative IRI reference.
assert!(IriRelativeStr::new("%").is_err());
assert!(IriRelativeStr::new("%GG").is_err());

Implementations

impl RiRelativeStr<IriSpec>

fn encode_to_uri(self: &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]crate::convert::MappedToUri type.

Examples

# use iri_string::validate::Error;
# #[cfg(feature = "alloc")] {
use iri_string::format::ToDedicatedString;
use iri_string::types::{IriRelativeStr, UriRelativeString};

let iri = IriRelativeStr::new("../?alpha=\u{03B1}")?;
// Type annotation here is not necessary.
let uri: UriRelativeString = iri.encode_to_uri().to_dedicated_string();
assert_eq!(uri, "../?alpha=%CE%B1");
# }
# Ok::<_, Error>(())
fn as_uri(self: &Self) -> Option<&UriRelativeStr>

Converts an IRI into a URI without modification, if possible.

This is semantically equivalent to UriRelativeStr::new(self.as_str()).ok().

Examples

# use iri_string::validate::Error;
use iri_string::types::{IriRelativeStr, UriRelativeStr};

let ascii_iri = IriRelativeStr::new("../?alpha=%CE%B1")?;
assert_eq!(
    ascii_iri.as_uri().map(AsRef::as_ref),
    Some("../?alpha=%CE%B1")
);

let nonascii_iri = IriRelativeStr::new("../?alpha=\u{03B1}")?;
assert_eq!(nonascii_iri.as_uri(), None);
# Ok::<_, Error>(())

impl<S: Spec> RiRelativeStr<S>

fn authority_str(self: &Self) -> Option<&str>

Returns the authority.

The leading // is truncated.

Examples

# use iri_string::validate::Error;
use iri_string::types::IriRelativeStr;

let iri = IriRelativeStr::new("//example.com/pathpath?queryquery#fragfrag")?;
assert_eq!(iri.authority_str(), Some("example.com"));
# Ok::<_, Error>(())
# use iri_string::validate::Error;
use iri_string::types::IriRelativeStr;

let iri = IriRelativeStr::new("foo//bar:baz")?;
assert_eq!(iri.authority_str(), None);
# Ok::<_, Error>(())
fn path_str(self: &Self) -> &str

Returns the path.

Examples

# use iri_string::validate::Error;
use iri_string::types::IriRelativeStr;

let iri = IriRelativeStr::new("//example.com/pathpath?queryquery#fragfrag")?;
assert_eq!(iri.path_str(), "/pathpath");
# Ok::<_, Error>(())
# use iri_string::validate::Error;
use iri_string::types::IriRelativeStr;

let iri = IriRelativeStr::new("foo//bar:baz")?;
assert_eq!(iri.path_str(), "foo//bar:baz");
# Ok::<_, Error>(())
fn query(self: &Self) -> Option<&RiQueryStr<S>>

Returns the query.

The leading question mark (?) is truncated.

Examples

# use iri_string::validate::Error;
use iri_string::types::{IriQueryStr, IriRelativeStr};

let iri = IriRelativeStr::new("//example.com/pathpath?queryquery#fragfrag")?;
let query = IriQueryStr::new("queryquery")?;
assert_eq!(iri.query(), Some(query));
# Ok::<_, Error>(())
# use iri_string::validate::Error;
use iri_string::types::{IriQueryStr, IriRelativeStr};

let iri = IriRelativeStr::new("foo//bar:baz?")?;
let query = IriQueryStr::new("")?;
assert_eq!(iri.query(), Some(query));
# Ok::<_, Error>(())
fn query_str(self: &Self) -> Option<&str>

Returns the query in a raw string slice.

The leading question mark (?) is truncated.

Examples

# use iri_string::validate::Error;
use iri_string::types::IriRelativeStr;

let iri = IriRelativeStr::new("//example.com/pathpath?queryquery#fragfrag")?;
assert_eq!(iri.query_str(), Some("queryquery"));
# Ok::<_, Error>(())
# use iri_string::validate::Error;
use iri_string::types::IriRelativeStr;

let iri = IriRelativeStr::new("foo//bar:baz?")?;
assert_eq!(iri.query_str(), Some(""));
# Ok::<_, Error>(())
fn fragment(self: &Self) -> Option<&RiFragmentStr<S>>

Returns the fragment part if exists.

A leading # character is truncated if the fragment part exists.

Examples

If the IRI has a fragment part, Some(_) is returned.

# use iri_string::{spec::IriSpec, types::{IriFragmentStr, IriRelativeStr}, validate::Error};
let iri = IriRelativeStr::new("?foo#bar")?;
let fragment = IriFragmentStr::new("bar")?;
assert_eq!(iri.fragment(), Some(fragment));
# Ok::<_, Error>(())
# use iri_string::{spec::IriSpec, types::{IriFragmentStr, IriRelativeStr}, validate::Error};
let iri = IriRelativeStr::new("#foo")?;
let fragment = IriFragmentStr::new("foo")?;
assert_eq!(iri.fragment(), Some(fragment));
# Ok::<_, Error>(())

When the fragment part exists but is empty string, Some(_) is returned.

# use iri_string::{spec::IriSpec, types::{IriFragmentStr, IriRelativeStr}, validate::Error};
let iri = IriRelativeStr::new("#")?;
let fragment = IriFragmentStr::new("")?;
assert_eq!(iri.fragment(), Some(fragment));
# Ok::<_, Error>(())

If the IRI has no fragment, None is returned.

# use iri_string::{spec::IriSpec, types::IriRelativeStr, validate::Error};
let iri = IriRelativeStr::new("")?;
assert_eq!(iri.fragment(), None);
# Ok::<_, Error>(())
fn fragment_str(self: &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

If the IRI has a fragment part, Some(_) is returned.

# use iri_string::{spec::IriSpec, types::IriRelativeStr, validate::Error};
let iri = IriRelativeStr::new("?foo#bar")?;
assert_eq!(iri.fragment_str(), Some("bar"));
# Ok::<_, Error>(())
# use iri_string::{spec::IriSpec, types::IriRelativeStr, validate::Error};
let iri = IriRelativeStr::new("#foo")?;
assert_eq!(iri.fragment_str(), Some("foo"));
# Ok::<_, Error>(())

When the fragment part exists but is empty string, Some(_) is returned.

# use iri_string::{spec::IriSpec, types::IriRelativeStr, validate::Error};
let iri = IriRelativeStr::new("#")?;
assert_eq!(iri.fragment_str(), Some(""));
# Ok::<_, Error>(())

If the IRI has no fragment, None is returned.

# use iri_string::{spec::IriSpec, types::IriRelativeStr, validate::Error};
let iri = IriRelativeStr::new("")?;
assert_eq!(iri.fragment(), None);
# Ok::<_, Error>(())
fn authority_components(self: &Self) -> Option<AuthorityComponents<'_>>

Returns the authority components.

Examples

# use iri_string::validate::Error;
use iri_string::types::IriRelativeStr;

let iri = IriRelativeStr::new("//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"));
# Ok::<_, Error>(())
# use iri_string::validate::Error;
use iri_string::types::IriRelativeStr;

let iri = IriRelativeStr::new("foo//bar:baz")?;
assert_eq!(iri.authority_str(), None);
# Ok::<_, Error>(())

impl<S: Spec> RiRelativeStr<S>

fn resolve_against<'a>(self: &'a Self, base: &'a RiAbsoluteStr<S>) -> Normalized<'a, RiStr<S>>

Returns resolved IRI against the given base IRI.

For IRI reference resolution output examples, see RFC 3986 section 5.4.

If you are going to resolve multiple references against the common base, consider using FixedBaseResolver.

Strictness

The IRI parsers provided by this crate is strict (e.g. http:g is always interpreted as a composition of the scheme http and the path g), so backward compatible parsing and resolution are not provided. About parser and resolver strictness, see RFC 3986 section 5.4.2:

Some parsers allow the scheme name to be present in a relative reference if it is the same as the base URI scheme. This is considered to be a loophole in prior specifications of partial URI RFC1630. Its use should be avoided but is allowed for backward compatibility.

--- https://tools.ietf.org/html/rfc3986#section-5.4.2

Failures

This method itself does not fail, but IRI resolution without WHATWG URL Standard serialization can fail in some minor cases.

To see examples of such unresolvable IRIs, visit the documentation for [normalize]crate::normalize module.

fn mask_password(self: &Self) -> PasswordMasked<'_, Self>

Returns the proxy to the IRI with password masking feature.

Examples

# use iri_string::validate::Error;
# #[cfg(feature = "alloc")] {
use iri_string::format::ToDedicatedString;
use iri_string::types::IriRelativeStr;

let iri = IriRelativeStr::new("//user:password@example.com/path?query")?;
let masked = iri.mask_password();
assert_eq!(masked.to_dedicated_string(), "//user:@example.com/path?query");

assert_eq!(
    masked.replace_password("${password}").to_string(),
    "//user:${password}@example.com/path?query"
);
# }
# Ok::<_, Error>(())

impl<S: crate::spec::Spec> RiRelativeStr<S>

fn new(s: &str) -> Result<&Self, Error>

Creates a new string.

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.

fn as_str(self: &Self) -> &str

Returns &str.

fn len(self: &Self) -> usize

Returns the string length.

fn is_empty(self: &Self) -> bool

Returns whether the string is empty.

impl<S> Freeze for RiRelativeStr<S>

impl<S> RefUnwindSafe for RiRelativeStr<S>

impl<S> Send for RiRelativeStr<S>

impl<S> Sized for RiRelativeStr<S>

impl<S> Sync for RiRelativeStr<S>

impl<S> Unpin for RiRelativeStr<S>

impl<S> UnsafeUnpin for RiRelativeStr<S>

impl<S> UnwindSafe for RiRelativeStr<S>

impl<S: Spec> Buildable for RiRelativeStr<S>

impl<S: crate::spec::Spec> AsRef for RiRelativeStr<S>

fn as_ref(self: &Self) -> &RiReferenceStr<S>

impl<S: crate::spec::Spec> AsRef for RiRelativeStr<S>

fn as_ref(self: &Self) -> &RiRelativeStr<S>

impl<S: crate::spec::Spec> AsRef for RiRelativeStr<S>

fn as_ref(self: &Self) -> &str

impl<S: crate::spec::Spec> Debug for RiRelativeStr<S>

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl<S: crate::spec::Spec> Display for RiRelativeStr<S>

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl<S: crate::spec::Spec> Eq for RiRelativeStr<S>

impl<S: crate::spec::Spec> Hash for RiRelativeStr<S>

fn hash<H: core::hash::Hasher>(self: &Self, state: &mut H)

impl<S: crate::spec::Spec> Ord for RiRelativeStr<S>

fn cmp(self: &Self, other: &Self) -> Ordering

impl<S: crate::spec::Spec> PartialEq for RiRelativeStr<S>

fn eq(self: &Self, other: &Self) -> bool

impl<S: crate::spec::Spec> PartialEq for RiRelativeStr<S>

fn eq(self: &Self, o: &Cow<'_, str>) -> bool

impl<S: crate::spec::Spec> PartialEq for RiRelativeStr<S>

fn eq(self: &Self, o: &&str) -> bool

impl<S: crate::spec::Spec> PartialEq for RiRelativeStr<S>

fn eq(self: &Self, o: &str) -> bool

impl<S: crate::spec::Spec> PartialOrd for RiRelativeStr<S>

fn partial_cmp(self: &Self, o: &Cow<'_, str>) -> Option<Ordering>

impl<S: crate::spec::Spec> PartialOrd for RiRelativeStr<S>

fn partial_cmp(self: &Self, o: &&str) -> Option<Ordering>

impl<S: crate::spec::Spec> PartialOrd for RiRelativeStr<S>

fn partial_cmp(self: &Self, o: &str) -> Option<Ordering>

impl<S: crate::spec::Spec> PartialOrd for RiRelativeStr<S>

fn partial_cmp(self: &Self, other: &Self) -> Option<Ordering>

impl<S: crate::spec::Spec> ToOwned for RiRelativeStr<S>

fn to_owned(self: &Self) -> <Self as >::Owned

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiRelativeStr<S>

fn eq(self: &Self, o: &RiReferenceString<T>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiRelativeStr<S>

fn eq(self: &Self, o: &&RiReferenceStr<T>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiRelativeStr<S>

fn eq(self: &Self, o: &RiReferenceStr<T>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiRelativeStr<S>

fn eq(self: &Self, o: &Cow<'_, RiReferenceStr<T>>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiRelativeStr<S>

fn eq(self: &Self, o: &RiRelativeString<T>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiRelativeStr<T>

fn eq(self: &Self, o: &&RiRelativeStr<S>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiRelativeStr<S>

fn partial_cmp(self: &Self, o: &RiReferenceStr<T>) -> Option<Ordering>

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiRelativeStr<S>

fn partial_cmp(self: &Self, o: &Cow<'_, RiReferenceStr<T>>) -> Option<Ordering>

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiRelativeStr<S>

fn partial_cmp(self: &Self, o: &RiRelativeString<T>) -> Option<Ordering>

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiRelativeStr<S>

fn partial_cmp(self: &Self, o: &RiReferenceString<T>) -> Option<Ordering>

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiRelativeStr<S>

fn partial_cmp(self: &Self, o: &&RiReferenceStr<T>) -> Option<Ordering>

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiRelativeStr<T>

fn partial_cmp(self: &Self, o: &&RiRelativeStr<S>) -> Option<Ordering>

impl<T> Any for RiRelativeStr<S>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for RiRelativeStr<S>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for RiRelativeStr<S>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> ToString for RiRelativeStr<S>

fn to_string(self: &Self) -> String