Skip to main content

next_rust/
lib.rs

1//! # Next Rust
2//!
3//! A Rust-native full-stack web framework: the filesystem is your router,
4//! `page.rs` is a page, `layout.rs` wraps its children, and the build turns
5//! the app directory into a compiled, type-checked route table.
6//!
7//! ```text
8//! app/
9//! ├── layout.rs          → wraps every page
10//! ├── page.rs            → /
11//! ├── about/page.rs      → /about
12//! └── blog/[slug]/page.rs → /blog/:slug
13//! ```
14//!
15//! ```ignore
16//! // app/page.rs
17//! use next_rust::prelude::*;
18//!
19//! pub fn Page() -> impl View {
20//!     div![h1!["Hello World"], p!["Welcome to Next Rust."]]
21//! }
22//! ```
23//!
24//! ```ignore
25//! // build.rs
26//! fn main() { next_rust_build::generate(); }
27//!
28//! // src/main.rs
29//! next_rust::app!();
30//! ```
31//!
32//! Guides live on the documentation website (`website/` in the repository).
33
34#![forbid(unsafe_code)]
35
36mod font;
37
38pub use font::LocalFont;
39pub use next_rust_cache::{Cache, CacheEntry, CacheKey, CacheOptions, CacheStore, FileStore, MemoryStore};
40pub use next_rust_core::{Config, Diagnostic, Environment, RenderingMode, config, env as dotenv};
41pub use next_rust_macros::{action, asset, client, css_module, global_css, server, server_action};
42pub use next_rust_router::{ParamValue, Params};
43pub use next_rust_server::*;
44pub use next_rust_view::*;
45
46/// Ready-made components: `use next_rust::ui::*;` (see [`next_rust_ui`]).
47pub use next_rust_ui as ui;
48
49/// Every Lucide icon: `icons::House().size(20)` (see [`next_rust_icons`]).
50pub use next_rust_icons as icons;
51
52/// Re-exported crates for advanced use.
53pub mod deps {
54    pub use next_rust_cache as cache;
55    pub use next_rust_core as core;
56    pub use next_rust_router as router;
57    pub use next_rust_server as server;
58    pub use next_rust_view as view;
59}
60
61/// Memoize an async computation in the data cache.
62pub use next_rust_cache::{cache, invalidate};
63/// ISR / data-cache invalidation.
64pub use next_rust_server::{revalidate_path, revalidate_tag};
65
66/// Everything needed in page, layout and route files.
67pub mod prelude {
68    pub use next_rust_macros::{action, asset, client, css_module, global_css, server, server_action};
69    pub use next_rust_router::Params;
70    pub use next_rust_server::{
71        ActionContext, Auth, AuthUser, Body, ClientIp, Cookie, Cookies, CsrfToken, Data, Error, ErrorInfo, Extension,
72        Feed, FeedEntry, FormState, Headers, Html, IntoResponse, Json, Next, Nonce, OrNotFound, Path, Query, Rendering,
73        Request, RequestInfo, Response, ResponseHeaders, Result, Robots, RobotsRule, SameSite, Session, Sitemap,
74        SitemapEntry, SseEvent, not_found, permanent_redirect, redirect,
75    };
76    pub use next_rust_view::metadata::{Icon, OgImage, OpenGraph, Twitter};
77    pub use next_rust_view::*;
78
79    pub use crate::LocalFont;
80}
81
82/// Include the generated route table (`routes()`) in the current module.
83///
84/// Normally used through [`app!`]; use it directly to customize the app:
85///
86/// ```ignore
87/// next_rust::routes!();
88///
89/// fn main() {
90///     next_rust::run_with(next_rust::App::new(routes()).middleware(next_rust::request_id()));
91/// }
92/// ```
93#[macro_export]
94macro_rules! routes {
95    () => {
96        #[doc(hidden)]
97        #[allow(non_snake_case, clippy::all, clippy::pedantic)]
98        mod __next_rust_routes {
99            include!(concat!(env!("OUT_DIR"), "/next_rust_routes.rs"));
100        }
101        #[allow(unused_imports)]
102        pub use self::__next_rust_routes::routes;
103    };
104}
105
106/// Define `main` for a Next Rust application with default settings.
107#[macro_export]
108macro_rules! app {
109    () => {
110        $crate::routes!();
111
112        fn main() {
113            $crate::run(routes());
114        }
115    };
116}
117
118#[doc(hidden)]
119pub mod __private {
120    pub use next_rust_server::__private::*;
121
122    use next_rust_view::{Element, attrs};
123
124    pub fn to_json_value<T: serde::Serialize + ?Sized>(value: &T) -> serde_json::Value {
125        serde_json::to_value(value).unwrap_or(serde_json::Value::Null)
126    }
127
128    pub fn island_props(entries: &[(&str, serde_json::Value)]) -> String {
129        let map: serde_json::Map<String, serde_json::Value> =
130            entries.iter().map(|(k, v)| ((*k).to_owned(), v.clone())).collect();
131        serde_json::Value::Object(map).to_string()
132    }
133
134    pub fn island(component: &str, module: Option<&str>, props: String, view: impl View) -> Node {
135        let mut el =
136            Element::new("nr-island").with(attrs::data("component", component)).with(attrs::data("props", props));
137        if let Some(m) = module {
138            el = el.with(attrs::data("module", m));
139        }
140        el.with(view).into_node()
141    }
142}