luxid-cli 0.1.1

In-application command line for the Luxid web framework
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
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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
//! File generation for `luxid new` and `luxid make:model`.
//!
//! Generation is split into planning and writing. A [`Plan`] is a pure value —
//! which files, with what contents, and which lines to insert into existing
//! ones — so the interesting logic is testable without touching a disk.
//!
//! Writing never overwrites. A generator that clobbers hand-written code once
//! is a generator nobody runs again.

use std::collections::BTreeSet;
use std::path::{Path, PathBuf};

use luxid_core::error::{Error, Result};

use crate::naming::Names;

/// A file to create.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NewFile {
    pub path: PathBuf,
    pub contents: String,
}

/// A line to insert after a marker in an existing file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Insertion {
    pub path: PathBuf,
    pub marker: &'static str,
    pub line: String,
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Plan {
    pub files: Vec<NewFile>,
    pub insertions: Vec<Insertion>,
}

impl Plan {
    fn file(&mut self, path: impl Into<PathBuf>, contents: impl Into<String>) {
        self.files.push(NewFile {
            path: path.into(),
            contents: contents.into(),
        });
    }

    fn insert(&mut self, path: impl Into<PathBuf>, marker: &'static str, line: impl Into<String>) {
        self.insertions.push(Insertion {
            path: path.into(),
            marker,
            line: line.into(),
        });
    }

    /// Every path this plan touches, for reporting.
    pub fn touched(&self) -> BTreeSet<&Path> {
        self.files
            .iter()
            .map(|file| file.path.as_path())
            .chain(
                self.insertions
                    .iter()
                    .map(|insertion| insertion.path.as_path()),
            )
            .collect()
    }
}

// ---------------------------------------------------------------- markers

pub const MARK_MODULES: &str = "// <luxid:modules>";
pub const MARK_ROUTES: &str = "// <luxid:routes>";
pub const MARK_MIGRATIONS: &str = "// <luxid:migrations>";
pub const MARK_MIGRATION_MODULES: &str = "// <luxid:migration-modules>";

// ------------------------------------------------------------- luxid new

/// How the generated app should depend on Luxid.
#[derive(Debug, Clone)]
pub enum Dependency {
    /// A published version.
    Version(String),
    /// A local checkout, for working on the framework itself.
    Path(String),
}

impl Dependency {
    fn luxid(&self) -> String {
        match self {
            Self::Version(version) => format!("luxid = \"{version}\""),
            Self::Path(root) => format!("luxid = {{ path = \"{root}/crates/luxid\" }}"),
        }
    }

    fn testing(&self) -> String {
        match self {
            Self::Version(version) => format!("luxid-testing = \"{version}\""),
            Self::Path(root) => {
                format!("luxid-testing = {{ path = \"{root}/crates/luxid-testing\" }}")
            }
        }
    }
}

