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

Simple template expansion context.

Implementations

Creates a new empty context.

Examples
#[cfg(feature = "alloc")] {
use iri_string::spec::UriSpec;
use iri_string::template::UriTemplateStr;
use iri_string::template::simple_context::SimpleContext;

let empty_ctx = SimpleContext::new();
let template = UriTemplateStr::new("{no_such_variable}")?;
let expanded = template.expand::<UriSpec, _>(&empty_ctx)?;

assert_eq!(
    expanded.to_string(),
    ""
);

Inserts a variable.

Passing Value::Undefined removes the value from the context.

The entry will be inserted or removed even if the key is invalid as a variable name. Such entries will be simply ignored on expansion.

Examples
#[cfg(feature = "alloc")] {
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"
);

Passing Value::Undefined removes the value from the context.

#[cfg(feature = "alloc")] {
use iri_string::spec::UriSpec;
use iri_string::template::UriTemplateStr;
use iri_string::template::simple_context::{SimpleContext, Value};

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

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

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

Removes all entries in the context.

Examples
#[cfg(feature = "alloc")] {
use iri_string::spec::UriSpec;
use iri_string::template::UriTemplateStr;
use iri_string::template::simple_context::SimpleContext;

let template = UriTemplateStr::new("{foo,bar}")?;
let mut context = SimpleContext::new();

context.insert("foo", "FOO");
context.insert("bar", "BAR");
assert_eq!(
    template.expand::<UriSpec, _>(&context)?.to_string(),
    "FOO,BAR"
);

context.clear();
assert_eq!(
    template.expand::<UriSpec, _>(&context)?.to_string(),
    ""
);

Returns a reference to the value for the key.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Visits a variable. Read more

Formats the value using the given formatter. Read more

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.