puerto 0.22.0

Puerto — Rust full-stack DDD framework CLI. Scaffolds Clean Architecture projects with auto-wired DI, SQLx support, and zero manual wiring. Formerly puerto-framework.
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
use crate::generators::layout;
use std::path::Path;

use crate::generators::conflict::{OverwritePolicy, guard_entity_overwrite};
use crate::generators::{
    application::write_application_files,
    domain::write_domain_files,
    infrastructure::write_repository_files,
    migration::run_create_table_migration,
    naming::{crud_use_cases, pascal_to_snake, to_pascal_case},
    presentation::write_presentation_files,
    web::write_web_files,
};
use crate::puerto_toml::{Field, ValueObjectDefinition};

/// Low-level scaffold: the four crates of the entity's context (ADR-0023), its web slice, its
/// `[[entidad]]` block and the regenerated wiring. Always the five CRUD use cases.
pub fn run(
    name: &str,
    base: &Path,
    db: bool,
    fields: &[Field],
    shared_vos: &[ValueObjectDefinition],
) -> Result<(), Box<dyn std::error::Error>> {
    let pascal = to_pascal_case(name);
    let snake = pascal_to_snake(&pascal);
    let web = crate::puerto_toml::read(base).is_ok_and(|c| c.project.web);

    write_domain_files(&pascal, &snake, base, fields, shared_vos)?;
    write_application_files(&pascal, &snake, base, fields, shared_vos)?;
    write_repository_files(&pascal, &snake, base, db, fields, shared_vos)?;
    write_presentation_files(&pascal, &snake, base, fields)?;
    if web {
        write_web_files(&pascal, base, fields)?;
    }

    crate::puerto_toml::add_entity(base, &pascal, crud_use_cases(&snake), db, fields.to_vec())?;
    crate::patchers::routes_rs::patch_routes_from_manifest(base, &pascal, &snake)?;
    crate::generators::bootstrap::regenerate_bootstrap(base)?;

    let repo_name = if db {
        format!("Repositorio{pascal}Postgres")
    } else {
        format!("Repositorio{pascal}EnMemoria")
    };
    let dir = layout::context_dir(&snake);
    println!("✓ {dir}/dominio/         — model, errors, repository port, Madre{pascal}");
    println!("✓ {dir}/aplicacion/      — create, get, list, update, delete");
    println!("✓ {dir}/infraestructura/ — {repo_name}");
    println!("✓ {dir}/rest/            — routes, dto, responses, errors");
    if web {
        println!("✓ web/ — list, detail and form screens, HTTP repository");
    }
    println!("✓ Done. Zero manual wiring.");
    if web {
        println!(
            "\nThe API changed. Next: make web/sync   (exports the spec, regenerates the web types)"
        );
    }

    Ok(())
}

/// `--publico` and `--rol` of `puerto generar scaffold` in an auth project (ADR-0021 § 4): the
/// CRUD use cases whose routes take no `BearerAuth`, and the roles a route requires.
#[derive(Debug, Default, Clone)]
pub struct ScaffoldAccess {
    pub public: Vec<String>,
    pub roles: std::collections::BTreeMap<String, Vec<String>>,
}

impl ScaffoldAccess {
    /// `--publico list_products,get_product` and `--rol create_product=admin,…` as the CLI got them.
    pub fn from_args(public: &[String], roles: &[String]) -> Result<Self, String> {
        let mut access = Self {
            public: public.to_vec(),
            ..Default::default()
        };
        for arg in roles {
            let (use_case, role) = arg.split_once('=').ok_or_else(|| {
                format!(
                    "invalid --rol '{arg}'. Expected <use_case>=<role>, e.g. borrar_product=admin"
                )
            })?;
            let roles = access.roles.entry(use_case.to_string()).or_default();
            if !roles.iter().any(|r| r == role) {
                roles.push(role.to_string());
            }
        }
        Ok(access)
    }

    fn is_empty(&self) -> bool {
        self.public.is_empty() && self.roles.is_empty()
    }
}

/// What a scaffold says about the entity beyond its field list: its uniques over several fields
/// (`--unico a,b`, repeatable), the order of its list (`--orden=-a,b`) and its private fields
/// (`--privado a,b`, auth projects). A re-scaffold without one keeps what the block says.
#[derive(Default)]
pub struct EntityShape {
    pub uniques: Vec<Vec<String>>,
    pub order: Vec<String>,
    pub private: Vec<String>,
}

impl EntityShape {
    /// `--unico jugador_id,partido_id` (one group each), `--orden` and `--privado` as the CLI got
    /// them.
    pub fn from_args(uniques: &[String], order: &[String], private: &[String]) -> Self {
        let trimmed = |args: &[String]| args.iter().map(|a| a.trim().to_string()).collect();
        Self {
            uniques: uniques
                .iter()
                .map(|group| group.split(',').map(|f| f.trim().to_string()).collect())
                .collect(),
            order: trimmed(order),
            private: trimmed(private),
        }
    }