pub fn new_app(name: &str, dependency: &Dependency) -> Plan {
    let mut plan = Plan::default();
    let crate_name = crate::naming::to_snake(name);

    plan.file(
        "Cargo.toml",
        format!(
            r#"[package]
name = "{crate_name}"
version = "0.1.0"
edition = "2024"

[dependencies]
{}
migration = {{ path = "migration" }}

tokio = {{ version = "1", features = ["macros", "rt-multi-thread"] }}
serde = {{ version = "1", features = ["derive"] }}
serde_json = "1"
sea-orm = {{ version = "2.0", features = [
    "macros",
    "runtime-tokio-rustls",
    "sqlx-sqlite",
    "sqlx-postgres",
    "with-chrono",
] }}
dotenvy = "0.15"

# Declared directly because their derive macros emit crate-qualified paths:
# re-exporting them through `luxid` is not enough for `#[derive(..)]` to resolve.
schemars = "1.2"

[dev-dependencies]
{}

[profile.dev]
opt-level = 0

# Dependencies are built optimised once; your own crate stays quick to rebuild.
[profile.dev.package."*"]
opt-level = 3
"#,
            dependency.luxid(),
            dependency.testing(),
        ),
    );

    plan.file(
        "migration/Cargo.toml",
        r#"[package]
name = "migration"
version = "0.1.0"
edition = "2024"
publish = false

[dependencies]
sea-orm-migration = { version = "2.0", default-features = false, features = [
    "runtime-tokio-rustls",
    "sqlx-sqlite",
    "sqlx-postgres",
] }
"#,
    );

    plan.file(
        "migration/src/lib.rs",
        format!(
            r#"pub use sea_orm_migration::prelude::*;

{MARK_MIGRATION_MODULES}

pub struct Migrator;

#[async_trait::async_trait]
impl MigratorTrait for Migrator {{
    fn migrations() -> Vec<Box<dyn MigrationTrait>> {{
        vec![
            {MARK_MIGRATIONS}
        ]
    }}
}}
"#
        ),
    );

    plan.file(
        "src/main.rs",
        r#"mod app;
mod config;
mod controllers;
mod entities;
mod factories;
mod middleware;
mod models;
mod policies;
mod routes;
mod seeders;
mod services;
mod validators;

#[tokio::main]
async fn main() -> luxid::Result<()> {
    let _ = dotenvy::dotenv();

    // `cargo run` serves; `cargo run -- routes`, `-- migrate`, `-- openapi`
    // and friends are all the same binary.
    luxid::cli::run::<migration::Migrator>(app::build().await?).await
}
"#,
    );

    plan.file(
        "src/app.rs",
        r#"use luxid::prelude::*;

/// Assemble the application: configuration, services, middleware, routes.
pub async fn build() -> luxid::Result<App> {
    // luxid.toml, with the environment layered over it.
    let config = Config::load("luxid.toml")?;

    // Accessing a relation that was not eager-loaded becomes an error, which
    // turns an N+1 into a failing test rather than a production surprise.
    luxid::set_strict_relations(config.get_or("database.strict_relations", cfg!(debug_assertions))?);

    // SQLite by default so a fresh app runs with no infrastructure. Point
    // DATABASE_URL at Postgres when you want one.
    let url = config
        .get_or("database.url", "sqlite://./app.db?mode=rwc".to_owned())?;

    let db = Db::connect(url).await?;

    Ok(App::new()
        .config(config)
        .providers(providers(db))
        .middleware(WithDatabase)
        .routes(crate::routes::register))
}

fn providers(db: Db) -> Providers {
    Providers::new().singleton(move |_| db.clone())
}
"#,
    );

    plan.file(
        "src/routes.rs",
        format!(
            r#"use luxid::prelude::*;

use crate::controllers;

pub fn register(r: &mut Router) {{
    r.group("/api", |r| {{
        r.get("/health", controllers::health_controller::HealthController::show);

        {MARK_ROUTES}
    }});
}}
"#
        ),
    );

    plan.file(
        "src/controllers/mod.rs",
        format!("pub mod health_controller;\n\n{MARK_MODULES}\n"),
    );

    plan.file(
        "src/controllers/health_controller.rs",
        r#"use luxid::prelude::*;
use serde_json::json;

pub struct HealthController;

#[luxid::controller]
impl HealthController {
    #[openapi(summary = "Liveness probe", tag = "system")]
    async fn show(ctx: HttpContext) -> Result<Response> {
        ctx.response.ok(json!({ "status": "ok" }))
    }
}
"#,
    );

    // `factories` and `seeders` are here because `make:model` registers into
    // them; a plan that inserts into a file nobody created fails at write time.
    for module in ["models", "entities", "validators", "services", "middleware"] {
        plan.file(format!("src/{module}/mod.rs"), format!("{MARK_MODULES}\n"));
    }

    // These are called from tests, seed commands and controllers you have not
    // written yet — none of which the binary target can see. Without this a
    // freshly generated app greets you with dead-code warnings, which is a good
    // way to teach people to ignore warnings.
    for module in ["policies", "factories", "seeders"] {
        plan.file(
            format!("src/{module}/mod.rs"),
            format!("#![allow(dead_code)]\n\n{MARK_MODULES}\n"),
        );
    }

    plan.file(
        "src/config/mod.rs",
        r#"//! Application configuration. Read from the environment here, and bind the
//! result as a singleton in `app::providers` so actions can resolve it.
"#,
    );

    plan.file(
        ".env.example",
        r#"# Copy to .env and adjust. These override luxid.toml.
DATABASE_URL=sqlite://./app.db?mode=rwc
APP_KEY=change-me-before-deploying
LUXID_ADDR=127.0.0.1:3000
"#,
    );

    plan.file(".gitignore", "/target\n/app.db\n/app.db-*\n.env\n");

    plan.file(
        "luxid.toml",
        format!(
            r#"# Read at boot by `app::build`, and available in any action as
# `ctx.config`. Environment variables override these: `app.name` is also
# `APP_NAME`, `database.url` is also `DATABASE_URL`.

[app]
name = "{crate_name}"
per_page = 20

[database]
# url = "postgres://localhost/{crate_name}"

# Reading a relation that was not eager-loaded becomes an error.
strict_relations = true
"#
        ),
    );

    // Shipped commented out on purpose. A faster linker is the biggest single
    // lever on rebuild time, but enabling it by default produces a project that
    // does not compile on any machine without mold — and this file gets
    // committed, so it would break teammates too.
    plan.file(
        ".cargo/config.toml",
        r#"# A faster linker is the single biggest lever on rebuild time.
#
# Uncomment after installing mold (https://github.com/rui314/mold), or swap in
# `lld`. Left off by default so this project builds anywhere.
#
# [target.x86_64-unknown-linux-gnu]
# rustflags = ["-C", "link-arg=-fuse-ld=mold", "-C", "debuginfo=1"]
"#,
    );

    plan.file(
        "README.md",
        format!(
            r#"# {crate_name}

```sh
cargo run                 # serve on http://127.0.0.1:3000
cargo run -- routes       # the routing table
cargo run -- migrate      # apply migrations
cargo run -- openapi      # the OpenAPI 3.1 document
```

Generated by `luxid new`.
"#
        ),
    );

    plan
}

