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/// Re-exported crates for advanced use.
47pub mod deps {
48    pub use next_rust_cache as cache;
49    pub use next_rust_core as core;
50    pub use next_rust_router as router;
51    pub use next_rust_server as server;
52    pub use next_rust_view as view;
53}
54
55/// Memoize an async computation in the data cache.
56pub use next_rust_cache::{cache, invalidate};
57/// ISR / data-cache invalidation.
58pub use next_rust_server::{revalidate_path, revalidate_tag};
59
60/// Everything needed in page, layout and route files.
61pub mod prelude {
62    pub use next_rust_macros::{action, asset, client, css_module, global_css, server, server_action};
63    pub use next_rust_router::Params;
64    pub use next_rust_server::{
65        ActionContext, Auth, AuthUser, Body, Cookie, Cookies, CsrfToken, Data, Error, ErrorInfo, Extension, FormState,
66        Headers, Html, IntoResponse, Json, Next, Nonce, OrNotFound, Path, Query, Rendering, Request, RequestInfo,
67        Response, ResponseHeaders, Result, Robots, SameSite, Session, Sitemap, SseEvent, not_found, permanent_redirect,
68        redirect,
69    };
70    pub use next_rust_view::metadata::{Icon, OgImage, OpenGraph, Twitter};
71    pub use next_rust_view::*;
72
73    pub use crate::LocalFont;
74}
75
76/// Include the generated route table (`routes()`) in the current module.
77///
78/// Normally used through [`app!`]; use it directly to customize the app:
79///
80/// ```ignore
81/// next_rust::routes!();
82///
83/// fn main() {
84///     next_rust::run_with(next_rust::App::new(routes()).middleware(next_rust::request_id()));
85/// }
86/// ```
87#[macro_export]
88macro_rules! routes {
89    () => {
90        #[doc(hidden)]
91        #[allow(non_snake_case, clippy::all, clippy::pedantic)]
92        mod __next_rust_routes {
93            include!(concat!(env!("OUT_DIR"), "/next_rust_routes.rs"));
94        }
95        #[allow(unused_imports)]
96        pub use self::__next_rust_routes::routes;
97    };
98}
99
100/// Define `main` for a Next Rust application with default settings.
101#[macro_export]
102macro_rules! app {
103    () => {
104        $crate::routes!();
105
106        fn main() {
107            $crate::run(routes());
108        }
109    };
110}
111
112#[doc(hidden)]
113pub mod __private {
114    pub use next_rust_server::__private::*;
115
116    use next_rust_view::{Element, attrs};
117
118    pub fn to_json_value<T: serde::Serialize + ?Sized>(value: &T) -> serde_json::Value {
119        serde_json::to_value(value).unwrap_or(serde_json::Value::Null)
120    }
121
122    pub fn island_props(entries: &[(&str, serde_json::Value)]) -> String {
123        let map: serde_json::Map<String, serde_json::Value> =
124            entries.iter().map(|(k, v)| ((*k).to_owned(), v.clone())).collect();
125        serde_json::Value::Object(map).to_string()
126    }
127
128    pub fn island(component: &str, module: Option<&str>, props: String, view: impl View) -> Node {
129        let mut el =
130            Element::new("nr-island").with(attrs::data("component", component)).with(attrs::data("props", props));
131        if let Some(m) = module {
132            el = el.with(attrs::data("module", m));
133        }
134        el.with(view).into_node()
135    }
136}