lazy_template/enclosed/
segment.rs

1use crate::Render;
2use derive_more::Display;
3use pipe_trait::Pipe;
4
5/// Represent a segment of a parsed template.
6#[derive(Debug, Clone, Copy)]
7pub enum Segment<Query> {
8    Character(char),
9    Expression(Query),
10}
11
12/// Returned upon the [rendering](Render) of a [`Segment`].
13///
14/// Value of this type can be converted to a string by using the [`Display`] trait.
15#[derive(Debug, Display, Clone, Copy)]
16pub enum SegmentDisplay<Output> {
17    Character(char),
18    ExpressionResult(Output),
19}
20
21impl<Respond, Output, Error, Query> Render<Respond, SegmentDisplay<Output>, Error>
22    for Segment<Query>
23where
24    Respond: FnMut(Query) -> Result<Output, Error>,
25{
26    fn render(self, respond: &mut Respond) -> Result<SegmentDisplay<Output>, Error> {
27        Ok(match self {
28            Segment::Character(value) => SegmentDisplay::Character(value),
29            Segment::Expression(query) => respond(query)?.pipe(SegmentDisplay::ExpressionResult),
30        })
31    }
32}