resuma 1.3.1

Resuma — resumable SSR Rust web framework: zero hydration, islands, server actions, Flow (Axum).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! `resuma new <name>` - scaffold a brand new Resuma project.

use std::fs;
use std::path::Path;

use anyhow::{anyhow, Context, Result};

const BASIC_MAIN: &str = r##"use resuma::prelude::*;

const CSS: &str = r#"<style>
body { font-family: system-ui, sans-serif; max-width: 40rem; margin: 3rem auto; padding: 0 1rem; line-height: 1.6; color: #1e1b4b; }
h1 { margin: 0 0 .5rem; font-size: 2rem; }
p { margin: .5rem 0; color: #4338ca; }
</style>"#;

fn home() -> View {
    view! {
        <main>
            <h1>"Hello, Resuma"</h1>
            <p>"A static page - zero client JavaScript, pure SSR."</p>
            <p>"Add signals, #[server], and islands when you need interactivity."</p>
        </main>
    }
}

#[tokio::main]
async fn main() -> std::io::Result<()> {
    ResumaApp::new()
        .with_title("%NAME%")
        .with_head(CSS)
        .page("/", || home())
        .serve(ServeOptions::default())
        .await
}
"##;

/// Full-feature todo showcase (kept in sync with `examples/todo` - update `templates/todo/` when editing the example).
const TODO_MAIN: &str = include_str!("../../templates/todo/main.rs");
const TODO_SECURITY: &str = include_str!("../../templates/todo/security.rs");
const TODO_STORE: &str = include_str!("../../templates/todo/todo_store.rs");

const CARGO_BASIC: &str = r#"[package]
name = "%NAME%"
version = "0.1.0"
edition = "2021"

[dependencies]
resuma = "%RESUMA_VERSION%"
tokio  = { version = "1", features = ["full"] }
"#;

const CARGO_TODO: &str = r#"[package]
name = "%NAME%"
version = "0.1.0"
edition = "2021"

[dependencies]
resuma      = "%RESUMA_VERSION%"
tokio       = { version = "1", features = ["full"] }
serde       = { version = "1", features = ["derive"] }
serde_json  = { version = "1" }
once_cell   = "1"
parking_lot = "0.12"
"#;

const CARGO_FLOW: &str = r#"[package]
name = "%NAME%"
version = "0.1.0"
edition = "2021"

[dependencies]
resuma = "%RESUMA_VERSION%"
tokio  = { version = "1", features = ["full"] }
serde  = { version = "1", features = ["derive"] }
"#;

const FLOW_MAIN: &str = include_str!("../../templates/flow/main.rs");
const FLOW_INDEX: &str = include_str!("../../templates/flow/pages/index.rs");
const FLOW_ABOUT: &str = include_str!("../../templates/flow/pages/about.rs");
const FLOW_MOD: &str = include_str!("../../templates/flow/pages/mod.rs");
const FLOW_REGISTRY: &str = include_str!("../../templates/flow/pages/_registry.rs");

const CARGO_FLOW_BOOKING: &str = r#"[package]
name = "%NAME%"
version = "0.1.0"
edition = "2021"

[dependencies]
resuma = "%RESUMA_VERSION%"
tokio  = { version = "1", features = ["full"] }
serde  = { version = "1", features = ["derive"] }
once_cell   = "1"
parking_lot = "0.12"
"#;

const FLOW_BOOKING_MAIN: &str = include_str!("../../templates/flow-booking/main.rs");
const FLOW_BOOKING_STORE: &str = include_str!("../../templates/flow-booking/booking_store.rs");
const FLOW_BOOKING_MOD: &str = include_str!("../../templates/flow-booking/pages/mod.rs");
const FLOW_BOOKING_REGISTRY: &str = include_str!("../../templates/flow-booking/pages/_registry.rs");
const FLOW_BOOKING_INDEX: &str = include_str!("../../templates/flow-booking/pages/index.rs");
const FLOW_BOOKING_BOOK: &str = include_str!("../../templates/flow-booking/pages/book.rs");
const FLOW_BOOKING_GRACIAS: &str = include_str!("../../templates/flow-booking/pages/gracias.rs");

const CARGO_FULLSTACK: &str = r#"[package]
name = "%NAME%"
version = "0.1.0"
edition = "2021"

[dependencies]
resuma = "%RESUMA_VERSION%"
tokio  = { version = "1", features = ["full"] }
serde  = { version = "1", features = ["derive"] }
sqlx   = { version = "0.8", features = ["runtime-tokio", "sqlite", "macros", "migrate"] }
anyhow = "1"
"#;

const FULLSTACK_MAIN: &str = include_str!("../../templates/flow-fullstack/main.rs");
const FULLSTACK_DB: &str = include_str!("../../templates/flow-fullstack/db.rs");
const FULLSTACK_PAGES_MOD: &str = include_str!("../../templates/flow-fullstack/pages/mod.rs");
const FULLSTACK_INDEX: &str = include_str!("../../templates/flow-fullstack/pages/index.rs");
const FULLSTACK_USERS: &str = include_str!("../../templates/flow-fullstack/pages/users.rs");
const FULLSTACK_REGISTRY: &str = include_str!("../../templates/flow-fullstack/pages/_registry.rs");
const FULLSTACK_MIGRATION: &str = include_str!("../../templates/add/sqlx/001_users.sql");

const CARGO_PRODUCTION: &str = r#"[package]
name = "%NAME%"
version = "0.1.0"
edition = "2021"

[dependencies]
resuma = "%RESUMA_VERSION%"
resuma-flow = "%RESUMA_VERSION%"
tokio  = { version = "1", features = ["full"] }
serde  = { version = "1", features = ["derive"] }
serde_json = "1"
"#;

const PRODUCTION_MAIN: &str = include_str!("../../templates/production/main.rs");
const PRODUCTION_SECURITY: &str = include_str!("../../templates/production/security.rs");
const PRODUCTION_PAGES_MOD: &str = include_str!("../../templates/production/pages/mod.rs");
const PRODUCTION_REGISTRY: &str = include_str!("../../templates/production/pages/_registry.rs");
const PRODUCTION_INDEX: &str = include_str!("../../templates/production/pages/index.rs");
const PRODUCTION_OPS: &str = include_str!("../../templates/production/pages/ops.rs");
const PRODUCTION_DOCKERFILE: &str = include_str!("../../templates/production/Dockerfile");
const PRODUCTION_FLY: &str = include_str!("../../templates/production/fly.toml");
const PRODUCTION_ENV: &str = include_str!("../../templates/production/env.example");

const RUST_TOOLCHAIN: &str = r#"[toolchain]
channel = "stable"
"#;

const README: &str = r##"# %NAME%

Created with [Resuma](https://github.com/GoldevLab/resuma).

## Templates

- **basic** - static SSR page, zero client JS
- **todo** - full Resuma showcase (signals, server, island, security, js!)
- **flow** - multi-page app with `src/pages/` and FlowApp
- **flow-booking** - appointments with query-driven `#[load]` + `loader_refresh_input`
- **flow-fullstack** - Flow + SQLx (SQLite) with users CRUD sample
- **production** - Flow + security stub + Dockerfile + fly.toml

## Develop

    resuma dev
    resuma dev --open

## Add integrations

    resuma add sqlx
    resuma add turso

## Build

    resuma build
"##;

fn render_cargo(template: &str, name: &str) -> String {
    template
        .replace("%NAME%", name)
        .replace("%RESUMA_VERSION%", env!("CARGO_PKG_VERSION"))
}

fn crate_name_from_path(path: &str) -> Result<String> {
    let base = Path::new(path)
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or(path);
    if !is_valid_crate_name(base) {
        return Err(anyhow!(
            "invalid project name `{base}` — use a Cargo package name (letters, digits, `-`, `_`)"
        ));
    }
    Ok(base.to_string())
}

fn is_valid_crate_name(s: &str) -> bool {
    let mut chars = s.chars();
    match chars.next() {
        Some(c) if c.is_ascii_alphabetic() || c == '_' => {}
        _ => return false,
    }
    s.chars()
        .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
}

pub fn create_project(name: &str, template: &str) -> Result<()> {
    let dir = Path::new(name);
    if dir.exists() {
        return Err(anyhow!("directory `{}` already exists", name));
    }
    let crate_name = crate_name_from_path(name)?;
    fs::create_dir_all(dir.join("src"))?;

    let readme = README.replace("%NAME%", &crate_name);
    fs::write(dir.join("README.md"), readme).context("write README.md")?;
    fs::write(dir.join(".gitignore"), "target/\n").context("write .gitignore")?;
    fs::write(dir.join("rust-toolchain.toml"), RUST_TOOLCHAIN)
        .context("write rust-toolchain.toml")?;

    match template {
        "basic" => {
            fs::write(
                dir.join("Cargo.toml"),
                render_cargo(CARGO_BASIC, &crate_name),
            )
            .context("write Cargo.toml")?;
            fs::write(
                dir.join("src/main.rs"),
                BASIC_MAIN.replace("%NAME%", &crate_name),
            )
            .context("write src/main.rs")?;
        }
        "todo" => {
            fs::write(
                dir.join("Cargo.toml"),
                render_cargo(CARGO_TODO, &crate_name),
            )
            .context("write Cargo.toml")?;
            let main_rs = TODO_MAIN
                .replace("Resuma · Todo", &crate_name)
                .replace("example-todo", &crate_name);
            fs::write(dir.join("src/main.rs"), main_rs).context("write src/main.rs")?;
            fs::write(dir.join("src/security.rs"), TODO_SECURITY)
                .context("write src/security.rs")?;
            fs::write(dir.join("src/todo_store.rs"), TODO_STORE)
                .context("write src/todo_store.rs")?;
        }
        "flow" => {
            fs::write(
                dir.join("Cargo.toml"),
                render_cargo(CARGO_FLOW, &crate_name),
            )
            .context("write Cargo.toml")?;
            fs::write(
                dir.join("src/main.rs"),
                FLOW_MAIN.replace("%NAME%", &crate_name),
            )
            .context("write src/main.rs")?;
            let pages = dir.join("src/pages");
            fs::create_dir_all(&pages)?;
            fs::write(pages.join("mod.rs"), FLOW_MOD).context("write pages/mod.rs")?;
            fs::write(pages.join("_registry.rs"), FLOW_REGISTRY)
                .context("write pages/_registry.rs")?;
            fs::write(pages.join("index.rs"), FLOW_INDEX).context("write pages/index.rs")?;
            fs::write(pages.join("about.rs"), FLOW_ABOUT).context("write pages/about.rs")?;
        }
        "flow-booking" => {
            fs::write(
                dir.join("Cargo.toml"),
                render_cargo(CARGO_FLOW_BOOKING, &crate_name),
            )
            .context("write Cargo.toml")?;
            fs::write(
                dir.join("src/main.rs"),
                FLOW_BOOKING_MAIN.replace("%NAME%", &crate_name),
            )
            .context("write src/main.rs")?;
            fs::write(dir.join("src/booking_store.rs"), FLOW_BOOKING_STORE)
                .context("write src/booking_store.rs")?;
            let pages = dir.join("src/pages");
            fs::create_dir_all(&pages)?;
            fs::write(pages.join("mod.rs"), FLOW_BOOKING_MOD).context("write pages/mod.rs")?;
            fs::write(pages.join("_registry.rs"), FLOW_BOOKING_REGISTRY)
                .context("write pages/_registry.rs")?;
            fs::write(
                pages.join("index.rs"),
                FLOW_BOOKING_INDEX.replace("%NAME%", &crate_name),
            )
            .context("write pages/index.rs")?;
            fs::write(pages.join("book.rs"), FLOW_BOOKING_BOOK).context("write pages/book.rs")?;
            fs::write(pages.join("gracias.rs"), FLOW_BOOKING_GRACIAS)
                .context("write pages/gracias.rs")?;
            let public = dir.join("public");
            fs::create_dir_all(&public)?;
            fs::write(public.join(".gitkeep"), "").ok();
        }
        "flow-fullstack" => {
            fs::write(
                dir.join("Cargo.toml"),
                render_cargo(CARGO_FULLSTACK, &crate_name),
            )
            .context("write Cargo.toml")?;
            fs::write(
                dir.join("src/main.rs"),
                FULLSTACK_MAIN.replace("%NAME%", &crate_name),
            )
            .context("write src/main.rs")?;
            fs::write(dir.join("src/db.rs"), FULLSTACK_DB).context("write src/db.rs")?;
            let pages = dir.join("src/pages");
            fs::create_dir_all(&pages)?;
            fs::write(pages.join("mod.rs"), FULLSTACK_PAGES_MOD).context("write pages/mod.rs")?;
            fs::write(pages.join("_registry.rs"), FULLSTACK_REGISTRY)
                .context("write pages/_registry.rs")?;
            fs::write(pages.join("index.rs"), FULLSTACK_INDEX).context("write pages/index.rs")?;
            fs::write(pages.join("users.rs"), FULLSTACK_USERS).context("write pages/users.rs")?;
            let mig = dir.join("migrations");
            fs::create_dir_all(&mig)?;
            fs::write(mig.join("001_users.sql"), FULLSTACK_MIGRATION)?;
            fs::write(dir.join(".env.example"), "DATABASE_URL=sqlite:local.db\n").ok();
        }
        "production" => {
            fs::write(
                dir.join("Cargo.toml"),
                render_cargo(CARGO_PRODUCTION, &crate_name),
            )
            .context("write Cargo.toml")?;
            fs::write(
                dir.join("src/main.rs"),
                PRODUCTION_MAIN.replace("%NAME%", &crate_name),
            )
            .context("write src/main.rs")?;
            fs::write(dir.join("src/security.rs"), PRODUCTION_SECURITY)
                .context("write src/security.rs")?;
            let pages = dir.join("src/pages");
            fs::create_dir_all(&pages)?;
            fs::write(pages.join("mod.rs"), PRODUCTION_PAGES_MOD).context("write pages/mod.rs")?;
            fs::write(pages.join("_registry.rs"), PRODUCTION_REGISTRY)
                .context("write pages/_registry.rs")?;
            fs::write(
                pages.join("index.rs"),
                PRODUCTION_INDEX.replace("%NAME%", &crate_name),
            )
            .context("write pages/index.rs")?;
            fs::write(pages.join("ops.rs"), PRODUCTION_OPS).context("write pages/ops.rs")?;
            fs::write(
                dir.join("Dockerfile"),
                PRODUCTION_DOCKERFILE.replace("%NAME%", &crate_name),
            )
            .context("write Dockerfile")?;
            fs::write(
                dir.join("fly.toml"),
                PRODUCTION_FLY.replace("%NAME%", &crate_name),
            )
            .context("write fly.toml")?;
            fs::write(dir.join(".env.example"), PRODUCTION_ENV).context("write .env.example")?;
            fs::write(dir.join(".dockerignore"), "/target\n.git\n.github\n")
                .context("write .dockerignore")?;
            let public = dir.join("public");
            fs::create_dir_all(&public)?;
            fs::write(public.join(".gitkeep"), "").ok();
        }
        other => {
            return Err(anyhow!(
                "unknown template `{}` (try: basic, todo, flow, flow-booking, flow-fullstack, production)",
                other
            ));
        }
    }

    println!("[resuma] created `{}` (template: {})", name, template);
    println!("\n  cd {}", name);
    println!("  resuma dev      # hot reload at http://127.0.0.1:3000");
    println!("  resuma --help   # build, routes, update, doctor, add…");
    println!("  cargo run       # works, but no hot reload — prefer resuma dev\n");
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn basic_template_page_returns_view() {
        assert!(BASIC_MAIN.contains("fn home() -> View"));
        assert!(!BASIC_MAIN.contains("fn home() {"));
    }

    #[test]
    fn cargo_templates_pin_the_cli_crate_version() {
        for cargo_template in [
            CARGO_BASIC,
            CARGO_TODO,
            CARGO_FLOW,
            CARGO_FLOW_BOOKING,
            CARGO_FULLSTACK,
            CARGO_PRODUCTION,
        ] {
            let rendered = render_cargo(cargo_template, "smoke-app");
            assert!(rendered.contains("name = \"smoke-app\""));
            assert!(rendered.contains(&format!("\"{}\"", env!("CARGO_PKG_VERSION"))));
            assert!(!rendered.contains("%RESUMA_VERSION%"));
        }
    }

    #[test]
    fn crate_name_uses_basename_of_a_path() {
        assert_eq!(crate_name_from_path("my-app").unwrap(), "my-app");
        assert_eq!(
            crate_name_from_path("/tmp/resuma-tpl/basic").unwrap(),
            "basic"
        );
        assert!(crate_name_from_path("/tmp/foo/bar/baz").is_ok());
        assert!(crate_name_from_path("/tmp/not valid").is_err());
    }
}