renox-cli 1.0.0-rc.5

rnx, the command-line tool for the Renox web framework (like Laravel's artisan).
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
//! `rnx make:*` generators. Each writes new files into the app (never over
//! existing ones) and wires them up where that's safe to do automatically.

use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, bail};
use heck::{ToKebabCase, ToSnakeCase, ToTitleCase, ToUpperCamelCase};

/// `path` relative to the app root, for messages.
fn shown(path: &Path) -> String {
    let cwd = std::env::current_dir().unwrap_or_default();
    path.strip_prefix(&cwd)
        .unwrap_or(path)
        .display()
        .to_string()
}

pub(crate) fn write_new(path: &Path, contents: &str) -> Result<()> {
    if path.exists() {
        bail!("{} already exists", shown(path));
    }
    if let Some(dir) = path.parent() {
        fs::create_dir_all(dir).with_context(|| format!("could not create {}", dir.display()))?;
    }
    fs::write(path, contents).with_context(|| format!("could not write {}", path.display()))?;
    crate::format::touched(path);
    println!("Created {}", shown(path));
    Ok(())
}

pub(crate) fn check_name(name: &str) -> Result<()> {
    let ok = name.starts_with(|c: char| c.is_ascii_alphabetic())
        && name
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-');
    if !ok {
        bail!(
            "`{name}` is not a valid name: use letters, digits, `_` or `-`, starting with a letter"
        );
    }
    let snake = name.to_snake_case();
    if is_reserved(&snake) {
        bail!("`{name}` can't be used: `{snake}` is a Rust keyword or a crate the app uses");
    }
    Ok(())
}

/// Rust keywords (current and reserved) and crate names an app's modules
/// and crate must not take.
pub(crate) fn is_reserved(word: &str) -> bool {
    const RESERVED: &[&str] = &[
        "as",
        "async",
        "await",
        "break",
        "const",
        "continue",
        "crate",
        "dyn",
        "else",
        "enum",
        "extern",
        "false",
        "fn",
        "for",
        "gen",
        "if",
        "impl",
        "in",
        "let",
        "loop",
        "match",
        "mod",
        "move",
        "mut",
        "pub",
        "ref",
        "return",
        "self",
        "static",
        "struct",
        "super",
        "trait",
        "true",
        "type",
        "unsafe",
        "use",
        "where",
        "while",
        "abstract",
        "become",
        "box",
        "do",
        "final",
        "macro",
        "override",
        "priv",
        "try",
        "typeof",
        "unsized",
        "virtual",
        "yield",
        "std",
        "core",
        "alloc",
        "renox",
        "renox_core",
        "renox_macros",
        "serde",
        "tokio",
        "test",
    ];
    RESERVED.contains(&word)
}

/// Adds `pub mod {name};` to a `mod.rs`, after its other module lines.
pub(crate) fn add_mod(mod_rs: &Path, name: &str) -> Result<()> {
    let source = fs::read_to_string(mod_rs).unwrap_or_default();
    let line = format!("pub mod {name};");
    if source
        .lines()
        .any(|l| l.trim() == line || l.trim() == format!("mod {name};"))
    {
        return Ok(());
    }
    let mut lines: Vec<&str> = source.lines().collect();
    let at = lines
        .iter()
        .rposition(|l| l.starts_with("pub mod ") || l.starts_with("mod "))
        .map_or(0, |i| i + 1);
    lines.insert(at, &line);
    let mut out = lines.join("\n");
    out.push('\n');
    if let Some(dir) = mod_rs.parent() {
        fs::create_dir_all(dir)?;
    }
    fs::write(mod_rs, out)?;
    crate::format::touched(mod_rs);
    println!("Updated {}", shown(mod_rs));
    Ok(())
}

fn module_dir(root: &Path, module: &str) -> Result<PathBuf> {
    let dir = root.join("src/app").join(module);
    if !dir.join("mod.rs").is_file() {
        bail!("there is no module `{module}`; create it with `rnx make:module {module}`");
    }
    Ok(dir)
}