    fn is_empty(&self) -> bool {
        self.uniques.is_empty() && self.order.is_empty()
    }

    /// `fields` with `--privado` applied: the named ones private. Without `--privado`, a field keeps
    /// what the block (`declared`) said of it — a re-scaffold that passes its fields again. A name
    /// that is no field is refused, before anything is written.
    pub(crate) fn mark_private(
        &self,
        pascal: &str,
        fields: Vec<Field>,
        declared: &[Field],
    ) -> Result<Vec<Field>, String> {
        if let Some(unknown) = self
            .private
            .iter()
            .find(|name| !fields.iter().any(|f| &&f.name == name))
        {
            return Err(format!(
                "{pascal}: privado: '{unknown}' is not a field of {pascal}"
            ));
        }
        Ok(fields
            .into_iter()
            .map(|f| Field {
                private: if self.private.is_empty() {
                    f.private || declared.iter().any(|d| d.name == f.name && d.private)
                } else {
                    self.private.contains(&f.name)
                },
                ..f
            })
            .collect())
    }
}

/// `shape` checked against the entity's fields, before anything is written.
pub(crate) fn check_shape(
    pascal: &str,
    fields: &[Field],
    shape: &EntityShape,
) -> Result<(), Box<dyn std::error::Error>> {
    let errors = crate::validation::validate_entity_shape(&crate::puerto_toml::Entity {
        name: pascal.to_string(),
        fields: fields.to_vec(),
        uniques: shape.uniques.clone(),
        order: shape.order.clone(),
        ..Default::default()
    });
    if errors.is_empty() {
        Ok(())
    } else {
        Err(format!("{pascal}: {}", errors.join("\n  ")).into())
    }
}

/// Writes a checked `shape` to the entity's `[[entidad]]` block (creating the block) before any
/// file is rendered: the generators read it back from there.
pub(crate) fn record_shape(
    base: &Path,
    pascal: &str,
    db: bool,
    fields: &[Field],
    shape: &EntityShape,
) -> Result<(), Box<dyn std::error::Error>> {
    if shape.is_empty() {
        return Ok(());
    }
    let mut config = crate::puerto_toml::read(base)?;
    match config.entity.iter_mut().find(|e| e.name == pascal) {
        // What is not given stays as the block says.
        Some(entity) => {
            if !shape.uniques.is_empty() {
                entity.uniques = shape.uniques.clone();
            }
            if !shape.order.is_empty() {
                entity.order = shape.order.clone();
            }
        }
        None => config.entity.push(crate::puerto_toml::Entity {
            name: pascal.to_string(),
            use_cases: crud_use_cases(&pascal_to_snake(pascal)),
            db,
            fields: fields.to_vec(),
            uniques: shape.uniques.clone(),
            order: shape.order.clone(),
            ..Default::default()
        }),
    }
    crate::puerto_toml::write(base, &config)
}

/// Checks `access` against the entity's CRUD use cases and writes it to its `[[entidad]]` block,
/// before any file is rendered: the generators read it back from there. A re-scaffold without
/// flags keeps what the block says.
fn record_access(
    base: &Path,
    pascal: &str,
    db: bool,
    fields: &[Field],
    access: &ScaffoldAccess,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut config = crate::puerto_toml::read(base)?;
    if !config.project.has_auth() {
        if !access.is_empty() {
            return Err(
                "--publico and --rol need an auth project (`puerto nuevo --auth oidc`)".into(),
            );
        }
        return Ok(());
    }
    let snake = pascal_to_snake(pascal);
    let checked = crate::puerto_toml::Entity {
        name: pascal.to_string(),
        use_cases: crud_use_cases(&snake),
        public: access.public.clone(),
        roles: access.roles.clone(),
        ..Default::default()
    };
    let errors = crate::validation::validate_entity_access(&checked);
    if !errors.is_empty() {
        return Err(format!("{pascal}: {}", errors.join("\n  ")).into());
    }
    match config.entity.iter_mut().find(|e| e.name == pascal) {
        // A re-scaffold: the routes are rewritten as the block already says.
        Some(_) if access.is_empty() => return Ok(()),
        Some(entity) => {
            entity.public = checked.public;
            entity.roles = checked.roles;
        }
        None => config.entity.push(crate::puerto_toml::Entity {
            db,
            fields: fields.to_vec(),
            ..checked
        }),
    }
    crate::puerto_toml::write(base, &config)
}

