prax-orm-cli 0.12.2

CLI tool for the Prax ORM
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
//! `prax format` command - Format Prax schema file(s).
//!
//! Bypasses `prax_schema::load` deliberately: formatting is per-file and
//! syntactic, so cross-file merge/validation would only get in the way.
//!
//! Note: plain `//` line comments are not preserved — the schema parser
//! discards them as trivia, so only `///` documentation comments survive
//! a round-trip through the formatter.

use std::path::Path;

use crate::cli::FormatArgs;
use crate::config::SCHEMA_FILE_PATH;
use crate::error::{CliError, CliResult};
use crate::output::{self, success};

/// Run the format command
pub async fn run(args: FormatArgs) -> CliResult<()> {
    output::header("Format Schema");

    let cwd = std::env::current_dir()?;
    let schema_path = args.schema.unwrap_or_else(|| cwd.join(SCHEMA_FILE_PATH));

    if !schema_path.exists() {
        return Err(CliError::Config(format!(
            "Schema path not found: {}",
            schema_path.display()
        )));
    }

    output::kv("Schema", &schema_path.display().to_string());
    output::newline();

    let files: Vec<std::path::PathBuf> = if schema_path.is_dir() {
        let discovered = prax_schema::loader::discover(&schema_path).map_err(CliError::from)?;
        if discovered.is_empty() {
            return Err(CliError::Config(format!(
                "No .prax files found under {}",
                schema_path.display()
            )));
        }
        discovered.into_iter().map(|d| d.absolute).collect()
    } else {
        vec![schema_path.clone()]
    };

    let mut any_changed = false;
    let mut any_needs_format = false;
    for file in &files {
        match format_one(file, args.check)? {
            FormatOutcome::Unchanged => {}
            FormatOutcome::Reformatted => any_changed = true,
            FormatOutcome::NeedsFormatting => any_needs_format = true,
        }
    }

    output::newline();
    if args.check {
        if any_needs_format {
            output::error("Some schema files are not formatted correctly.");
            output::info("Run `prax format` to fix formatting.");
            return Err(CliError::Format(
                "One or more schema files need formatting".to_string(),
            ));
        }
        success(&format!(
            "All {} schema file(s) are formatted!",
            files.len()
        ));
    } else if any_changed {
        success(&format!("Formatted {} schema file(s).", files.len()));
    } else {
        success(&format!(
            "All {} schema file(s) are already formatted!",
            files.len()
        ));
    }

    Ok(())
}

enum FormatOutcome {
    Unchanged,
    Reformatted,
    NeedsFormatting,
}

fn format_one(path: &Path, check: bool) -> CliResult<FormatOutcome> {
    let content = std::fs::read_to_string(path)?;
    let schema = parse_schema(&content)?;
    let formatted = format_schema(&schema);
    let changed = formatted != content;

    if check {
        return Ok(if changed {
            output::error(&format!("Needs formatting: {}", path.display()));
            FormatOutcome::NeedsFormatting
        } else {
            FormatOutcome::Unchanged
        });
    }

    if changed {
        std::fs::write(path, &formatted)?;
        output::list_item(&format!("Formatted {}", path.display()));
        Ok(FormatOutcome::Reformatted)
    } else {
        Ok(FormatOutcome::Unchanged)
    }
}

fn parse_schema(content: &str) -> CliResult<prax_schema::Schema> {
    // Use validate_schema to ensure field types are properly resolved
    // (e.g., FieldType::Model -> FieldType::Enum for enum references)
    prax_schema::validate_schema(content)
        .map_err(|e| CliError::Schema(format!("Syntax error: {}", e)))
}

