next_rust_view/lib.rs
1//! The Next Rust view layer.
2//!
3//! ```
4//! use next_rust_view::*;
5//!
6//! fn card(title: &str) -> impl View {
7//! div![class("card"), h2![title], p!["Escaped by default: <b>"]]
8//! }
9//!
10//! let html = render_static(card("Hello"));
11//! assert_eq!(html, r#"<div class="card"><h2>Hello</h2><p>Escaped by default: <b></p></div>"#);
12//! ```
13//!
14//! Design:
15//!
16//! * Element macros (`div!`, `p!`, ...) build an [`Element`] from a list of
17//! *parts*. A part is either an attribute ([`Attr`]) or a child view.
18//! * Everything that can be rendered implements [`View`], which converts it
19//! into a [`Node`] tree. Components are plain functions returning `impl View`.
20//! * All text and attribute values are escaped. Raw HTML requires
21//! [`raw_html`], which is intentionally conspicuous.
22//! * Async content is expressed with [`suspense`] and can be streamed.
23
24#![forbid(unsafe_code)]
25
26pub mod attrs;
27pub mod class_names;
28pub mod components;
29mod escape;
30pub mod metadata;
31mod node;
32mod render;
33pub mod style;
34mod tags;
35
36pub use attrs::*;
37pub use components::{Children, ImageProps, LinkProps, Slots, each, fragment_of, island, raw_html, suspense, when};
38pub use escape::{escape_attr, escape_text, is_safe_url};
39pub use metadata::Metadata;
40pub use node::{Attr, AttrValue, Element, IntoViewResult, Node, Part, Suspense, View};
41pub use render::{
42 DocumentParts, RenderFlags, STREAMING_RUNTIME, render_static, render_to_string, resolve, stream_document,
43};
44pub use style::{CssClass, Stylesheet};
45pub use tags::{TAGS, VOID_TAGS};
46
47/// Group children without a wrapper element: `fragment![a, b, c]`.
48#[macro_export]
49macro_rules! fragment {
50 ($($child:expr),* $(,)?) => {
51 $crate::Node::Fragment(vec![$($crate::View::into_node($child)),*])
52 };
53}
54
55/// Client-side-navigation aware link.
56///
57/// ```
58/// use next_rust_view::*;
59/// let html = render_static(Link!(href = "/about", class = "nav", "About"));
60/// assert_eq!(html, r#"<a href="/about" class="nav" data-nr-link="">About</a>"#);
61/// ```
62#[macro_export]
63macro_rules! Link {
64 ($($body:tt)*) => { $crate::__link_munch!([$crate::Element::new("a")] $($body)*) };
65}
66
67#[doc(hidden)]
68#[macro_export]
69macro_rules! __link_munch {
70 ([$($acc:tt)*]) => { $($acc)*.with($crate::attrs::data("nr-link", "")) };
71 ([$($acc:tt)*] $key:ident = $val:expr $(, $($rest:tt)*)?) => {
72 $crate::__link_munch!([$($acc)*.with($crate::attrs::$key($val))] $($($rest)*)?)
73 };
74 ([$($acc:tt)*] $child:expr $(, $($rest:tt)*)?) => {
75 $crate::__link_munch!([$($acc)*.with($child)] $($($rest)*)?)
76 };
77}
78
79/// Responsive, lazily loaded image.
80///
81/// ```
82/// use next_rust_view::*;
83/// let html = render_static(Image!(src = "/cat.jpg", width = 640, height = 480, alt = "A cat"));
84/// assert!(html.starts_with(r#"<img src="/_nr/image?url=%2Fcat.jpg&w=640&q=75""#));
85/// assert!(html.contains(r#"loading="lazy""#));
86/// ```
87#[macro_export]
88macro_rules! Image {
89 ($($key:ident = $val:expr),* $(,)?) => {
90 $crate::ImageProps::default()$(.$key($val))*
91 };
92}