/// `rnx make:module stock_items`: a module with an index route and view.
pub fn module(root: &Path, name: &str) -> Result<()> {
    check_name(name)?;
    let snake = name.to_snake_case();
    let pascal = name.to_upper_camel_case();
    let kebab = name.to_kebab_case();
    let title = name.to_title_case();

    write_new(
        &root.join("src/app").join(&snake).join("mod.rs"),
        &format!(
            r#"use renox::prelude::*;

pub struct {pascal};

impl Module for {pascal} {{
    fn name(&self) -> &'static str {{
        "{snake}"
    }}

    fn routes(&self) -> Routes {{
        Routes::new().get("/{kebab}", index).name("{snake}.index")
    }}
}}

async fn index() -> View {{
    view("{snake}/index.html", context! {{}})
}}
"#
        ),
    )?;
    write_new(
        &root.join("resources/views").join(&snake).join("index.html"),
        &format!(
            "{{% extends \"layouts/app.html\" %}}\n\n{{% block content %}}\n<h1>{title}</h1>\n{{% endblock %}}\n"
        ),
    )?;
    add_mod(&root.join("src/app/mod.rs"), &snake)?;
    // Apps from `rnx new` build the App in src/lib.rs; older ones in main.rs.
    register_in_main(&app_file(root), &format!("app::{snake}::{pascal}"))
}

/// Adds `.module(path)` after the last `.module(` call in `lib.rs`/`main.rs`.
pub(crate) fn register_in_main(main_rs: &Path, path: &str) -> Result<()> {
    register_call(main_rs, &format!(".module({path})"))
}

/// Adds `call` (e.g. `.seeder(…)`) after the last `.module(` call in
/// `lib.rs`/`main.rs`; prints it when the file doesn't look like that.
fn register_call(main_rs: &Path, call: &str) -> Result<()> {
    let hint = || println!("Register it where the App is built: {call}");
    let Ok(source) = fs::read_to_string(main_rs) else {
        hint();
        return Ok(());
    };
    if source.contains(call) {
        return Ok(());
    }
    let mut lines: Vec<String> = source.lines().map(str::to_owned).collect();
    let Some(last) = lines
        .iter()
        .rposition(|l| l.trim_start().starts_with(".module("))
    else {
        hint();
        return Ok(());
    };
    let indent: String = lines[last]
        .chars()
        .take_while(|c| c.is_whitespace())
        .collect();
    lines.insert(last + 1, format!("{indent}{call}"));
    fs::write(main_rs, lines.join("\n") + "\n")?;
    crate::format::touched(main_rs);
    println!("Updated {} ({call})", shown(main_rs));
    Ok(())
}

/// Where the App is built: `src/lib.rs` (apps from `rnx new`) or `main.rs`.
fn app_file(root: &Path) -> PathBuf {
    let lib = root.join("src/lib.rs");
    if lib.is_file() {
        lib
    } else {
        root.join("src/main.rs")
    }
}

/// Adds `mod {name};` to the app file, after its other `mod` lines.
fn add_top_mod(root: &Path, name: &str) -> Result<()> {
    let file = app_file(root);
    let source = fs::read_to_string(&file).unwrap_or_default();
    if source
        .lines()
        .any(|l| l.trim() == format!("mod {name};") || l.trim() == format!("pub mod {name};"))
    {
        return Ok(());
    }
    let mut lines: Vec<&str> = source.lines().collect();
    let at = lines
        .iter()
        .rposition(|l| l.starts_with("mod ") || l.starts_with("pub mod "))
        .map_or(0, |i| i + 1);
    let line = format!("mod {name};");
    lines.insert(at, &line);
    fs::write(&file, lines.join("\n") + "\n")?;
    crate::format::touched(&file);
    println!("Updated {} (mod {name};)", shown(&file));
    Ok(())
}