/// Format a schema AST into a formatted string
fn format_schema(schema: &prax_schema::ast::Schema) -> String {
    let mut output = String::new();
    let mut wrote_section = false;

    // Serialize the datasource declared in the schema, preserving the
    // actual provider/url instead of injecting a hardcoded default.
    if let Some(datasource) = &schema.datasource {
        format_datasource(&mut output, datasource);
        wrote_section = true;
    }

    // Serialize the generator blocks declared in the schema.
    for generator in schema.generators.values() {
        if wrote_section {
            output.push('\n');
        }
        format_generator(&mut output, generator);
        wrote_section = true;
    }

    // Format enums first (since they're used by models)
    for enum_def in schema.enums.values() {
        if wrote_section {
            output.push('\n');
        }
        format_enum(&mut output, enum_def);
        wrote_section = true;
    }

    // Format models
    for model in schema.models.values() {
        if wrote_section {
            output.push('\n');
        }
        format_model(&mut output, model);
        wrote_section = true;
    }

    // Format views
    for view in schema.views.values() {
        if wrote_section {
            output.push('\n');
        }
        format_view(&mut output, view);
        wrote_section = true;
    }

    // Format composite types
    for composite in schema.types.values() {
        if wrote_section {
            output.push('\n');
        }
        format_composite(&mut output, composite);
        wrote_section = true;
    }

    output
}

fn format_datasource(output: &mut String, datasource: &prax_schema::ast::Datasource) {
    output.push_str(&format!("datasource {} {{\n", datasource.name));
    output.push_str(&format!(
        "    provider = \"{}\"\n",
        datasource.provider.as_str()
    ));

    if let Some(url_env) = &datasource.url_env {
        output.push_str(&format!("    url      = env(\"{}\")\n", url_env));
    } else if let Some(url) = &datasource.url {
        output.push_str(&format!("    url      = \"{}\"\n", url));
    }

    if !datasource.extensions.is_empty() {
        let extensions: Vec<String> = datasource
            .extensions
            .iter()
            .map(|ext| {
                let mut args = Vec::new();
                if let Some(schema) = &ext.schema {
                    args.push(format!("schema: \"{}\"", schema));
                }
                if let Some(version) = &ext.version {
                    args.push(format!("version: \"{}\"", version));
                }
                if args.is_empty() {
                    ext.name().to_string()
                } else {
                    format!("{}({})", ext.name(), args.join(", "))
                }
            })
            .collect();
        output.push_str(&format!("    extensions = [{}]\n", extensions.join(", ")));
    }

    for (key, value) in &datasource.properties {
        // env("VAR") values are stored verbatim; re-emit them unquoted
        // so the formatted output stays parseable.
        if value.starts_with("env(") {
            output.push_str(&format!("    {} = {}\n", key, value));
        } else {
            output.push_str(&format!("    {} = \"{}\"\n", key, value));
        }
    }

    output.push_str("}\n");
}

fn format_generator(output: &mut String, generator: &prax_schema::ast::Generator) {
    use prax_schema::ast::{GeneratorToggle, GeneratorValue};

    output.push_str(&format!("generator {} {{\n", generator.name()));

    if let Some(provider) = &generator.provider {
        output.push_str(&format!("    provider = \"{}\"\n", provider));
    }

    if let Some(out) = &generator.output {
        output.push_str(&format!("    output   = \"{}\"\n", out));
    }

    match &generator.generate {
        GeneratorToggle::Always => {}
        GeneratorToggle::Never | GeneratorToggle::Literal(false) => {
            output.push_str("    generate = false\n");
        }
        GeneratorToggle::Literal(true) => {
            output.push_str("    generate = true\n");
        }
        GeneratorToggle::Env(var) => {
            output.push_str(&format!("    generate = env(\"{}\")\n", var));
        }
    }

    for (key, value) in &generator.properties {
        let formatted = match value {
            GeneratorValue::String(s) => format!("\"{}\"", s),
            GeneratorValue::Bool(b) => b.to_string(),
            GeneratorValue::Env(var) => format!("env(\"{}\")", var),
            GeneratorValue::Ident(s) => s.to_string(),
        };
        output.push_str(&format!("    {} = {}\n", key, formatted));
    }

    output.push_str("}\n");
}