// -------------------------------------------------------- luxid make:model

/// Which artefacts `make:model` should produce.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct ModelFlags {
    pub migration: bool,
    pub factory: bool,
    pub seeder: bool,
    pub controller: bool,
    pub policy: bool,
    pub requests: bool,
}

impl ModelFlags {
    /// `-a`: everything.
    pub fn all() -> Self {
        Self {
            migration: true,
            factory: true,
            seeder: true,
            controller: true,
            policy: true,
            requests: true,
        }
    }
}

pub fn make_model(names: &Names, flags: ModelFlags, timestamp: &str) -> Plan {
    let mut plan = Plan::default();
    let Names {
        model,
        snake,
        plural,
        ..
    } = names;

    plan.file(
        format!("src/models/{snake}.rs"),
        format!(
            r#"pub use crate::entities::{plural}::Model as {model};

// Relations, scopes and hooks go here. Declare relations in the attribute and
// scopes as `#[scope]` functions:
//
//     #[luxid::model(has_many(posts = Post, fk = "{snake}_id"))]
//     impl {model} {{
//         #[scope]
//         fn active(query: Query<crate::entities::{plural}::Entity>)
//             -> Query<crate::entities::{plural}::Entity>
//         {{
//             query.where_null({model}::deleted_at)
//         }}
//     }}
#[luxid::model()]
impl {model} {{}}
"#
        ),
    );
    plan.insert(
        "src/models/mod.rs",
        MARK_MODULES,
        format!("pub mod {snake};"),
    );

    // The entity is regenerated from the schema by `luxid db:sync`; this stub
    // exists so the app compiles before the first migration has run.
    plan.file(
        format!("src/entities/{plural}.rs"),
        format!(
            r#"//! Generated from the database schema. Re-run `luxid db:sync` after a
//! migration rather than editing this by hand.

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, serde::Serialize, luxid::Model)]
#[sea_orm(table_name = "{plural}")]
pub struct Model {{
    // <luxid:fields>  refreshed by `cargo run -- db:sync`
    #[sea_orm(primary_key)]
    pub id: i64,
    // </luxid:fields>
    #[sea_orm(ignore)]
    #[serde(flatten)]
    pub relations: luxid::Relations,
}}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {{}}

impl ActiveModelBehavior for ActiveModel {{}}
"#
        ),
    );
    plan.insert(
        "src/entities/mod.rs",
        MARK_MODULES,
        format!("pub mod {plural};"),
    );

    if flags.migration {
        let migration = format!("m{timestamp}_create_{plural}");

        plan.file(
            format!("migration/src/{migration}.rs"),
            format!(
                r#"use sea_orm_migration::prelude::*;
use sea_orm_migration::schema::*;

#[derive(DeriveIden)]
enum {} {{
    Table,
    Id,
}}

pub struct Migration;

impl MigrationName for Migration {{
    fn name(&self) -> &str {{
        "{migration}"
    }}
}}

#[async_trait::async_trait]
impl MigrationTrait for Migration {{
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {{
        manager
            .create_table(
                Table::create()
                    .table({0}::Table)
                    .if_not_exists()
                    .col(pk_auto({0}::Id))
                    // Add your columns here.
                    .to_owned(),
            )
            .await
    }}

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {{
        manager.drop_table(Table::drop().table({0}::Table).to_owned()).await
    }}
}}
"#,
                crate::naming::to_pascal(plural)
            ),
        );

        plan.insert(
            "migration/src/lib.rs",
            MARK_MIGRATION_MODULES,
            format!("mod {migration};"),
        );
        plan.insert(
            "migration/src/lib.rs",
            MARK_MIGRATIONS,
            format!("Box::new({migration}::Migration),"),
        );
    }

    if flags.requests {
        let store = &names.store_request;
        let update = &names.update_request;

        plan.file(
            format!("src/validators/{snake}.rs"),
            format!(
                r#"use luxid::prelude::*;
use serde::Deserialize;

// `JsonSchema` lets `#[openapi(body = ..)]` describe this request.
#[derive(Debug, Deserialize, Validate, luxid::JsonSchema)]
pub struct {store} {{
    // #[validate(length(min = 1, max = 255))]
    // pub name: String,
}}

#[derive(Debug, Deserialize, Validate, luxid::JsonSchema)]
pub struct {update} {{
    // #[validate(length(min = 1, max = 255))]
    // pub name: Option<String>,
}}
"#
            ),
        );
        plan.insert(
            "src/validators/mod.rs",
            MARK_MODULES,
            format!("pub mod {snake};"),
        );
    }

    if flags.policy {
        let policy = &names.policy;

        plan.file(
            format!("src/policies/{snake}_policy.rs"),
            format!(
                r#"use luxid::prelude::*;

use crate::models::{snake}::{model};

pub struct {policy};

/// A policy is an ordinary function of `(&Auth, &T) -> bool`. Enforce one with
/// `ctx.authorize({policy}::update, &{snake})?`, or ask without consequence
/// with `ctx.can({policy}::update, &{snake})`.
impl {policy} {{
    pub fn view(_auth: &Auth, _{snake}: &{model}) -> bool {{
        true
    }}

    pub fn update(auth: &Auth, {snake}: &{model}) -> bool {{
        // Replace with your rule, e.g. comparing the owner to the identity.
        let _ = {snake};
        auth.check()
    }}
}}
"#
            ),
        );
        plan.insert(
            "src/policies/mod.rs",
            MARK_MODULES,
            format!("pub mod {snake}_policy;"),
        );
    }

    if flags.factory {
        let factory = &names.factory;

        plan.file(
            format!("src/factories/{snake}_factory.rs"),
            format!(
                r#"use luxid::prelude::*;
use sea_orm::ActiveValue::Set;

use crate::entities::{plural};

/// The typical {model}. Tests override only what they care about:
///
///     {factory}::new().create_one().await?;
///     {factory}::new().count(3).create().await?;
///     {factory}::new().state(|row| row.name = Set("Ada".into())).create_one().await?;
pub struct {factory};

impl Factory for {factory} {{
    type Active = {plural}::ActiveModel;

    fn definition() -> Self::Active {{
        {plural}::ActiveModel {{
            // <luxid:fields>  refreshed by `cargo run -- db:sync`
            // </luxid:fields>
            ..Default::default()
        }}
    }}
}}
"#
            ),
        );
        plan.insert(
            "src/factories/mod.rs",
            MARK_MODULES,
            format!("pub mod {snake}_factory;"),
        );
    }

    if flags.seeder {
        let seeder = &names.seeder;
        let factory = &names.factory;

        plan.file(
            format!("src/seeders/{snake}_seeder.rs"),
            format!(
                r#"use luxid::prelude::*;

use crate::factories::{snake}_factory::{factory};

pub struct {seeder};

impl {seeder} {{
    pub async fn run() -> luxid::Result<()> {{
        {factory}::new().count(10).create().await?;
        Ok(())
    }}
}}
"#
            ),
        );
        plan.insert(
            "src/seeders/mod.rs",
            MARK_MODULES,
            format!("pub mod {snake}_seeder;"),
        );
    }

    if flags.controller {
        let controller = &names.controller;
        let controller_file = &names.controller_file;

        plan.file(
            format!("src/controllers/{controller_file}.rs"),
            format!(
                r#"use luxid::prelude::*;

use crate::models::{snake}::{model};

pub struct {controller};

#[luxid::controller]
impl {controller} {{
    #[openapi(tag = "{plural}")]
    async fn index(ctx: HttpContext) -> Result<Response> {{
        let page = ctx.request.input::<u64>("page")?.unwrap_or(1);

        ctx.response.ok({model}::query().paginate(page, 20).await?)
    }}

    #[openapi(tag = "{plural}", errors = [404])]
    async fn show(ctx: HttpContext) -> Result<Response> {{
        let id: i64 = ctx.params.get("id")?;

        ctx.response.ok({model}::find_or_fail(id).await?)
    }}

    #[openapi(tag = "{plural}", errors = [422])]
    async fn store(ctx: HttpContext) -> Result<Response> {{
        let _input = ctx.request.validate::<crate::validators::{snake}::{}>().await?;

        ctx.response.status(501).json("not implemented")
    }}

    #[openapi(tag = "{plural}", errors = [404, 422])]
    async fn update(ctx: HttpContext) -> Result<Response> {{
        let id: i64 = ctx.params.get("id")?;
        let existing = {model}::find_or_fail(id).await?;

        ctx.authorize(crate::policies::{snake}_policy::{}::update, &existing)?;

        let _input = ctx.request.validate::<crate::validators::{snake}::{}>().await?;

        ctx.response.status(501).json("not implemented")
    }}

    #[openapi(tag = "{plural}", no_content, errors = [404])]
    async fn destroy(ctx: HttpContext) -> Result<Response> {{
        let id: i64 = ctx.params.get("id")?;
        {model}::find_or_fail(id).await?;

        ctx.response.no_content()
    }}
}}
"#,
                names.store_request, names.policy, names.update_request
            ),
        );
        plan.insert(
            "src/controllers/mod.rs",
            MARK_MODULES,
            format!("pub mod {controller_file};"),
        );

        // One line, registering only the actions the controller defines.
        plan.insert(
            "src/routes.rs",
            MARK_ROUTES,
            format!("r.resource(\"/{plural}\", controllers::{controller_file}::{controller});"),
        );
    }

    plan
}

