iri_string/template/context.rs
1//! Template expansion context.
2//!
3//! # Examples
4//!
5//! 1. Define your context type.
6//! 2. Implement [`Context`] trait (and [`Context::visit`] method) for the type.
7//! 1. Get variable name by [`Visitor::var_name`] method.
8//! 2. Feed the corresponding value(s) by one of `Visitor::visit_*` methods.
9//!
10//! Note that contexts should return consistent result across multiple visits for
11//! the same variable. In other words, `Context::visit` should return the same
12//! result for the same `Visitor::var_name()` during the context is borrowed.
13//! If this condition is violated, the URI template processor can return
14//! invalid result or panic at worst.
15//!
16//! ```
17//! use iri_string::template::context::{Context, Visitor, ListVisitor, AssocVisitor};
18//!
19//! struct MyContext {
20//! name: &'static str,
21//! id: u64,
22//! tags: &'static [&'static str],
23//! children: &'static [(&'static str, usize)],
24//! }
25//!
26//! impl Context for MyContext {
27//! fn visit<V: Visitor>(&self, visitor: V) -> V::Result {
28//! let name = visitor.var_name().as_str();
29//! match name {
30//! "name" => visitor.visit_string(self.name),
31//! "id" => visitor.visit_string(self.id),
32//! "tags" => visitor.visit_list().visit_items_and_finish(self.tags),
33//! "children" => visitor
34//! .visit_assoc()
35//! .visit_entries_and_finish(self.children.iter().copied()),
36//! _ => visitor.visit_undefined(),
37//! }
38//! }
39//! }
40//! ```
41//
42// # Developers note
43//
44// Visitor types **should not** be cloneable in order to enforce just one
45// visitor is used to visit a variable. If visitors are cloneable, it can make
46// the wrong usage to be available, i.e. storing cloned visitors somewhere and
47// using the wrong one.
48//
49// However, if visitors are made cloneable by any chance, it does not indicate
50// the whole implementation will be broken. Users can only use the visitors
51// through visitor traits (and their API do not allow cloning), so the logic
52// would work as expected if the internal usage of the visitors are correct.
53// Making visitors noncloneable is an optional safety guard (with no overhead).
54
55use core::fmt;
56use core::ops::ControlFlow;
57
58pub use crate::template::components::VarName;
59
60/// A trait for types that can behave as a static URI template expansion context.
61///
62/// This type is for use with [`UriTemplateStr::expand`] method.
63///
64/// See [the module documentation][`crate::template`] for usage.
65///
66/// [`UriTemplateStr::expand`]: `crate::template::UriTemplateStr::expand`
67pub trait Context: Sized {
68 /// Visits a variable.
69 ///
70 /// To get variable name, use [`Visitor::var_name()`].
71 #[must_use]
72 fn visit<V: Visitor>(&self, visitor: V) -> V::Result;
73}
74
75/// A trait for types that can behave as a dynamic (mutable) URI template expansion context.
76///
77/// This type is for use with [`UriTemplateStr::expand_dynamic`] method and its
78/// family.
79///
80/// Note that "dynamic" here does not mean that the value of variables can
81/// change during a template expansion. The value should be fixed and consistent
82/// during each expansion, but the context is allowed to mutate itself if it
83/// does not break this rule.
84///
85/// # Exmaples
86///
87/// ```
88/// # #[cfg(feature = "alloc")]
89/// # extern crate alloc;
90/// # use iri_string::template::Error;
91/// # #[cfg(feature = "alloc")] {
92/// # use alloc::string::String;
93/// use iri_string::template::UriTemplateStr;
94/// use iri_string::template::context::{DynamicContext, Visitor, VisitPurpose};
95/// use iri_string::spec::UriSpec;
96///
97/// struct MyContext<'a> {
98/// /// Target path.
99/// target: &'a str,
100/// /// Username.
101/// username: Option<&'a str>,
102/// /// A flag to remember whether the URI template
103/// /// attempted to use `username` variable.
104/// username_visited: bool,
105/// }
106///
107/// impl DynamicContext for MyContext<'_> {
108/// fn on_expansion_start(&mut self) {
109/// // Reset the state.
110/// self.username_visited = false;
111/// }
112/// fn visit_dynamic<V: Visitor>(&mut self, visitor: V) -> V::Result {
113/// match visitor.var_name().as_str() {
114/// "target" => visitor.visit_string(self.target),
115/// "username" => {
116/// if visitor.purpose() == VisitPurpose::Expand {
117/// // The variable `username` is being used
118/// // on the template expansion.
119/// // Don't care whether `username` is defined or not.
120/// self.username_visited = true;
121/// }
122/// if let Some(username) = &self.username {
123/// visitor.visit_string(username)
124/// } else {
125/// visitor.visit_undefined()
126/// }
127/// }
128/// _ => visitor.visit_undefined(),
129/// }
130/// }
131/// }
132///
133/// let mut context = MyContext {
134/// target: "/posts/1",
135/// username: Some("the_admin"),
136/// username_visited: false,
137/// };
138/// let mut buf = String::new();
139///
140/// // No access to the variable `username`.
141/// let template1 = UriTemplateStr::new("{+target}")?;
142/// template1.expand_dynamic::<UriSpec, _, _>(&mut buf, &mut context)?;
143/// assert_eq!(buf, "/posts/1");
144/// assert!(!context.username_visited);
145///
146/// buf.clear();
147/// // Will access to the variable `username`.
148/// let template2 = UriTemplateStr::new("{+target}{?username}")?;
149/// template2.expand_dynamic::<UriSpec, _, _>(&mut buf, &mut context)?;
150/// assert_eq!(buf, "/posts/1?username=the_admin");
151/// assert!(context.username_visited);
152///
153/// buf.clear();
154/// context.username = None;
155/// // Will access to the variable `username` but it is undefined.
156/// template2.expand_dynamic::<UriSpec, _, _>(&mut buf, &mut context)?;
157/// assert_eq!(buf, "/posts/1");
158/// assert!(
159/// context.username_visited,
160/// "`MyContext` can know and remember whether `visit_dynamic()` is called
161/// for `username`, even if its value is undefined"
162/// );
163/// # }
164/// # Ok::<_, Error>(())
165/// ```
166///
167/// [`UriTemplateStr::expand_dynamic`]: `crate::template::UriTemplateStr::expand_dynamic`
168pub trait DynamicContext: Sized {
169 /// Visits a variable.
170 ///
171 /// To get variable name, use [`Visitor::var_name()`].
172 ///
173 /// # Restriction
174 ///
175 /// The visit results should be consistent and unchanged between the last
176 /// time [`on_expansion_start`][`Self::on_expansion_start`] was called and
177 /// the next time [`on_expansion_end`][`Self::on_expansion_end`] will be
178 /// called. If this condition is violated, template expansion will produce
179 /// wrong result or may panic at worst.
180 #[must_use]
181 fn visit_dynamic<V: Visitor>(&mut self, visitor: V) -> V::Result;
182
183 /// A callback that is called before the expansion of a URI template.
184 #[inline]
185 fn on_expansion_start(&mut self) {}
186
187 /// A callback that is called after the expansion of a URI template.
188 #[inline]
189 fn on_expansion_end(&mut self) {}
190}
191
192impl<C: Context> DynamicContext for C {
193 #[inline]
194 fn visit_dynamic<V: Visitor>(&mut self, visitor: V) -> V::Result {
195 self.visit(visitor)
196 }
197}
198
199/// A purpose of a visit.
200///
201/// This enum is nonexhaustive since this partially exposes the internal
202/// implementation of the template expansion, and thus this is subject to
203/// change.
204#[derive(Debug, Clone, Copy, PartialEq, Eq)]
205#[non_exhaustive]
206pub enum VisitPurpose {
207 /// A visit for type checking.
208 Typecheck,
209 /// A visit for template expansion to retrieve the value.
210 Expand,
211}
212
213/// Variable visitor.
214///
215/// See [the module documentation][self] for usage.
216// NOTE (internal): Visitor types **should not** be cloneable.
217pub trait Visitor: Sized + private::Sealed {
218 /// Result of the visit.
219 type Result;
220 /// List visitor.
221 type ListVisitor: ListVisitor<Result = Self::Result>;
222 /// Associative array visitor.
223 type AssocVisitor: AssocVisitor<Result = Self::Result>;
224
225 /// Returns the name of the variable to visit.
226 #[must_use]
227 fn var_name(&self) -> VarName<'_>;
228 /// Returns the purpose of the visit.
229 ///
230 /// The template expansion algorithm checks the types for some variables
231 /// depending on its usage. To get the usage count correctly, you should
232 /// only count visits with [`VisitPurpose::Expand`].
233 ///
234 /// If you need to know whether the variable is accessed and does not
235 /// need dynamic context generation or access counts, consider using
236 /// [`UriTemplateStr::variables`] method to iterate the variables in the
237 /// URI template.
238 ///
239 /// [`UriTemplateStr::variables`]: `crate::template::UriTemplateStr::variables`
240 #[must_use]
241 fn purpose(&self) -> VisitPurpose;
242 /// Visits an undefined variable, i.e. indicates that the requested variable is unavailable.
243 #[must_use]
244 fn visit_undefined(self) -> Self::Result;
245 /// Visits a string variable.
246 #[must_use]
247 fn visit_string<T: fmt::Display>(self, v: T) -> Self::Result;
248 /// Visits a list variable.
249 #[must_use]
250 fn visit_list(self) -> Self::ListVisitor;
251 /// Visits an associative array variable.
252 #[must_use]
253 fn visit_assoc(self) -> Self::AssocVisitor;
254}
255
256/// List visitor.
257///
258/// See [the module documentation][self] for usage.
259// NOTE (internal): Visitor types **should not** be cloneable.
260pub trait ListVisitor: Sized + private::Sealed {
261 /// Result of the visit.
262 type Result;
263
264 /// Visits an item.
265 ///
266 /// If this returned `ControlFlow::Break(v)`, [`Context::visit`] should also
267 /// return this `v`.
268 ///
269 /// To feed multiple items at once, do
270 /// `items.into_iter().try_for_each(|item| self.visit_item(item))` for example.
271 #[must_use]
272 fn visit_item<T: fmt::Display>(&mut self, item: T) -> ControlFlow<Self::Result>;
273 /// Finishes visiting the list.
274 #[must_use]
275 fn finish(self) -> Self::Result;
276
277 /// Visits items and finish.
278 #[must_use]
279 fn visit_items_and_finish<T, I>(mut self, items: I) -> Self::Result
280 where
281 T: fmt::Display,
282 I: IntoIterator<Item = T>,
283 {
284 match items.into_iter().try_for_each(|item| self.visit_item(item)) {
285 ControlFlow::Break(v) => v,
286 ControlFlow::Continue(()) => self.finish(),
287 }
288 }
289}
290
291/// Associative array visitor.
292///
293/// See [the module documentation][self] for usage.
294// NOTE (internal): Visitor types **should not** be cloneable.
295pub trait AssocVisitor: Sized + private::Sealed {
296 /// Result of the visit.
297 type Result;
298
299 /// Visits an entry.
300 ///
301 /// If this returned `ControlFlow::Break(v)`, [`Context::visit`] should also
302 /// return this `v`.
303 ///
304 /// To feed multiple items at once, do
305 /// `entries.into_iter().try_for_each(|(key, value)| self.visit_entry(key, value))`
306 /// for example.
307 #[must_use]
308 fn visit_entry<K: fmt::Display, V: fmt::Display>(
309 &mut self,
310 key: K,
311 value: V,
312 ) -> ControlFlow<Self::Result>;
313 /// Finishes visiting the associative array.
314 #[must_use]
315 fn finish(self) -> Self::Result;
316
317 /// Visits entries and finish.
318 #[must_use]
319 fn visit_entries_and_finish<K, V, I>(mut self, entries: I) -> Self::Result
320 where
321 K: fmt::Display,
322 V: fmt::Display,
323 I: IntoIterator<Item = (K, V)>,
324 {
325 match entries
326 .into_iter()
327 .try_for_each(|(key, value)| self.visit_entry(key, value))
328 {
329 ControlFlow::Break(v) => v,
330 ControlFlow::Continue(()) => self.finish(),
331 }
332 }
333}
334
335/// Private module to put the trait to seal.
336pub(super) mod private {
337 /// A trait for visitor types of variables in a context.
338 pub trait Sealed {}
339}