/// Adds `call` to the module's `register`, creating that method before
/// `fn routes` when the module has none. Prints what to add when the file
/// doesn't look like a generated module.
fn register_in_module(mod_rs: &Path, call: &str) -> Result<()> {
    let hint = || println!("Register it in the module's `register`: {call}");
    let Ok(source) = fs::read_to_string(mod_rs) else {
        hint();
        return Ok(());
    };
    if source.contains(call) {
        return Ok(());
    }
    let mut lines: Vec<String> = source.lines().map(str::to_owned).collect();
    if let Some(at) = lines
        .iter()
        .position(|l| l.trim() == "fn register(&self, app: &mut Registry) {")
    {
        let indent: String = lines[at]
            .chars()
            .take_while(|c| c.is_whitespace())
            .collect();
        lines.insert(at + 1, format!("{indent}    {call}"));
    } else if let Some(at) = lines
        .iter()
        .position(|l| l.trim() == "fn routes(&self) -> Routes {")
    {
        let indent: String = lines[at]
            .chars()
            .take_while(|c| c.is_whitespace())
            .collect();
        let method = [
            format!("{indent}fn register(&self, app: &mut Registry) {{"),
            format!("{indent}    {call}"),
            format!("{indent}}}"),
            String::new(),
        ];
        lines.splice(at..at, method);
    } else {
        hint();
        return Ok(());
    }
    fs::write(mod_rs, lines.join("\n") + "\n")?;
    crate::format::touched(mod_rs);
    println!("Updated {} ({call})", shown(mod_rs));
    Ok(())
}

/// `rnx make:model Product [--module products] [--migration] [--key ulid]`.
pub fn model(
    root: &Path,
    name: &str,
    module: Option<&str>,
    migration: bool,
    key: crate::KeyType,
) -> Result<()> {
    check_name(name)?;
    let pascal = name.to_upper_camel_case();
    let snake = name.to_snake_case();
    // Tables are plural, as `make:module --resource`, the docs and the
    // examples have them (#127): `WaitlistSignup` → `waitlist_signups`.
    let table = crate::scaffold::plural(&snake);
    let module = module.map_or_else(|| snake.clone(), |m| m.to_snake_case());
    let dir = module_dir(root, &module)?;
    let file = if dir.join("model.rs").exists() {
        snake.clone()
    } else {
        "model".into()
    };

    let (import, id_doc) = match key {
        crate::KeyType::Integer => ("", ""),
        crate::KeyType::Ulid => (
            "use renox::db::Ulid;\n",
            "    /// Made on insert (`Ulid::default()` means \"not saved yet\").\n",
        ),
        crate::KeyType::Uuid => (
            "use renox::uuid::Uuid;\n",
            "    /// A UUID v7 made on insert (needs renox's `uuid` feature).\n",
        ),
        crate::KeyType::String => (
            "",
            "    /// Set by the app: save a new row with `insert` or `create`.\n",
        ),
    };
    let id_type = key.rust_type();
    write_new(
        &dir.join(format!("{file}.rs")),
        &format!(
            r#"{import}use renox::prelude::*;
use serde::{{Deserialize, Serialize}};

#[derive(Model, Serialize, Deserialize, Default, Debug, Clone)]
#[model(table = "{table}")]
pub struct {pascal} {{
{id_doc}    pub id: {id_type},
    pub created_at: Option<DateTime>,
    pub updated_at: Option<DateTime>,
}}
"#
        ),
    )?;
    add_mod(&dir.join("mod.rs"), &file)?;
    if migration {
        crate::make::migration_keyed(
            &format!("create_{table}_table"),
            &root.join("migrations"),
            key,
        )?;
    }
    if key == crate::KeyType::Uuid {
        println!(
            "Uuid keys need renox's `uuid` feature: renox = {{ …, features = [\"uuid\"] }} in Cargo.toml"
        );
    }
    Ok(())
}

