Expand description
Template expansion context.
§Examples
- Define your context type.
- Implement
Context
trait (andContext::visit
method) for the type.- Get variable name by
Visitor::var_name
method. - Feed the corresponding value(s) by one of
Visitor::visit_*
methods.
- Get variable name by
Note that contexts should return consistent result across multiple visits for
the same variable. In other words, Context::visit
should return the same
result for the same Visitor::var_name()
during the context is borrowed.
If this condition is violated, the URI template processor can return
invalid result or panic at worst.
use iri_string::template::context::{Context, Visitor, ListVisitor, AssocVisitor};
struct MyContext {
name: &'static str,
id: u64,
tags: &'static [&'static str],
children: &'static [(&'static str, usize)],
}
impl Context for MyContext {
fn visit<V: Visitor>(&self, visitor: V) -> V::Result {
let name = visitor.var_name().as_str();
match name {
"name" => visitor.visit_string(self.name),
"id" => visitor.visit_string(self.id),
"tags" => visitor.visit_list().visit_items_and_finish(self.tags),
"children" => visitor
.visit_assoc()
.visit_entries_and_finish(self.children.iter().copied()),
_ => visitor.visit_undefined(),
}
}
}
Structs§
- Variable name.
Enums§
- A purpose of a visit.
Traits§
- Associative array visitor.
- A trait for types that can behave as a static URI template expansion context.
- A trait for types that can behave as a dynamic (mutable) URI template expansion context.
- List visitor.
- Variable visitor.