Skip to main content

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: &lt;b&gt;</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 components;
28mod escape;
29pub mod metadata;
30mod node;
31mod render;
32pub mod style;
33mod tags;
34
35pub use attrs::*;
36pub use components::{Children, ImageProps, LinkProps, Slots, each, fragment_of, island, raw_html, suspense, when};
37pub use escape::{escape_attr, escape_text, is_safe_url};
38pub use metadata::Metadata;
39pub use node::{Attr, AttrValue, Element, IntoViewResult, Node, Part, Suspense, View};
40pub use render::{
41    DocumentParts, RenderFlags, STREAMING_RUNTIME, render_static, render_to_string, resolve, stream_document,
42};
43pub use style::{CssClass, Stylesheet};
44pub use tags::{TAGS, VOID_TAGS};
45
46/// Group children without a wrapper element: `fragment![a, b, c]`.
47#[macro_export]
48macro_rules! fragment {
49    ($($child:expr),* $(,)?) => {
50        $crate::Node::Fragment(vec![$($crate::View::into_node($child)),*])
51    };
52}
53
54/// Client-side-navigation aware link.
55///
56/// ```
57/// use next_rust_view::*;
58/// let html = render_static(Link!(href = "/about", class = "nav", "About"));
59/// assert_eq!(html, r#"<a href="/about" class="nav" data-nr-link="">About</a>"#);
60/// ```
61#[macro_export]
62macro_rules! Link {
63    ($($body:tt)*) => { $crate::__link_munch!([$crate::Element::new("a")] $($body)*) };
64}
65
66#[doc(hidden)]
67#[macro_export]
68macro_rules! __link_munch {
69    ([$($acc:tt)*]) => { $($acc)*.with($crate::attrs::data("nr-link", "")) };
70    ([$($acc:tt)*] $key:ident = $val:expr $(, $($rest:tt)*)?) => {
71        $crate::__link_munch!([$($acc)*.with($crate::attrs::$key($val))] $($($rest)*)?)
72    };
73    ([$($acc:tt)*] $child:expr $(, $($rest:tt)*)?) => {
74        $crate::__link_munch!([$($acc)*.with($child)] $($($rest)*)?)
75    };
76}
77
78/// Responsive, lazily loaded image.
79///
80/// ```
81/// use next_rust_view::*;
82/// let html = render_static(Image!(src = "/cat.jpg", width = 640, height = 480, alt = "A cat"));
83/// assert!(html.starts_with(r#"<img src="/_nr/image?url=%2Fcat.jpg&amp;w=640&amp;q=75""#));
84/// assert!(html.contains(r#"loading="lazy""#));
85/// ```
86#[macro_export]
87macro_rules! Image {
88    ($($key:ident = $val:expr),* $(,)?) => {
89        $crate::ImageProps::default()$(.$key($val))*
90    };
91}