Struct iri_string::template::simple_context::SimpleContext
source · [−]pub struct SimpleContext { /* private fields */ }
Available on crate feature
alloc
only.Expand description
Simple template expansion context.
Implementations
sourceimpl SimpleContext
impl SimpleContext
sourcepub fn new() -> Self
pub fn new() -> Self
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(),
""
);
sourcepub fn insert<K, V>(&mut self, key: K, value: V) -> Option<Value>where
K: Into<String>,
V: Into<Value>,
pub fn insert<K, V>(&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
#[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/"
);
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
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(),
""
);
Trait Implementations
sourceimpl Clone for SimpleContext
impl Clone for SimpleContext
sourcefn clone(&self) -> SimpleContext
fn clone(&self) -> SimpleContext
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Context for SimpleContext
impl Context for SimpleContext
sourceimpl Debug for SimpleContext
impl Debug for SimpleContext
sourceimpl Default for SimpleContext
impl Default for SimpleContext
sourcefn default() -> SimpleContext
fn default() -> SimpleContext
Returns the “default value” for a type. Read more
Auto Trait Implementations
impl RefUnwindSafe for SimpleContext
impl Send for SimpleContext
impl Sync for SimpleContext
impl Unpin for SimpleContext
impl UnwindSafe for SimpleContext
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more