fn format_enum(output: &mut String, enum_def: &prax_schema::ast::Enum) {
    // Documentation
    if let Some(doc) = &enum_def.documentation {
        for line in doc.text.lines() {
            output.push_str(&format!("/// {}\n", line));
        }
    }

    output.push_str(&format!("enum {} {{\n", enum_def.name()));

    for variant in &enum_def.variants {
        // Documentation
        if let Some(doc) = &variant.documentation {
            for line in doc.text.lines() {
                output.push_str(&format!("    /// {}\n", line));
            }
        }

        output.push_str(&format!("    {}", variant.name()));

        // Format attributes
        for attr in &variant.attributes {
            output.push_str(&format!(" {}", format_attribute(attr)));
        }

        output.push('\n');
    }

    // Enum-level attributes
    for attr in &enum_def.attributes {
        output.push_str(&format!("\n    {}", format_attribute(attr)));
    }

    output.push_str("}\n");
}

fn format_model(output: &mut String, model: &prax_schema::ast::Model) {
    // Documentation
    if let Some(doc) = &model.documentation {
        for line in doc.text.lines() {
            output.push_str(&format!("/// {}\n", line));
        }
    }

    output.push_str(&format!("model {} {{\n", model.name()));

    // Calculate alignment for fields
    let max_name_len = model
        .fields
        .values()
        .map(|f| f.name().len())
        .max()
        .unwrap_or(0);

    let max_type_len = model
        .fields
        .values()
        .map(|f| format_field_type(&f.field_type, f.modifier).len())
        .max()
        .unwrap_or(0);

    for field in model.fields.values() {
        // Documentation
        if let Some(doc) = &field.documentation {
            for line in doc.text.lines() {
                output.push_str(&format!("    /// {}\n", line));
            }
        }

        let type_str = format_field_type(&field.field_type, field.modifier);

        // Pad name and type for alignment
        let padded_name = format!("{:width$}", field.name(), width = max_name_len);
        let padded_type = format!("{:width$}", type_str, width = max_type_len);

        output.push_str(&format!("    {} {}", padded_name, padded_type));

        // Format attributes
        for attr in &field.attributes {
            output.push_str(&format!(" {}", format_attribute(attr)));
        }

        output.push('\n');
    }

    // Model-level attributes
    let model_attrs: Vec<_> = model.attributes.iter().collect();
    if !model_attrs.is_empty() {
        output.push('\n');
        for attr in model_attrs {
            output.push_str(&format!("    {}\n", format_attribute(attr)));
        }
    }

    output.push_str("}\n");
}

fn format_view(output: &mut String, view: &prax_schema::ast::View) {
    // Documentation
    if let Some(doc) = &view.documentation {
        for line in doc.text.lines() {
            output.push_str(&format!("/// {}\n", line));
        }
    }

    output.push_str(&format!("view {} {{\n", view.name()));

    // Calculate alignment for fields
    let max_name_len = view
        .fields
        .values()
        .map(|f| f.name().len())
        .max()
        .unwrap_or(0);

    let max_type_len = view
        .fields
        .values()
        .map(|f| format_field_type(&f.field_type, f.modifier).len())
        .max()
        .unwrap_or(0);

    for field in view.fields.values() {
        let type_str = format_field_type(&field.field_type, field.modifier);
        let padded_name = format!("{:width$}", field.name(), width = max_name_len);
        let padded_type = format!("{:width$}", type_str, width = max_type_len);

        output.push_str(&format!("    {} {}", padded_name, padded_type));

        for attr in &field.attributes {
            output.push_str(&format!(" {}", format_attribute(attr)));
        }

        output.push('\n');
    }

    // View-level attributes
    let view_attrs: Vec<_> = view.attributes.iter().collect();
    if !view_attrs.is_empty() {
        output.push('\n');
        for attr in view_attrs {
            output.push_str(&format!("    {}\n", format_attribute(attr)));
        }
    }

    output.push_str("}\n");
}

fn format_composite(output: &mut String, composite: &prax_schema::ast::CompositeType) {
    // Documentation
    if let Some(doc) = &composite.documentation {
        for line in doc.text.lines() {
            output.push_str(&format!("/// {}\n", line));
        }
    }

    output.push_str(&format!("type {} {{\n", composite.name()));

    // Calculate alignment for fields
    let max_name_len = composite
        .fields
        .values()
        .map(|f| f.name().len())
        .max()
        .unwrap_or(0);

    let max_type_len = composite
        .fields
        .values()
        .map(|f| format_field_type(&f.field_type, f.modifier).len())
        .max()
        .unwrap_or(0);

    for field in composite.fields.values() {
        let type_str = format_field_type(&field.field_type, field.modifier);
        let padded_name = format!("{:width$}", field.name(), width = max_name_len);
        let padded_type = format!("{:width$}", type_str, width = max_type_len);

        output.push_str(&format!("    {} {}", padded_name, padded_type));

        for attr in &field.attributes {
            output.push_str(&format!(" {}", format_attribute(attr)));
        }

        output.push('\n');
    }

    output.push_str("}\n");
}

