Struct SimpleContext

struct SimpleContext { ... }

Simple template expansion context.

Implementations

impl SimpleContext

fn new() -> Self

Creates a new empty context.

Examples

# use iri_string::template::Error;
# #[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(),
    ""
);
# }
# Ok::<_, Error>(())
fn insert<K, V>(self: &mut Self, key: K, value: V) -> Option<Value>
where
    K: Into<String>,
    V: Into<Value>

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

# use iri_string::template::Error;
# #[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"
);
# }
# Ok::<_, Error>(())

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

# use iri_string::template::Error;
## [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/"
);
# }
# Ok::<_, Error>(())
fn clear(self: &mut Self)

Removes all entries in the context.

Examples

# use iri_string::template::Error;
# #[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(),
    ""
);
# }
# Ok::<_, Error>(())
fn get(self: &Self, key: VarName<'_>) -> Option<&Value>

Returns a reference to the value for the key.

impl Clone for SimpleContext

fn clone(self: &Self) -> SimpleContext

impl Context for SimpleContext

fn visit<V: Visitor>(self: &Self, visitor: V) -> <V as >::Result

impl Debug for SimpleContext

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

impl Default for SimpleContext

fn default() -> SimpleContext

impl Freeze for SimpleContext

impl RefUnwindSafe for SimpleContext

impl Send for SimpleContext

impl Sync for SimpleContext

impl Unpin for SimpleContext

impl UnsafeUnpin for SimpleContext

impl UnwindSafe for SimpleContext

impl<C> DynamicContext for SimpleContext

fn visit_dynamic<V>(self: &mut Self, visitor: V) -> <V as Visitor>::Result
where
    V: Visitor

impl<T> Any for SimpleContext

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for SimpleContext

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

impl<T> BorrowMut for SimpleContext

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

impl<T> CloneToUninit for SimpleContext

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for SimpleContext

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for SimpleContext

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for SimpleContext

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for SimpleContext

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

impl<T, U> TryInto for SimpleContext

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