iri_string::template

Struct UriTemplateStr

source
pub struct UriTemplateStr { /* private fields */ }
Expand description

A borrowed 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.

§Applied errata

Errata ID 6937 is applied, so single quotes are allowed to appear in an URI template.

use iri_string::template::UriTemplateStr;

let template = UriTemplateStr::new("'quoted'")?;

Implementations§

source§

impl UriTemplateStr

source

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

Creates a new string.

§Examples
use iri_string::template::UriTemplateStr;

let template = UriTemplateStr::new("/users/{username}")?;
source

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

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§

impl UriTemplateStr

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>

Available on crate feature alloc only.

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 UriTemplateStr

source§

fn as_ref(&self) -> &UriTemplateStr

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

impl AsRef<UriTemplateStr> for UriTemplateString

Available on crate feature alloc only.
source§

fn as_ref(&self) -> &UriTemplateStr

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

impl AsRef<str> for UriTemplateStr

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

Available on crate feature alloc only.
source§

fn borrow(&self) -> &UriTemplateStr

Immutably borrows from an owned value. Read more
source§

impl Debug for UriTemplateStr

source§

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

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

impl<'a, 'de: 'a> Deserialize<'de> for &'a UriTemplateStr

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 UriTemplateStr

source§

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

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

impl<'a> From<&'a UriTemplateStr> for &'a str

source§

fn from(s: &'a UriTemplateStr) -> &'a str

Converts to this type from the input type.
source§

impl From<&UriTemplateStr> for Arc<UriTemplateStr>

Available on crate feature alloc only.
source§

fn from(s: &UriTemplateStr) -> Self

Converts to this type from the input type.
source§

impl From<&UriTemplateStr> for Box<UriTemplateStr>

Available on crate feature alloc only.
source§

fn from(s: &UriTemplateStr) -> Self

Converts to this type from the input type.
source§

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

Available on crate feature alloc only.
source§

fn from(s: &'a UriTemplateStr) -> Self

Converts to this type from the input type.
source§

impl From<&UriTemplateStr> for Rc<UriTemplateStr>

Available on crate feature alloc only.
source§

fn from(s: &UriTemplateStr) -> Self

Converts to this type from the input type.
source§

impl From<&UriTemplateStr> for UriTemplateString

Available on crate feature alloc only.
source§

fn from(s: &UriTemplateStr) -> Self

Converts to this type from the input type.
source§

impl From<UriTemplateString> for Box<UriTemplateStr>

Available on crate feature alloc only.
source§

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

Converts to this type from the input type.
source§

impl Hash for UriTemplateStr

source§

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

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

impl Ord for UriTemplateStr

source§

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

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

impl PartialEq<&UriTemplateStr> for Cow<'_, str>

Available on crate feature alloc only.
source§

fn eq(&self, o: &&UriTemplateStr) -> 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<&UriTemplateStr> for str

source§

fn eq(&self, o: &&UriTemplateStr) -> 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 UriTemplateStr

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 &UriTemplateStr

Available on crate feature alloc only.
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<Cow<'_, str>> for UriTemplateStr

Available on crate feature alloc only.
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<UriTemplateStr> for &str

source§

fn eq(&self, o: &UriTemplateStr) -> 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<UriTemplateStr> for Cow<'_, str>

Available on crate feature alloc only.
source§

fn eq(&self, o: &UriTemplateStr) -> 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<UriTemplateStr> for str

source§

fn eq(&self, o: &UriTemplateStr) -> 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 &UriTemplateStr

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<str> for UriTemplateStr

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 UriTemplateStr

source§

fn eq(&self, other: &UriTemplateStr) -> 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<&UriTemplateStr> for Cow<'_, str>

Available on crate feature alloc only.
source§

fn partial_cmp(&self, o: &&UriTemplateStr) -> 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<&UriTemplateStr> for str

source§

fn partial_cmp(&self, o: &&UriTemplateStr) -> 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 UriTemplateStr

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 &UriTemplateStr

Available on crate feature alloc only.
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<Cow<'_, str>> for UriTemplateStr

Available on crate feature alloc only.
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<UriTemplateStr> for &str

source§

fn partial_cmp(&self, o: &UriTemplateStr) -> 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<UriTemplateStr> for Cow<'_, str>

Available on crate feature alloc only.
source§

fn partial_cmp(&self, o: &UriTemplateStr) -> 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<UriTemplateStr> for str

source§

fn partial_cmp(&self, o: &UriTemplateStr) -> 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 &UriTemplateStr

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<str> for UriTemplateStr

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 UriTemplateStr

source§

fn partial_cmp(&self, other: &UriTemplateStr) -> 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 UriTemplateStr

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 ToOwned for UriTemplateStr

Available on crate feature alloc only.
source§

type Owned = UriTemplateString

The resulting type after obtaining ownership.
source§

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

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

fn clone_into(&self, target: &mut Self::Owned)

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

impl<'a> TryFrom<&'a [u8]> for &'a UriTemplateStr

source§

type Error = Error

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

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

Performs the conversion.
source§

impl<'a> TryFrom<&'a str> for &'a UriTemplateStr

source§

type Error = Error

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

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

Performs the conversion.
source§

impl Eq for UriTemplateStr

source§

impl StructuralPartialEq for UriTemplateStr

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more