fn format_field_type(
    field_type: &prax_schema::ast::FieldType,
    modifier: prax_schema::ast::TypeModifier,
) -> String {
    use prax_schema::ast::{FieldType, ScalarType, TypeModifier};

    let base = match field_type {
        FieldType::Scalar(scalar) => match scalar {
            ScalarType::Int => "Int",
            ScalarType::BigInt => "BigInt",
            ScalarType::Float => "Float",
            ScalarType::String => "String",
            ScalarType::Boolean => "Boolean",
            ScalarType::DateTime => "DateTime",
            ScalarType::Date => "Date",
            ScalarType::Time => "Time",
            ScalarType::Json => "Json",
            ScalarType::Bytes => "Bytes",
            ScalarType::Decimal => "Decimal",
            ScalarType::Uuid => "Uuid",
            ScalarType::Cuid => "Cuid",
            ScalarType::Cuid2 => "Cuid2",
            ScalarType::NanoId => "NanoId",
            ScalarType::Ulid => "Ulid",
            ScalarType::Vector(_) => "Vector",
            ScalarType::HalfVector(_) => "HalfVector",
            ScalarType::SparseVector(_) => "SparseVector",
            ScalarType::Bit(_) => "Bit",
        }
        .to_string(),
        FieldType::Model(name) => name.to_string(),
        FieldType::Enum(name) => name.to_string(),
        FieldType::Composite(name) => name.to_string(),
        FieldType::Unsupported(name) => format!("Unsupported(\"{}\")", name),
    };

    match modifier {
        TypeModifier::Optional => format!("{}?", base),
        TypeModifier::List => format!("{}[]", base),
        TypeModifier::OptionalList => format!("{}[]?", base),
        TypeModifier::Required => base,
    }
}

fn format_attribute(attr: &prax_schema::ast::Attribute) -> String {
    // For model-level attributes we check if it's a known model attribute
    let prefix = if attr.is_model_attribute() { "@@" } else { "@" };

    if attr.args.is_empty() {
        format!("{}{}", prefix, attr.name())
    } else {
        let args: Vec<String> = attr
            .args
            .iter()
            .map(|arg| {
                if let Some(name) = &arg.name {
                    format!("{}: {}", name.as_str(), format_attribute_value(&arg.value))
                } else {
                    format_attribute_value(&arg.value)
                }
            })
            .collect();

        format!("{}{}({})", prefix, attr.name(), args.join(", "))
    }
}

fn format_attribute_value(value: &prax_schema::ast::AttributeValue) -> String {
    use prax_schema::ast::AttributeValue;

    match value {
        AttributeValue::String(s) => format!("\"{}\"", s),
        AttributeValue::Int(i) => i.to_string(),
        AttributeValue::Float(f) => f.to_string(),
        AttributeValue::Boolean(b) => b.to_string(),
        AttributeValue::Ident(id) => id.to_string(),
        AttributeValue::Function(name, args) => {
            if args.is_empty() {
                format!("{}()", name)
            } else {
                let arg_strs: Vec<String> = args.iter().map(format_attribute_value).collect();
                format!("{}({})", name, arg_strs.join(", "))
            }
        }
        AttributeValue::Array(items) => {
            let item_strs: Vec<String> = items.iter().map(format_attribute_value).collect();
            format!("[{}]", item_strs.join(", "))
        }
        AttributeValue::FieldRef(field) => field.to_string(),
        AttributeValue::FieldRefList(fields) => {
            format!(
                "[{}]",
                fields
                    .iter()
                    .map(|f| f.to_string())
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        }
    }
}