pub struct SimpleContext { /* private fields */ }
Available on crate feature
alloc
only.Expand description
Simple template expansion context.
Implementations§
source§impl SimpleContext
impl SimpleContext
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty context.
§Examples
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>
pub fn insert<K, V>(&mut self, key: K, value: V) -> Option<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::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
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§
source§impl Clone for SimpleContext
impl Clone for SimpleContext
source§fn clone(&self) -> SimpleContext
fn clone(&self) -> SimpleContext
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Context for SimpleContext
impl Context for SimpleContext
source§impl Debug for SimpleContext
impl Debug for SimpleContext
source§impl Default for SimpleContext
impl Default for SimpleContext
source§fn default() -> SimpleContext
fn default() -> SimpleContext
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for SimpleContext
impl RefUnwindSafe for SimpleContext
impl Send for SimpleContext
impl Sync for SimpleContext
impl Unpin for SimpleContext
impl UnwindSafe for SimpleContext
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)source§impl<C> DynamicContext for Cwhere
C: Context,
impl<C> DynamicContext for Cwhere
C: Context,
source§fn visit_dynamic<V>(&mut self, visitor: V) -> <V as Visitor>::Resultwhere
V: Visitor,
fn visit_dynamic<V>(&mut self, visitor: V) -> <V as Visitor>::Resultwhere
V: Visitor,
Visits a variable. Read more
source§fn on_expansion_start(&mut self)
fn on_expansion_start(&mut self)
A callback that is called before the expansion of a URI template.
source§fn on_expansion_end(&mut self)
fn on_expansion_end(&mut self)
A callback that is called after the expansion of a URI template.