/// `rnx make:job SendReceipt --module orders`.
pub fn job(root: &Path, name: &str, module: &str) -> Result<()> {
    check_name(name)?;
    let pascal = name.to_upper_camel_case();
    let snake = name.to_snake_case();
    let module = module.to_snake_case();
    let dir = module_dir(root, &module)?;
    write_new(
        &dir.join(format!("{snake}.rs")),
        &format!(
            r#"use renox::prelude::*;
use serde::{{Deserialize, Serialize}};

#[derive(Serialize, Deserialize)]
pub struct {pascal} {{}}

impl Job for {pascal} {{
    const NAME: &'static str = "{kebab}";

    async fn handle(self, ctx: JobContext) -> Result {{
        let _ = ctx;
        Ok(())
    }}
}}
"#,
            kebab = name.to_kebab_case()
        ),
    )?;
    add_mod(&dir.join("mod.rs"), &snake)?;
    register_in_module(
        &dir.join("mod.rs"),
        &format!("app.job::<{snake}::{pascal}>();"),
    )
}

/// `rnx make:command admin:create --module users`: an app command in the
/// module, run as `my-app admin:create …`.
pub fn command(root: &Path, name: &str, module: &str) -> Result<()> {
    let valid = name.split(':').all(|part| {
        part.starts_with(|c: char| c.is_ascii_lowercase())
            && part
                .chars()
                .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-' || c == '_')
    });
    if !valid {
        bail!(
            "`{name}` is not a valid command name: use lowercase words separated by `:`, e.g. admin:create"
        );
    }
    let snake = name.replace([':', '-'], "_");
    if is_reserved(&snake) {
        bail!("`{name}` can't be used: `{snake}` is a Rust keyword or a crate the app uses");
    }
    let module = module.to_snake_case();
    let dir = module_dir(root, &module)?;
    let pascal = snake.to_upper_camel_case();
    write_new(
        &dir.join(format!("{snake}.rs")),
        &format!(
            r#"use renox::clap;
use renox::command::AppCommand;
use renox::prelude::*;

/// What it does (shown in `my-app help`).
#[derive(clap::Parser)]
#[command(name = "{name}")]
pub struct {pascal} {{
    /// Show what would happen without changing anything.
    #[arg(long)]
    pub dry_run: bool,
}}

impl AppCommand for {pascal} {{
    /// `my-app {name} [--dry-run]`; `--help` lists the arguments. Ask for what's
    /// missing with `renox::prompt::ask(…)`.
    async fn run(self, state: AppState) -> Result {{
        let _ = state;
        println!("{name}: done{{}}", if self.dry_run {{ " (dry run)" }} else {{ "" }});
        Ok(())
    }}
}}
"#
        ),
    )?;
    add_mod(&dir.join("mod.rs"), &snake)?;
    register_in_module(
        &dir.join("mod.rs"),
        &format!("app.typed_command::<{snake}::{pascal}>();"),
    )
}

/// `rnx make:policy Product --module products`: `impl Policy` for a model.
pub fn policy(root: &Path, model: &str, module: &str) -> Result<()> {
    check_name(model)?;
    let pascal = model.to_upper_camel_case();
    let module = module.to_snake_case();
    let dir = module_dir(root, &module)?;
    write_new(
        &dir.join("policy.rs"),
        &format!(
            r#"use renox::prelude::*;

use super::model::{pascal};

impl Policy for {pascal} {{
    fn allows(&self, user: &User, ability: &str) -> bool {{
        let _ = user;
        match ability {{
            "view" => true,
            // e.g. "update" | "delete" => self.user_id == user.id,
            _ => false,
        }}
    }}
}}
"#
        ),
    )?;
    add_mod(&dir.join("mod.rs"), "policy")
}

/// `rnx make:mail order_shipped`: an HTML and a text template.
pub fn mail(root: &Path, name: &str) -> Result<()> {
    check_name(name)?;
    let snake = name.to_snake_case();
    let dir = root.join("resources/views/mail");
    write_new(
        &dir.join(format!("{snake}.html")),
        r#"{% extends "renox/mail/layout.html" %}
{% from "renox/mail/button.html" import button %}
{% block content %}
<p>Hello!</p>
{{ button(app.url, "Open " ~ app.name) }}
{% endblock %}
"#,
    )?;
    write_new(
        &dir.join(format!("{snake}.txt")),
        "Hello!\n\n{{ app.url }}\n",
    )?;
    println!(
        "Send it with: state.queue_mail(state.mail_view(to, subject, \"mail/{snake}\", context! {{}})?).await?\n\
         (or state.mailer.send(…) to send it now)"
    );
    Ok(())
}

