iri_string::template

Struct UriTemplateString

source
pub struct UriTemplateString { /* private fields */ }
Available on crate feature alloc only.
Expand description

An owned slice of a URI template.

URI Template is defined by RFC 6570.

Note that “URI Template” can also be used for IRI.

§Valid values

This type can have a URI template string.

Implementations§

source§

impl UriTemplateString

source

pub unsafe fn new_unchecked(s: String) -> 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

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the inner buffer to match its length.

source

pub fn capacity(&self) -> usize

Returns the internal buffer capacity in bytes.

source

pub fn as_slice(&self) -> &UriTemplateStr

Returns the borrowed IRI string slice.

This is equivalent to &*self.

source

pub fn append(&mut self, other: &UriTemplateStr)

Appends the template string.

Methods from Deref<Target = UriTemplateStr>§

source

pub fn as_str(&self) -> &str

Returns the template as a plain &str.

§Examples
use iri_string::template::UriTemplateStr;

let template = UriTemplateStr::new("/users/{username}")?;
assert_eq!(template.as_str(), "/users/{username}");
source

pub fn len(&self) -> usize

Returns the template string length.

§Examples
use iri_string::template::UriTemplateStr;

let template = UriTemplateStr::new("/users/{username}")?;
assert_eq!(template.len(), "/users/{username}".len());
source

pub fn is_empty(&self) -> bool

Returns whether the string is empty.

§Examples
use iri_string::template::UriTemplateStr;

let template = UriTemplateStr::new("/users/{username}")?;
assert!(!template.is_empty());

let empty = UriTemplateStr::new("")?;
assert!(empty.is_empty());
source

pub fn expand<'a, S: Spec, C: Context>( &'a self, context: &'a C, ) -> Result<Expanded<'a, S, C>, Error>

Expands the template with the given context.

§Examples
use iri_string::spec::UriSpec;
use iri_string::template::UriTemplateStr;
use iri_string::template::simple_context::SimpleContext;

let mut context = SimpleContext::new();
context.insert("username", "foo");

let template = UriTemplateStr::new("/users/{username}")?;
let expanded = template.expand::<UriSpec, _>(&context)?;

assert_eq!(
    expanded.to_string(),
    "/users/foo"
);

You can control allowed characters in the output by changing spec type.

use iri_string::spec::{IriSpec, UriSpec};
use iri_string::template::UriTemplateStr;
use iri_string::template::simple_context::SimpleContext;

let mut context = SimpleContext::new();
context.insert("alpha", "\u{03B1}");

let template = UriTemplateStr::new("{?alpha}")?;

assert_eq!(
    template.expand::<UriSpec, _>(&context)?.to_string(),
    "?alpha=%CE%B1",
    "a URI cannot contain Unicode alpha (U+03B1), so it should be escaped"
);
assert_eq!(
    template.expand::<IriSpec, _>(&context)?.to_string(),
    "?alpha=\u{03B1}",
    "an IRI can contain Unicode alpha (U+03B1), so it written as is"
);
source

pub fn expand_dynamic<S: Spec, W: Write, C: DynamicContext>( &self, writer: &mut W, context: &mut C, ) -> Result<(), Error>

Expands the template with the given dynamic context.

If you need the allocated String, useexpand_dynamic_to_string.

See the documentation for DynamicContext for usage.

source

pub fn expand_dynamic_to_string<S: Spec, C: DynamicContext>( &self, context: &mut C, ) -> Result<String, Error>

Expands the template into a string, with the given dynamic context.

This is basically expand_dynamic method that returns an owned string instead of writing to the given writer.

See the documentation for DynamicContext for usage.

§Examples
use iri_string::template::UriTemplateStr;
use iri_string::spec::UriSpec;

struct MyContext<'a> {
    // See the documentation for `DynamicContext`.
}

let mut context = MyContext {
    target: "/posts/1",
    username: Some("the_admin"),
    username_visited: false,
};

