Skip to main content

topcoat_runtime/
lib.rs

1mod bind_attribute;
2mod event_handler;
3mod expr;
4#[cfg(feature = "router")]
5mod procedure;
6#[cfg(feature = "router")]
7mod reactive_scope;
8#[cfg(feature = "router")]
9mod shard;
10mod signal;
11mod surrogate;
12
13pub use bind_attribute::*;
14pub use event_handler::*;
15pub use expr::*;
16#[cfg(feature = "router")]
17pub use procedure::*;
18#[cfg(feature = "router")]
19pub use reactive_scope::*;
20#[cfg(feature = "router")]
21pub use shard::*;
22pub use signal::*;
23pub use surrogate::*;
24
25use topcoat_asset::{Asset, asset};
26
27pub const SCRIPT: Asset = asset!("browser/dist/index.js", rename: "topcoat");
28
29/// Macro helpers to shorten the generated source code.
30#[doc(hidden)]
31pub mod internal {
32    use topcoat_view::{HtmlContext, PartsWriter, ViewParts};
33
34    #[inline]
35    pub fn __js(parts: &mut ViewParts, js: impl Into<std::borrow::Cow<'static, str>>) {
36        // JavaScript source renders inside comment markers and double-quoted
37        // attributes; the comment context escapes the union of what both
38        // positions need.
39        PartsWriter::new(parts, HtmlContext::Comment).push_str(js);
40    }
41
42    #[inline]
43    pub fn __js_unescaped(parts: &mut ViewParts, s: &'static str) {
44        PartsWriter::new(parts, HtmlContext::Unescaped).push_str(s);
45    }
46
47    #[inline]
48    pub fn __surrogate(parts: &mut ViewParts, value: &(impl serde::Serialize + ?Sized)) {
49        let mut writer = PartsWriter::new(parts, HtmlContext::Comment);
50        writer.push_str_unescaped("cx.hydrate(");
51        let json = serde_json::to_string(value).expect("failed to serialize surrogate value");
52        writer.push_str(json);
53        writer.push_str_unescaped(")");
54    }
55}