// -------------------------------------------------------------- writing

/// Write a plan to disk.
///
/// Refuses if any file already exists, and writes nothing in that case: a
/// half-applied generator is worse than one that declined.
pub fn write(plan: &Plan, root: &Path) -> Result<Vec<PathBuf>> {
    let mut clashes = Vec::new();
    for file in &plan.files {
        if root.join(&file.path).exists() {
            clashes.push(file.path.display().to_string());
        }
    }

    if !clashes.is_empty() {
        return Err(Error::internal(format!(
            "these files already exist, so nothing was written: {}",
            clashes.join(", ")
        )));
    }

    let mut written = Vec::new();

    for file in &plan.files {
        let target = root.join(&file.path);

        if let Some(parent) = target.parent() {
            std::fs::create_dir_all(parent)
                .map_err(|err| Error::internal(format!("could not create {parent:?}: {err}")))?;
        }

        std::fs::write(&target, &file.contents)
            .map_err(|err| Error::internal(format!("could not write {target:?}: {err}")))?;

        written.push(file.path.clone());
    }

    for insertion in &plan.insertions {
        let target = root.join(&insertion.path);

        let existing = std::fs::read_to_string(&target)
            .map_err(|err| Error::internal(format!("could not read {target:?}: {err}")))?;

        let updated = apply(&existing, insertion.marker, &insertion.line).ok_or_else(|| {
            Error::internal(format!(
                "{:?} has no `{}` marker, so there is nowhere to register this. \
                 Add the marker back, or wire it up by hand.",
                insertion.path, insertion.marker
            ))
        })?;

        std::fs::write(&target, updated)
            .map_err(|err| Error::internal(format!("could not write {target:?}: {err}")))?;

        written.push(insertion.path.clone());
    }

    written.sort();
    written.dedup();

    Ok(written)
}