// No access to the variable `username`.
let template = UriTemplateStr::new("{+target}{?username}")?;
let s = template.expand_dynamic_to_string::<UriSpec, _>(&mut context)?;
assert_eq!(s, "/posts/1?username=the_admin");
assert!(context.username_visited);
source

pub fn variables(&self) -> UriTemplateVariables<'_>

Returns an iterator of variables in the template.

§Examples
use iri_string::template::UriTemplateStr;

let template = UriTemplateStr::new("foo{/bar*,baz:4}{?qux}{&bar*}")?;
let mut vars = template.variables();
assert_eq!(vars.next().map(|var| var.as_str()), Some("bar"));
assert_eq!(vars.next().map(|var| var.as_str()), Some("baz"));
assert_eq!(vars.next().map(|var| var.as_str()), Some("qux"));
assert_eq!(vars.next().map(|var| var.as_str()), Some("bar"));

Trait Implementations§

source§

impl AsRef<UriTemplateStr> for UriTemplateString

source§

fn as_ref(&self) -> &UriTemplateStr

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<str> for UriTemplateString

source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Borrow<UriTemplateStr> for UriTemplateString

source§

fn borrow(&self) -> &UriTemplateStr

Immutably borrows from an owned value. Read more
source§

impl Borrow<str> for UriTemplateString

source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
source§

impl Clone for UriTemplateString

source§

fn clone(&self) -> UriTemplateString

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for UriTemplateString

source§

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

Formats the value using the given formatter. Read more
source§

impl Default for UriTemplateString

source§

fn default() -> UriTemplateString

Returns the “default value” for a type. Read more
source§

impl Deref for UriTemplateString

source§

type Target = UriTemplateStr

The resulting type after dereferencing.
source§

fn deref(&self) -> &UriTemplateStr

Dereferences the value.
source§

impl<'de> Deserialize<'de> for UriTemplateString

Available on crate feature serde only.
source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for UriTemplateString

source§

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

Formats the value using the given formatter. Read more
source§

impl From<&UriTemplateStr> for UriTemplateString

source§

fn from(s: &UriTemplateStr) -> Self

Converts to this type from the input type.
source§

impl From<UriTemplateString> for Box<UriTemplateStr>

source§

fn from(s: UriTemplateString) -> Box<UriTemplateStr>

Converts to this type from the input type.
source§

impl<'a> From<UriTemplateString> for Cow<'a, UriTemplateStr>

source§

fn from(s: UriTemplateString) -> Cow<'a, UriTemplateStr>

Converts to this type from the input type.
source§

impl From<UriTemplateString> for String

source§

fn from(s: UriTemplateString) -> Self

Converts to this type from the input type.
source§

impl FromStr for UriTemplateString

source§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl Hash for UriTemplateString

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for UriTemplateString

source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<&str> for UriTemplateString

source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Cow<'_, str>> for UriTemplateString

source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<String> for UriTemplateString

source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<UriTemplateString> for &str

source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<UriTemplateString> for Cow<'_, str>

source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<UriTemplateString> for String

source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<UriTemplateString> for str

source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<str> for UriTemplateString

source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq for UriTemplateString

source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<&str> for UriTemplateString

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<Cow<'_, str>> for UriTemplateString

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<String> for UriTemplateString

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<UriTemplateString> for &str

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<UriTemplateString> for Cow<'_, str>

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<UriTemplateString> for String

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<UriTemplateString> for str

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<str> for UriTemplateString

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd for UriTemplateString

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for UriTemplateString

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl TryFrom<&[u8]> for UriTemplateString

source§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&str> for UriTemplateString

source§

type Error = Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl TryFrom<String> for UriTemplateString

source§

type Error = CreationError<String>

The type returned in the event of a conversion error.
source§

fn try_from(s: String) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Eq for UriTemplateString

source§

impl StructuralPartialEq for UriTemplateString

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T> ToStringFallible for T
where T: Display,

source§

fn try_to_string(&self) -> Result<String, TryReserveError>

Available on crate feature alloc only.

ToString::to_string, but without panic on OOM.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,