/// `rnx make:factory Product --module products`: fake records for seeders and tests.
pub fn factory(root: &Path, model: &str, module: &str) -> Result<()> {
    check_name(model)?;
    let pascal = model.to_upper_camel_case();
    let snake = model.to_snake_case();
    let module = module.to_snake_case();
    let dir = module_dir(root, &module)?;
    let from = if dir.join(format!("{snake}.rs")).is_file() {
        snake.clone()
    } else {
        "model".into()
    };
    write_new(
        &dir.join(format!("{snake}_factory.rs")),
        &format!(
            r#"use renox::fake::Fake;
use renox::fake::faker::lorem::en::Word;
use renox::prelude::*;

use super::{from}::{pascal};

/// Fake records: `{pascal}::factory().make_one()` (unsaved),
/// `{pascal}::factory().create_one(&db)`, `{pascal}::factory().count(20).create(&db)`.
impl Factory for {pascal} {{
    fn definition() -> Self {{
        let _word: String = Word().fake(); // fill the fields with fake data
        {pascal} {{
            ..Default::default()
        }}
    }}
}}
"#
        ),
    )?;
    add_mod(&dir.join("mod.rs"), &format!("{snake}_factory"))
}

/// `rnx make:seeder DemoData`: a seeder for `db:seed`, registered on the App.
pub fn seeder(root: &Path, name: &str) -> Result<()> {
    check_name(name)?;
    let snake = name.to_snake_case();
    write_new(
        &root.join(format!("src/seeders/{snake}.rs")),
        r#"use renox::prelude::*;

/// Run by `rnx db:seed` (and `migrate:fresh --seed`), in the app's context:
/// `renox::context::app()` gives the config, `encrypt`, the cache.
pub async fn run(state: AppState) -> Result {
    let _ = state; // e.g. User::register(&state.db, "Admin", "admin@example.com", "password123").await?;
    Ok(())
}
"#,
    )?;
    add_mod(&root.join("src/seeders/mod.rs"), &snake)?;
    add_top_mod(root, "seeders")?;
    register_call(&app_file(root), &format!(".seeder(seeders::{snake}::run)"))
}

/// `rnx make:test Checkout`: an integration test file.
pub fn test(root: &Path, name: &str) -> Result<()> {
    check_name(name)?;
    let snake = name.to_snake_case();
    let crate_name = crate::scaffold::crate_name(root)?;
    write_new(
        &root.join(format!("tests/{snake}.rs")),
        &format!(
            r#"use renox::prelude::*;
use renox::testing::TestApp;

#[renox::test]
async fn {snake}_works() {{
    let app = TestApp::new({crate_name}::app()).await;
    let user = User::register(app.db(), "Test", "test@example.com", "password123")
        .await
        .unwrap();
    app.acting_as(&user);
    app.get("/").await.assert_ok();
    // Also: app.post(..), assert_redirect, assert_see, assert_view,
    // assert_database_has, app.fake_events(), app.fake_notifications(),
    // app.fake_http(), app.travel(..), app.run_jobs().
}}
"#
        ),
    )?;
    Ok(())
}

