prax-cli 0.3.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
//! `prax format` command - Format Prax schema file.

use crate::cli::FormatArgs;
use crate::config::SCHEMA_FILE_NAME;
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_NAME));

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

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

    // Read schema
    output::step(1, 3, "Reading schema...");
    let schema_content = std::fs::read_to_string(&schema_path)?;

    // Parse schema to validate it first
    let schema = parse_schema(&schema_content)?;

    // Format schema
    output::step(2, 3, "Formatting...");
    let formatted = format_schema(&schema);

    // Check if formatting changed anything
    let changed = formatted != schema_content;

    if args.check {
        // Check mode - just report if formatting is needed
        if changed {
            output::newline();
            output::error("Schema is not formatted correctly!");
            output::info("Run `prax format` to fix formatting.");
            return Err(CliError::Format("Schema needs formatting".to_string()).into());
        } else {
            output::newline();
            success("Schema is already formatted!");
            return Ok(());
        }
    }

    // Write formatted schema
    output::step(3, 3, "Writing formatted schema...");

    if changed {
        std::fs::write(&schema_path, &formatted)?;
        output::newline();
        success("Schema formatted successfully!");
    } else {
        output::newline();
        success("Schema is already formatted!");
    }

    Ok(())
}

fn parse_schema(content: &str) -> CliResult<prax_schema::Schema> {
    prax_schema::parse_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();

    // Format datasource (if present in schema)
    // For now, just add a standard datasource section
    output.push_str("datasource db {\n");
    output.push_str("    provider = \"postgresql\"\n");
    output.push_str("    url      = env(\"DATABASE_URL\")\n");
    output.push_str("}\n");
    let mut first_section = false;

    // Format generator
    if !first_section {
        output.push('\n');
    }
    output.push_str("generator client {\n");
    output.push_str("    provider = \"prax-client-rust\"\n");
    output.push_str("    output   = \"./src/generated\"\n");
    output.push_str("}\n");
    first_section = false;

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

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

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

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

    output
}

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",
        }
        .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(", ")
            )
        }
    }
}