/// Insert `line` immediately above `marker`, matching the marker's indentation.
///
/// Above rather than below so repeated generation keeps chronological order:
/// the marker stays at the bottom of its list.
pub fn apply(source: &str, marker: &str, line: &str) -> Option<String> {
    let position = source.find(marker)?;
    let line_start = source[..position]
        .rfind('\n')
        .map(|index| index + 1)
        .unwrap_or(0);
    let indent = &source[line_start..position];

    let inserted: String = line
        .lines()
        .map(|part| format!("{indent}{part}\n"))
        .collect::<Vec<_>>()
        .join("");

    let mut out = String::with_capacity(source.len() + inserted.len());
    out.push_str(&source[..line_start]);
    out.push_str(&inserted);
    out.push_str(&source[line_start..]);

    Some(out)
}

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

    #[test]
    fn inserts_above_the_marker_with_its_indentation() {
        let source = "fn main() {\n    let x = 1;\n    // <luxid:routes>\n}\n";
        let updated = apply(source, MARK_ROUTES, "call_me();").expect("marker present");

        assert_eq!(
            updated,
            "fn main() {\n    let x = 1;\n    call_me();\n    // <luxid:routes>\n}\n"
        );
    }

    #[test]
    fn repeated_insertion_keeps_order() {
        let source = "// <luxid:modules>\n";
        let once = apply(source, MARK_MODULES, "pub mod a;").expect("marker");
        let twice = apply(&once, MARK_MODULES, "pub mod b;").expect("marker");

        assert_eq!(twice, "pub mod a;\npub mod b;\n// <luxid:modules>\n");
    }

    #[test]
    fn multi_line_insertions_are_indented_line_by_line() {
        let source = "        // <luxid:routes>\n";
        let updated = apply(source, MARK_ROUTES, "one();\ntwo();").expect("marker");

        assert_eq!(
            updated,
            "        one();\n        two();\n        // <luxid:routes>\n"
        );
    }

    #[test]
    fn a_missing_marker_is_reported_rather_than_guessed() {
        assert!(apply("fn main() {}\n", MARK_ROUTES, "x();").is_none());
    }

    #[test]
    fn a_new_app_carries_every_marker_make_model_needs() {
        let plan = new_app("my-app", &Dependency::Version("0.1".into()));

        let find = |path: &str| {
            plan.files
                .iter()
                .find(|file| file.path == Path::new(path))
                .unwrap_or_else(|| panic!("{path} missing from the plan"))
        };

        assert!(find("src/routes.rs").contents.contains(MARK_ROUTES));
        assert!(find("src/models/mod.rs").contents.contains(MARK_MODULES));
        assert!(find("src/entities/mod.rs").contents.contains(MARK_MODULES));
        assert!(
            find("src/controllers/mod.rs")
                .contents
                .contains(MARK_MODULES)
        );
        assert!(find("src/factories/mod.rs").contents.contains(MARK_MODULES));
        assert!(find("src/seeders/mod.rs").contents.contains(MARK_MODULES));
        assert!(
            find("migration/src/lib.rs")
                .contents
                .contains(MARK_MIGRATIONS)
        );
        assert!(
            find("migration/src/lib.rs")
                .contents
                .contains(MARK_MIGRATION_MODULES)
        );
    }

    #[test]
    fn a_hyphenated_app_name_becomes_a_valid_crate_name() {
        let plan = new_app("my-app", &Dependency::Version("0.1".into()));
        let manifest = &plan.files[0].contents;

        assert!(manifest.contains(r#"name = "my_app""#), "{manifest}");
    }

    #[test]
    fn a_bare_model_generates_only_the_model_and_entity() {
        let plan = make_model(
            &Names::new("User"),
            ModelFlags::default(),
            "20260101_000001",
        );

        let paths: Vec<_> = plan
            .files
            .iter()
            .map(|file| file.path.display().to_string())
            .collect();

        assert_eq!(paths, vec!["src/models/user.rs", "src/entities/users.rs"]);
    }

    #[test]
    fn the_all_flag_generates_every_artefact() {
        let plan = make_model(&Names::new("User"), ModelFlags::all(), "20260101_000001");

        let paths: Vec<_> = plan
            .files
            .iter()
            .map(|file| file.path.display().to_string())
            .collect();

        for expected in [
            "src/models/user.rs",
            "src/entities/users.rs",
            "migration/src/m20260101_000001_create_users.rs",
            "src/validators/user.rs",
            "src/policies/user_policy.rs",
            "src/factories/user_factory.rs",
            "src/seeders/user_seeder.rs",
            "src/controllers/users_controller.rs",
        ] {
            assert!(
                paths.contains(&expected.to_owned()),
                "{expected} missing from {paths:?}"
            );
        }
    }

    #[test]
    fn a_controller_registers_one_resource_line() {
        let plan = make_model(
            &Names::new("User"),
            ModelFlags {
                controller: true,
                ..ModelFlags::default()
            },
            "20260101_000001",
        );

        let routes = plan
            .insertions
            .iter()
            .find(|insertion| insertion.marker == MARK_ROUTES)
            .expect("routes registered");

        // One line, not five: `resource` registers only the actions the
        // controller actually defines.
        assert_eq!(routes.line.lines().count(), 1);
        assert!(
            routes.line.starts_with(r#"r.resource("/users", "#),
            "{}",
            routes.line
        );
        assert!(routes.line.contains("UsersController"));
    }

    #[test]
    fn migrations_register_both_the_module_and_the_boxed_value() {
        let plan = make_model(
            &Names::new("User"),
            ModelFlags {
                migration: true,
                ..ModelFlags::default()
            },
            "20260101_000001",
        );

        let markers: Vec<_> = plan
            .insertions
            .iter()
            .map(|insertion| insertion.marker)
            .collect();

        assert!(markers.contains(&MARK_MIGRATION_MODULES));
        assert!(markers.contains(&MARK_MIGRATIONS));
    }
}

pub const MARK_FIELDS_OPEN: &str = "// <luxid:fields>";
pub const MARK_FIELDS_CLOSE: &str = "// </luxid:fields>";

/// Replace the body between the field markers, preserving everything else.
///
/// Only the region between the markers is touched, so hand-written rules and
/// overrides outside it survive a resync. This is the piece Laravel's stub
/// generators never had.
pub fn refresh_fields(source: &str, body: &str) -> Option<String> {
    let open = source.find(MARK_FIELDS_OPEN)?;
    let close = source[open..].find(MARK_FIELDS_CLOSE)? + open;

    let line_start = source[..open]
        .rfind('\n')
        .map(|index| index + 1)
        .unwrap_or(0);
    let indent = &source[line_start..open];

    let open_line_end = source[open..].find('\n')? + open + 1;

    let rendered: String = body
        .lines()
        .map(|line| format!("{indent}{line}\n"))
        .collect::<Vec<_>>()
        .join("");

    let mut out = String::with_capacity(source.len() + rendered.len());
    out.push_str(&source[..open_line_end]);
    out.push_str(&rendered);
    out.push_str(&source[line_start..]);

    // `source[line_start..]` re-emits the indent before the closing marker.
    let tail_start = close - indent.len();
    out.truncate(open_line_end + rendered.len());
    out.push_str(&source[tail_start..]);

    Some(out)
}

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

    const SOURCE: &str = "fn definition() {\n    ActiveModel {\n        // <luxid:fields>\n        old: Set(1),\n        // </luxid:fields>\n        ..Default::default()\n    }\n}\n";

    #[test]
    fn replaces_only_the_marked_region() {
        let updated = refresh_fields(SOURCE, "name: Set(\"name\".to_owned()),").expect("markers");

        assert!(updated.contains("name: Set(\"name\".to_owned()),"));
        assert!(
            !updated.contains("old: Set(1)"),
            "the old body was replaced"
        );

        // Everything outside the markers survives.
        assert!(updated.contains("..Default::default()"));
        assert!(updated.starts_with("fn definition() {"));
        assert!(updated.trim_end().ends_with('}'));
    }

    #[test]
    fn is_idempotent() {
        let once = refresh_fields(SOURCE, "a: Set(1),").expect("markers");
        let twice = refresh_fields(&once, "a: Set(1),").expect("markers");

        assert_eq!(once, twice, "resyncing an unchanged schema changes nothing");
    }

    #[test]
    fn keeps_the_marker_indentation() {
        let updated = refresh_fields(SOURCE, "a: Set(1),").expect("markers");
        assert!(updated.contains("\n        a: Set(1),\n"), "{updated}");
    }

    #[test]
    fn an_empty_body_clears_the_region() {
        let updated = refresh_fields(SOURCE, "").expect("markers");
        assert!(!updated.contains("old: Set(1)"));
    }

    #[test]
    fn a_file_without_markers_is_left_alone() {
        assert!(refresh_fields("fn nothing() {}", "a: Set(1),").is_none());
    }
}