/// `rnx make:notification OrderShipped --module orders`.
pub fn notification(root: &Path, name: &str, module: &str) -> Result<()> {
    check_name(name)?;
    let pascal = name.to_upper_camel_case();
    let snake = name.to_snake_case();
    let kebab = name.to_kebab_case();
    let module = module.to_snake_case();
    let dir = module_dir(root, &module)?;
    write_new(
        &dir.join(format!("{snake}.rs")),
        &format!(
            r#"use renox::auth::{{Channel, Notification, Recipient}};
use renox::mail::Mail;
use renox::prelude::*;

/// Send it with `state.notify(&user, &{pascal} {{ .. }})` (now) or
/// `state.notify_later(&user, &…)` (through the queue).
pub struct {pascal} {{
    pub id: i64,
}}

impl Notification for {pascal} {{
    fn kind(&self) -> &'static str {{
        "{kebab}"
    }}

    fn channels(&self, _to: &Recipient) -> Vec<Channel> {{
        vec![Channel::Mail, Channel::Database]
    }}

    fn to_mail(&self, to: &Recipient, state: &AppState) -> Result<Mail> {{
        // Written in the recipient's language: t() in a mail view, or
        // state.current_lang().t(..) here.
        let _ = state;
        Ok(Mail::new(to.email().unwrap_or_default(), "{title}", "…"))
    }}

    fn to_database(&self, _to: &Recipient, _state: &AppState) -> Result<renox::serde_json::Value> {{
        Ok(json!({{ "id": self.id }}))
    }}
}}
"#,
            title = name.to_title_case()
        ),
    )?;
    add_mod(&dir.join("mod.rs"), &snake)
}

/// `rnx make:event OrderPlaced --module orders`: the event and a listener.
pub fn event(root: &Path, name: &str, module: &str) -> Result<()> {
    check_name(name)?;
    let pascal = name.to_upper_camel_case();
    let snake = name.to_snake_case();
    let module = module.to_snake_case();
    let dir = module_dir(root, &module)?;
    write_new(
        &dir.join(format!("{snake}.rs")),
        &format!(
            r#"use renox::prelude::*;

/// Emit it with `state.emit({pascal} {{ .. }}).await?`; the listeners
/// registered in the module's `register` run in turn.
#[derive(Clone, Debug)]
pub struct {pascal} {{
    pub id: i64,
}}

impl Event for {pascal} {{}}
"#
        ),
    )?;
    add_mod(&dir.join("mod.rs"), &snake)?;
    register_in_module(
        &dir.join("mod.rs"),
        &format!(
            "app.listen(|event: {snake}::{pascal}, _state| async move {{ let _ = event.id; Ok(()) }});"
        ),
    )
}

/// `rnx make:rule TaxId --module invoices`: a validation rule to `apply`.
pub fn rule(root: &Path, name: &str, module: &str) -> Result<()> {
    check_name(name)?;
    let pascal = name.to_upper_camel_case();
    let snake = name.to_snake_case();
    let module = module.to_snake_case();
    let dir = module_dir(root, &module)?;
    write_new(
        &dir.join(format!("{snake}.rs")),
        &format!(
            r#"use renox::validation::{{Inspected, Rule}};

/// `v.field("{snake}", &self.{snake}).required().apply(&{pascal})`.
pub struct {pascal};

impl Rule for {pascal} {{
    fn check(&self, value: &Inspected) -> std::result::Result<(), String> {{
        let Inspected::Text(text) = value else {{
            return Ok(());
        }};
        if text.trim().is_empty() {{
            return Err("The :attribute is not valid.".into()); // :attribute is the field's label
        }}
        Ok(())
    }}
}}
"#
        ),
    )?;
    add_mod(&dir.join("mod.rs"), &snake)
}

/// `rnx make:middleware StampRequests`: a middleware on every route.
pub fn middleware(root: &Path, name: &str) -> Result<()> {
    check_name(name)?;
    let snake = name.to_snake_case();
    write_new(
        &root.join(format!("src/middleware/{snake}.rs")),
        r#"use renox::axum::extract::Request;
use renox::axum::middleware::Next;
use renox::prelude::*;

/// Runs around every route of the app's modules (`App::layer`), after the
/// session and the user are loaded: take `Option<AuthUser>`, `Session`… as
/// arguments before `req`.
pub async fn handle(req: Request, next: Next) -> Response {
    // Before the handler: e.g. renox::context::set(..), or return early.
    let res = next.run(req).await;
    // After: e.g. add a header.
    res
}
"#,
    )?;
    add_mod(&root.join("src/middleware/mod.rs"), &snake)?;
    add_top_mod(root, "middleware")?;
    register_call(
        &app_file(root),
        &format!(".layer(renox::axum::middleware::from_fn(middleware::{snake}::handle))"),
    )
}

