iri_string::template

Module context

source
Expand description

Template expansion context.

§Examples

  1. Define your context type.
  2. Implement Context trait (and Context::visit method) for the type.
    1. Get variable name by Visitor::var_name method.
    2. Feed the corresponding value(s) by one of Visitor::visit_* methods.

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§

Enums§

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.