Skip to main content

topcoat_runtime/
expr.rs

1use topcoat_core::context::Cx;
2use topcoat_view::{NodeViewParts, PartsWriter};
3
4use crate::Js;
5
6#[derive(Debug, Clone)]
7pub struct Expr<T> {
8    pub(crate) evaluated: T,
9    pub(crate) js: Js,
10}
11
12impl<T> Expr<T> {
13    #[inline]
14    pub fn new(evaluated: T, js: Js) -> Self {
15        Self { evaluated, js }
16    }
17
18    #[inline]
19    pub fn into_evaluated_and_js(self) -> (T, Js) {
20        (self.evaluated, self.js)
21    }
22}
23
24impl<T> NodeViewParts for Expr<T>
25where
26    T: NodeViewParts,
27{
28    fn into_view_parts(self, cx: &Cx, parts: &mut PartsWriter<'_>) {
29        // <!-- ::topcoat::expr::start("<js>") -->
30        //
31        // The comment context seals the source, so a `"` inside it renders
32        // as `&quot;` and the quotes stay unambiguous delimiters on the
33        // client.
34        parts.push_comment(|comment| {
35            comment.push_promoted_str_unescaped(&"::topcoat::expr::start(\"");
36            self.js.write(comment);
37            comment.push_promoted_str_unescaped(&"\")");
38        });
39        self.evaluated.into_view_parts(cx, parts);
40        parts.push_comment(|comment| {
41            comment.push_promoted_str_unescaped(&"::topcoat::expr::end");
42        });
43    }
44}