Skip to main content

topcoat_runtime/
lib.rs

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