Skip to main content

xsd_parser/pipeline/renderer/
custom.rs

1use std::fmt::{Debug, Formatter, Result as FmtResult};
2
3use proc_macro2::TokenStream;
4
5use super::Context;
6
7/// Boxed version of [`ValueRenderer`].
8pub type ValueRendererBox = Box<dyn ValueRenderer>;
9
10impl Debug for ValueRendererBox {
11    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
12        f.debug_struct("ValueRendererBox").finish()
13    }
14}
15
16/// Trait that is responsible for rendering values.
17pub trait ValueRenderer {
18    /// Renders the value and return the resulting token stream if possible.
19    ///
20    /// The `as_const` parameter indicates whether the value should be rendered
21    /// as a constant code or not.
22    fn render(&self, ctx: &Context<'_, '_>) -> TokenStream;
23}
24
25impl<X> ValueRenderer for X
26where
27    X: Fn(&Context<'_, '_>) -> TokenStream,
28{
29    fn render(&self, ctx: &Context<'_, '_>) -> TokenStream {
30        (*self)(ctx)
31    }
32}
33
34impl ValueRenderer for TokenStream {
35    fn render(&self, _ctx: &Context<'_, '_>) -> TokenStream {
36        self.clone()
37    }
38}