/// `puerto generar scaffold <Name>` without `--publico` / `--rol`.
/// Reads `project.db` from `puerto.toml`, always generates full CRUD.
/// `cli_fields` are fields passed from the CLI (e.g. `title:String price:i64`).
///
/// Refuses to overwrite an already-generated entity unless `--forzar`; see `conflict.rs`.
#[cfg(test)]
pub fn run_scaffold(
    name: &str,
    base: &Path,
    cli_fields: &[Field],
    overwrite: OverwritePolicy,
) -> Result<(), Box<dyn std::error::Error>> {
    run_scaffold_with_access(
        name,
        base,
        cli_fields,
        overwrite,
        &ScaffoldAccess::default(),
    )
}

/// [`run_scaffold`] with `--publico` / `--rol`. In an auth project every CRUD route takes
/// `BearerAuth` except the `public` ones; outside one, `access` must be empty.
#[cfg(test)]
pub fn run_scaffold_with_access(
    name: &str,
    base: &Path,
    cli_fields: &[Field],
    overwrite: OverwritePolicy,
    access: &ScaffoldAccess,
) -> Result<(), Box<dyn std::error::Error>> {
    run_scaffold_with_options(
        name,
        base,
        cli_fields,
        overwrite,
        access,
        &EntityShape::default(),
    )
}

/// [`run_scaffold_with_access`] with `--unico`.
pub fn run_scaffold_with_options(
    name: &str,
    base: &Path,
    cli_fields: &[Field],
    overwrite: OverwritePolicy,
    access: &ScaffoldAccess,
    shape: &EntityShape,
) -> Result<(), Box<dyn std::error::Error>> {
    let config = crate::puerto_toml::read(base)?;
    let db = config.project.db;
    let shared_vos = config.value_object.clone();
    let pascal = to_pascal_case(name);
    let declared = config
        .entity
        .iter()
        .find(|e| e.name == pascal)
        .map(|e| e.fields.clone())
        .unwrap_or_default();
    let fields = if !cli_fields.is_empty() {
        cli_fields.to_vec()
    } else {
        declared.clone()
    };
    let fields = shape.mark_private(&pascal, fields, &declared)?;
    if !config.project.has_auth() && fields.iter().any(|f| f.private) {
        return Err(
            "--privado needs an auth project (`puerto nuevo --auth oidc`): with no session, \
             nobody would ever see the field"
                .into(),
        );
    }

    // Checked before the first byte is written — a half-overwritten project is worse than a
    // refused command.
    crate::validation::validate_entity_name(&pascal)?;
    if config.project.has_auth() {
        crate::validation::validate_auth_entity_name(&pascal)?;
    }
    crate::validation::validate_unique_field_names(&fields)?;
    if db {
        for f in &fields {
            crate::validation::validate_sql_field_name(&f.name)?;
        }
    }
    let vo_conflicts = crate::validation::validate_vo_coherence(&fields, &shared_vos);
    if !vo_conflicts.is_empty() {
        return Err(vo_conflicts.join("\n  ").into());
    }
    crate::validation::validate_references_before_writing(&pascal, &fields, &config, base)?;
    guard_entity_overwrite(
        base,
        &pascal_to_snake(&pascal),
        db,
        config.project.web,
        overwrite,
    )?;
    // Checked before the access is written: a refused shape leaves nothing behind.
    check_shape(&pascal, &fields, shape)?;
    record_access(base, &pascal, db, &fields, access)?;
    record_shape(base, &pascal, db, &fields, shape)?;

    run(name, base, db, &fields, &shared_vos)?;
    if db {
        let snake = pascal_to_snake(&pascal);
        let groups = crate::puerto_toml::read(base)?
            .entity
            .into_iter()
            .find(|e| e.name == pascal)
            .map(|e| e.uniques)
            .unwrap_or_default();
        let migration_sql =
            crate::generators::infrastructure::create_table_sql_with(&snake, &fields, &groups);
        run_create_table_migration(&snake, base, &migration_sql)?;
    }
    Ok(())
}

/// CLI entry point for `puerto generar objeto-valor <Name> <type>`.
///
/// Validated here, at the command that writes it: an invalid declaration used to land in
/// puerto.toml and only surface later, in `puerto validar` or as uncompilable generated code.
pub fn run_value_object(
    name: &str,
    inner_type: &str,
    base: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    let inner_type = &crate::puerto_toml::expand_type(inner_type);
    crate::validation::validate_vo_name(name)?;
    crate::validation::validate_shared_vo_inner_type(inner_type)?;

    let config = crate::puerto_toml::read(base)?;
    if config.value_object.iter().any(|vo| vo.name == name) {
        return Err(
            format!("shared value object '{name}' is already declared in puerto.toml").into(),
        );
    }
    crate::puerto_toml::add_value_object(base, name, inner_type)?;
    println!("✓ Shared value object '{name}' added to puerto.toml");
    Ok(())
}