pub fn component(root: &Path, name: &str) -> Result<()> {
    check_name(name)?;
    let snake = name.to_snake_case();
    let path = root.join(format!("resources/views/components/{snake}.html"));
    let body = COMPONENT.replace("NAME", &snake);
    write_new(&path, &body)?;
    println!("Use it with: {{% from \"components/{snake}.html\" import {snake} %}}");
    Ok(())
}

const COMPONENT: &str = r#"{#- {% from "components/NAME.html" import NAME %}{{ NAME("field", "Label") }}
    A component sees the request like the page does: old(), error(), t(),
    csrf_field(), can(), auth, request. -#}
{% macro NAME(name, label) -%}
<div class="rx-field">
  <label class="rx-label" for="rx-{{ name }}">{{ label }}</label>
  <input class="rx-input" id="rx-{{ name }}" name="{{ name }}" value="{{ old(name) }}"
    {%- if error(name) %} aria-invalid="true"{% endif %} aria-describedby="rx-{{ name }}-error">
  <p class="rx-error" id="rx-{{ name }}-error" data-error-for="{{ name }}" aria-live="polite">{{ error(name) }}</p>
</div>
{%- endmacro %}
"#;

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

    fn app() -> tempfile::TempDir {
        let dir = tempfile::tempdir().unwrap();
        fs::create_dir_all(dir.path().join("src/app/home")).unwrap();
        fs::write(dir.path().join("src/app/mod.rs"), "pub mod home;\n").unwrap();
        fs::write(
            dir.path().join("src/app/home/mod.rs"),
            "use renox::prelude::*;\n",
        )
        .unwrap();
        fs::write(
            dir.path().join("src/main.rs"),
            "mod app;\n\nfn main() -> renox::Result {\n    renox::App::new()\n        .migrations(renox::migrations!())\n        .module(app::home::Home)\n        .run()\n}\n",
        )
        .unwrap();
        dir
    }

    fn read(dir: &tempfile::TempDir, path: &str) -> String {
        fs::read_to_string(dir.path().join(path)).unwrap()
    }

    #[test]
    fn modules_are_created_and_registered() {
        let dir = app();
        module(dir.path(), "stock_items").unwrap();
        let code = read(&dir, "src/app/stock_items/mod.rs");
        assert!(code.contains("pub struct StockItems;"));
        assert!(
            code.contains(r#"Routes::new().get("/stock-items", index).name("stock_items.index")"#)
        );
        assert!(
            read(&dir, "resources/views/stock_items/index.html").contains("<h1>Stock Items</h1>")
        );
        assert_eq!(
            read(&dir, "src/app/mod.rs"),
            "pub mod home;\npub mod stock_items;\n"
        );
        assert!(read(&dir, "src/main.rs").contains(
            "        .module(app::home::Home)\n        .module(app::stock_items::StockItems)\n        .run()"
        ));
        assert!(
            module(dir.path(), "stock_items").is_err(),
            "never overwrites"
        );
        assert!(module(dir.path(), "../evil").is_err());
    }

    #[test]
    fn models_jobs_policies_and_mails() {
        let dir = app();
        module(dir.path(), "product").unwrap();
        model(dir.path(), "Product", None, true, crate::KeyType::Integer).unwrap();
        let code = read(&dir, "src/app/product/model.rs");
        // Plural tables, as `--resource` and the docs have them (#127).
        assert!(
            code.contains(r#"#[model(table = "products")]"#)
                && code.contains("pub struct Product {"),
            "{code}"
        );
        let mut migrations: Vec<String> = fs::read_dir(dir.path().join("migrations"))
            .unwrap()
            .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
            .collect();
        migrations.sort();
        assert_eq!(migrations.len(), 2, "up and down");
        assert!(
            migrations[0].ends_with("_create_products_table.down.sql"),
            "{migrations:?}"
        );
        let up = read(&dir, &format!("migrations/{}", migrations[1]));
        assert!(up.contains(r#"CREATE TABLE "products""#), "{up}");

        model(
            dir.path(),
            "Category",
            Some("product"),
            false,
            crate::KeyType::Integer,
        )
        .unwrap();
        model(
            dir.path(),
            "Invoice",
            Some("product"),
            false,
            crate::KeyType::Ulid,
        )
        .unwrap();
        let invoice = read(&dir, "src/app/product/invoice.rs");
        assert!(
            invoice.starts_with("use renox::db::Ulid;\n")
                && invoice.contains("    pub id: Ulid,\n"),
            "{invoice}"
        );
        assert!(
            dir.path().join("src/app/product/category.rs").exists(),
            "model.rs is taken"
        );
        assert!(read(&dir, "src/app/product/category.rs").contains(r#"table = "categories""#));
        model(
            dir.path(),
            "WaitlistSignup",
            Some("product"),
            false,
            crate::KeyType::Integer,
        )
        .unwrap();
        assert!(
            read(&dir, "src/app/product/waitlist_signup.rs")
                .contains(r#"#[model(table = "waitlist_signups")]"#)
        );

        job(dir.path(), "MailReceipt", "product").unwrap();
        assert!(
            read(&dir, "src/app/product/mail_receipt.rs")
                .contains(r#"const NAME: &'static str = "mail-receipt";"#)
        );
        command(dir.path(), "stock:import", "product").unwrap();
        assert!(
            read(&dir, "src/app/product/stock_import.rs")
                .contains("impl AppCommand for StockImport")
        );
        assert!(command(dir.path(), "Bad Name", "product").is_err());
        assert!(command(dir.path(), "self", "product").is_err());
        policy(dir.path(), "Product", "product").unwrap();
        assert!(read(&dir, "src/app/product/policy.rs").contains("impl Policy for Product"));

        let mods = read(&dir, "src/app/product/mod.rs");
        for m in [
            "pub mod model;",
            "pub mod category;",
            "pub mod mail_receipt;",
            "pub mod stock_import;",
            "pub mod policy;",
        ] {
            assert!(mods.contains(m), "{mods}");
        }
        assert!(
            model(
                dir.path(),
                "Order",
                Some("nope"),
                false,
                crate::KeyType::Integer
            )
            .is_err(),
            "unknown module"
        );

        mail(dir.path(), "order_shipped").unwrap();
        assert!(
            read(&dir, "resources/views/mail/order_shipped.html")
                .contains("renox/mail/layout.html")
        );
        assert!(
            dir.path()
                .join("resources/views/mail/order_shipped.txt")
                .exists()
        );
    }

    #[test]
    fn jobs_and_commands_register_themselves_in_their_module() {
        let dir = app();
        module(dir.path(), "orders").unwrap();
        job(dir.path(), "SendReceipt", "orders").unwrap();
        command(dir.path(), "orders:close", "orders").unwrap();
        job(dir.path(), "SendReceipt", "orders").unwrap_err(); // the file exists
        let code = read(&dir, "src/app/orders/mod.rs");
        assert!(
            code.contains(
                "    fn register(&self, app: &mut Registry) {\n        \
             app.typed_command::<orders_close::OrdersClose>();\n        \
             app.job::<send_receipt::SendReceipt>();\n    }\n\n    fn routes(&self) -> Routes {"
            ),
            "{code}"
        );
        assert_eq!(code.matches("fn register").count(), 1);
    }

    #[test]
    fn components_use_the_request_helpers() {
        let dir = app();
        component(dir.path(), "PriceTag").unwrap();
        let body = read(&dir, "resources/views/components/price_tag.html");
        assert!(body.contains("{% macro price_tag(name, label) -%}"));
        assert!(body.contains("{{ old(name) }}") && body.contains("{{ error(name) }}"));
        assert!(component(dir.path(), "PriceTag").is_err(), "no overwrite");
    }
}