babbel-cli 0.2.2

Universal CLI utility for Babbel polyglot serialization, querying, diffing, patching, and formatting
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
//! # Babbel CLI
//!
//! Universal command-line utility for multi-format serialization, document querying,
//! AST diffing, RFC 6902 patching, schema validation, and formatting.

use std::fs;
use std::io::{self, Read, Write};
use std::path::Path;
use std::process;

use clap::{Args, CommandFactory, Parser, Subcommand, ValueEnum};
use clap_complete::{Shell, generate};

use babbel::core::io::Buffer;
use babbel::core::{
    CompiledSchema, FormatOptions, Patch, Value, apply_merge_patch, diff, diff_merge_patch,
    jsonpath_query,
};
use babbel::{FormatEmitter, default_registry};

#[derive(Parser, Debug)]
#[command(
    name = "babbel",
    about = "Universal Polyglot Serialization & Document Toolkit",
    version,
    long_about = "Universal command-line utility for multi-format serialization, RFC 9535 JSONPath querying, AST diffing, RFC 6902/7396 patching, schema validation, and document formatting across 16 formats."
)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand, Debug)]
enum Commands {
    /// Convert documents between any supported formats
    Convert(ConvertArgs),

    /// Query documents using RFC 9535 JSONPath expressions
    Query(QueryArgs),

    /// Compute structural diff (RFC 6902 Patch or RFC 7396 Merge Patch)
    Diff(DiffArgs),

    /// Apply an RFC 6902 or RFC 7396 patch to a document
    Patch(PatchArgs),

    /// Validate a document against a JSON Schema (Draft 7 / 2020-12)
    Validate(ValidateArgs),

    /// Format and pretty-print a document
    Fmt(FmtArgs),

    /// Inspect format, structure, size, and depth of a document
    Inspect(InspectArgs),

    /// Generate shell autocompletions (bash, zsh, fish, powershell, elvish)
    Completions(CompletionsArgs),
}

#[derive(Args, Debug)]
struct ConvertArgs {
    /// Path to input file (use '-' for stdin)
    input: String,

    /// Source format (auto-detected from file extension if omitted)
    #[arg(short = 'f', long = "from")]
    from: Option<String>,

    /// Target format (e.g. json, yaml, toml, xml, cbor, msgpack, bson, ron, kdl, parquet, hcl, avro)
    #[arg(short = 't', long = "to")]
    to: String,

    /// Output file path (defaults to stdout)
    #[arg(short = 'o', long = "output")]
    output: Option<String>,

    /// Pretty-print formatted output (default: true for text formats)
    #[arg(long = "pretty", default_value_t = true, conflicts_with = "compact")]
    pretty: bool,

    /// Emit compact output without indentation
    #[arg(long = "compact")]
    compact: bool,

    /// Indentation spaces (default: 2)
    #[arg(long = "indent", default_value_t = 2)]
    indent: usize,
}

#[derive(Args, Debug)]
struct QueryArgs {
    /// Path to input file (use '-' for stdin)
    input: String,

    /// RFC 9535 JSONPath query expression (e.g. '$.store.book[*].title')
    #[arg(short = 'q', long = "query")]
    query: String,

    /// Input document format (inferred from extension if omitted)
    #[arg(short = 'f', long = "from")]
    from: Option<String>,

    /// Return only the first matching element instead of an array
    #[arg(long = "first")]
    first: bool,

    /// Output file path (defaults to stdout)
    #[arg(short = 'o', long = "output")]
    output: Option<String>,
}

#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
enum DiffFormat {
    Patch,
    Merge,
}

#[derive(Args, Debug)]
struct DiffArgs {
    /// Original document file path (use '-' for stdin)
    file1: String,

    /// Target document file path
    file2: String,

    /// Diff representation format: patch (RFC 6902) or merge (RFC 7396)
    #[arg(long = "format", value_enum, default_value_t = DiffFormat::Patch)]
    format: DiffFormat,

    /// Shortcut for '--format merge' (RFC 7396 JSON Merge Patch)
    #[arg(long = "merge")]
    merge: bool,

    /// Output file path (defaults to stdout)
    #[arg(short = 'o', long = "output")]
    output: Option<String>,
}

#[derive(Args, Debug)]
struct PatchArgs {
    /// Path to target document (use '-' for stdin)
    input: String,

    /// Path to RFC 6902 or RFC 7396 patch file
    #[arg(short = 'p', long = "patch")]
    patch: String,

    /// Force RFC 7396 Merge Patch semantics
    #[arg(long = "merge")]
    merge: bool,

    /// Output file path (defaults to stdout)
    #[arg(short = 'o', long = "output")]
    output: Option<String>,
}

#[derive(Args, Debug)]
struct ValidateArgs {
    /// Path to document to validate (use '-' for stdin)
    input: String,

    /// Path to JSON Schema file
    #[arg(short = 's', long = "schema")]
    schema: String,
}

#[derive(Args, Debug)]
struct FmtArgs {
    /// Path to input file (use '-' for stdin)
    input: String,

    /// Indentation spaces (default: 2)
    #[arg(long = "indent", default_value_t = 2)]
    indent: usize,

    /// Overwrite input file in-place
    #[arg(short = 'i', long = "in-place")]
    in_place: bool,

    /// Output file path (defaults to stdout)
    #[arg(short = 'o', long = "output")]
    output: Option<String>,
}

#[derive(Args, Debug)]
struct InspectArgs {
    /// Path to input file (use '-' for stdin)
    input: String,
}

#[derive(Args, Debug)]
struct CompletionsArgs {
    /// Target shell to generate completions for
    shell: Shell,
}

#[derive(Debug)]
enum CliError {
    Io(String),
    Format(String),
    Validation(String),
    Usage(String),
}

fn main() {
    let cli = Cli::parse();
    if let Err(err) = run(cli) {
        match err {
            CliError::Usage(msg) => {
                eprintln!("error: {}", msg);
                process::exit(1);
            }
            CliError::Io(msg) | CliError::Format(msg) => {
                eprintln!("error: {}", msg);
                process::exit(2);
            }
            CliError::Validation(msg) => {
                eprintln!("{}", msg);
                process::exit(3);
            }
        }
    }
}

fn run(cli: Cli) -> Result<(), CliError> {
    match cli.command {
        Commands::Convert(args) => cmd_convert(args),
        Commands::Query(args) => cmd_query(args),
        Commands::Diff(args) => cmd_diff(args),
        Commands::Patch(args) => cmd_patch(args),
        Commands::Validate(args) => cmd_validate(args),
        Commands::Fmt(args) => cmd_fmt(args),
        Commands::Inspect(args) => cmd_inspect(args),
        Commands::Completions(args) => cmd_completions(args),
    }
}

fn read_input(path: &str) -> Result<(Vec<u8>, Option<String>), CliError> {
    if path == "-" {
        let mut buffer = Vec::new();
        io::stdin()
            .read_to_end(&mut buffer)
            .map_err(|e| CliError::Io(format!("failed to read from stdin: {}", e)))?;
        Ok((buffer, None))
    } else {
        let bytes = fs::read(path)
            .map_err(|e| CliError::Io(format!("failed to read file '{}': {}", path, e)))?;
        let ext = Path::new(path)
            .extension()
            .and_then(|s| s.to_str())
            .map(|s| s.to_lowercase());
        Ok((bytes, ext))
    }
}

fn write_output(bytes: &[u8], path: Option<&str>, is_binary: bool) -> Result<(), CliError> {
    if let Some(out_p) = path {
        fs::write(out_p, bytes)
            .map_err(|e| CliError::Io(format!("failed to write to '{}': {}", out_p, e)))?;
    } else {
        io::stdout()
            .write_all(bytes)
            .map_err(|e| CliError::Io(format!("failed to write to stdout: {}", e)))?;
        if !bytes.ends_with(b"\n") && !is_binary {
            println!();
        }
    }
    Ok(())
}

fn detect_engine(
    format: Option<&str>,
    ext: Option<&str>,
) -> Result<std::sync::Arc<dyn babbel::FormatEngine>, CliError> {
    let registry = default_registry();
    if let Some(fmt) = format {
        if let Some(engine) = registry.get(fmt) {
            return Ok(engine);
        }
        return Err(CliError::Usage(format!(
            "unsupported format '{}'. Available formats: {:?}",
            fmt,
            registry.available_formats()
        )));
    }
    if let Some(e) = ext {
        if let Some(engine) = registry.get_by_extension(e) {
            return Ok(engine);
        }
    }
    // Fallback: try json
    registry
        .get("json")
        .ok_or_else(|| CliError::Format("json format engine not found".to_string()))
}

fn cmd_convert(args: ConvertArgs) -> Result<(), CliError> {
    let (input_bytes, ext) = read_input(&args.input)?;
    let src_engine = detect_engine(args.from.as_deref(), ext.as_deref())?;
    let dst_engine = detect_engine(Some(&args.to), None)?;

    let val = src_engine
        .parse_bytes(&input_bytes)
        .map_err(|e| CliError::Format(format!("failed to parse input: {}", e)))?;

    let mut dest = Buffer::new();
    let pretty = if args.compact { false } else { args.pretty };
    let options = FormatOptions {
        pretty,
        indent: args.indent,
    };
    dst_engine
        .serialize(&val, &mut dest, &options)
        .map_err(|e| CliError::Format(format!("failed to serialize output: {}", e)))?;

    let out_bytes = dest.into_vec();
    write_output(&out_bytes, args.output.as_deref(), dst_engine.is_binary())
}

fn cmd_query(args: QueryArgs) -> Result<(), CliError> {
    let (input_bytes, ext) = read_input(&args.input)?;
    let engine = detect_engine(args.from.as_deref(), ext.as_deref())?;
    let val = engine
        .parse_bytes(&input_bytes)
        .map_err(|e| CliError::Format(format!("parse error: {}", e)))?;

    let results = jsonpath_query(&val, &args.query)
        .map_err(|e| CliError::Format(format!("query evaluation error: {}", e)))?;

    let output_val = if args.first {
        results.into_iter().next().cloned().unwrap_or(Value::Null)
    } else {
        Value::Array(results.into_iter().cloned().collect())
    };

    let mut dest = Buffer::new();
    let json_engine = default_registry()
        .get("json")
        .ok_or_else(|| CliError::Format("json engine missing".to_string()))?;
    json_engine
        .emit_pretty(&output_val, &mut dest, 2)
        .map_err(|e| CliError::Format(format!("failed to format query output: {}", e)))?;

    let out_bytes = dest.into_vec();
    write_output(&out_bytes, args.output.as_deref(), false)
}

fn cmd_diff(args: DiffArgs) -> Result<(), CliError> {
    let (bytes1, ext1) = read_input(&args.file1)?;
    let (bytes2, ext2) = read_input(&args.file2)?;

    let engine1 = detect_engine(None, ext1.as_deref())?;
    let engine2 = detect_engine(None, ext2.as_deref())?;

    let val1 = engine1
        .parse_bytes(&bytes1)
        .map_err(|e| CliError::Format(format!("failed to parse '{}': {}", args.file1, e)))?;
    let val2 = engine2
        .parse_bytes(&bytes2)
        .map_err(|e| CliError::Format(format!("failed to parse '{}': {}", args.file2, e)))?;

    let json_engine = default_registry()
        .get("json")
        .ok_or_else(|| CliError::Format("json engine missing".to_string()))?;
    let mut dest = Buffer::new();

    let is_merge = args.merge || args.format == DiffFormat::Merge;
    if is_merge {
        let merge_patch = diff_merge_patch(&val1, &val2);
        json_engine
            .emit_pretty(&merge_patch, &mut dest, 2)
            .map_err(|e| CliError::Format(format!("serialization error: {}", e)))?;
    } else {
        let patch = diff(&val1, &val2);
        let patch_val = patch.to_value();
        json_engine
            .emit_pretty(&patch_val, &mut dest, 2)
            .map_err(|e| CliError::Format(format!("serialization error: {}", e)))?;
    }

    let out_bytes = dest.into_vec();
    write_output(&out_bytes, args.output.as_deref(), false)
}

fn cmd_patch(args: PatchArgs) -> Result<(), CliError> {
    let (input_bytes, ext) = read_input(&args.input)?;
    let (patch_bytes, patch_ext) = read_input(&args.patch)?;

    let engine = detect_engine(None, ext.as_deref())?;
    let patch_engine = detect_engine(None, patch_ext.as_deref())?;

    let mut target_val = engine
        .parse_bytes(&input_bytes)
        .map_err(|e| CliError::Format(format!("failed to parse '{}': {}", args.input, e)))?;
    let patch_val = patch_engine
        .parse_bytes(&patch_bytes)
        .map_err(|e| CliError::Format(format!("failed to parse '{}': {}", args.patch, e)))?;

    if args.merge {
        apply_merge_patch(&mut target_val, &patch_val);
    } else if let Ok(patch) = Patch::from_value(&patch_val) {
        patch
            .apply_inplace(&mut target_val)
            .map_err(|e| CliError::Format(format!("failed to apply patch: {}", e)))?;
    } else {
        apply_merge_patch(&mut target_val, &patch_val);
    }

    let mut dest = Buffer::new();
    engine
        .emit_pretty(&target_val, &mut dest, 2)
        .map_err(|e| CliError::Format(format!("serialization error: {}", e)))?;

    let out_bytes = dest.into_vec();
    write_output(&out_bytes, args.output.as_deref(), engine.is_binary())
}

fn cmd_validate(args: ValidateArgs) -> Result<(), CliError> {
    let (input_bytes, ext) = read_input(&args.input)?;
    let (schema_bytes, s_ext) = read_input(&args.schema)?;

    let engine = detect_engine(None, ext.as_deref())?;
    let schema_engine = detect_engine(None, s_ext.as_deref())?;

    let val = engine
        .parse_bytes(&input_bytes)
        .map_err(|e| CliError::Format(format!("failed to parse '{}': {}", args.input, e)))?;
    let schema_val = schema_engine
        .parse_bytes(&schema_bytes)
        .map_err(|e| CliError::Format(format!("failed to parse '{}': {}", args.schema, e)))?;

    let compiled = CompiledSchema::compile(&schema_val)
        .map_err(|e| CliError::Format(format!("failed to compile schema: {}", e)))?;

    match compiled.validate(&val) {
        Ok(()) => {
            println!(
                "VALID: '{}' conforms to schema '{}'",
                args.input, args.schema
            );
            Ok(())
        }
        Err(errors) => {
            let mut msg = format!(
                "INVALID: '{}' violates schema ({} errors):",
                args.input,
                errors.len()
            );
            for err in errors {
                msg.push_str(&format!(
                    "\n  - [{}] at '{}': {}",
                    err.keyword, err.pointer, err.message
                ));
            }
            Err(CliError::Validation(msg))
        }
    }
}

fn cmd_fmt(args: FmtArgs) -> Result<(), CliError> {
    let (input_bytes, ext) = read_input(&args.input)?;
    let engine = detect_engine(None, ext.as_deref())?;
    let val = engine
        .parse_bytes(&input_bytes)
        .map_err(|e| CliError::Format(format!("failed to parse '{}': {}", args.input, e)))?;

    let mut dest = Buffer::new();
    engine
        .emit_pretty(&val, &mut dest, args.indent)
        .map_err(|e| CliError::Format(format!("failed to format output: {}", e)))?;
    let out_bytes = dest.into_vec();

    if args.in_place && args.input != "-" {
        fs::write(&args.input, &out_bytes).map_err(|e| {
            CliError::Io(format!(
                "failed to write in-place to '{}': {}",
                args.input, e
            ))
        })?;
        println!("Formatted '{}'", args.input);
        Ok(())
    } else {
        write_output(&out_bytes, args.output.as_deref(), engine.is_binary())
    }
}

fn cmd_inspect(args: InspectArgs) -> Result<(), CliError> {
    let (input_bytes, ext) = read_input(&args.input)?;
    let engine = detect_engine(None, ext.as_deref())?;
    let val = engine
        .parse_bytes(&input_bytes)
        .map_err(|e| CliError::Format(format!("failed to parse '{}': {}", args.input, e)))?;

    println!("File:             {}", args.input);
    println!(
        "Detected Format:  {} ({})",
        engine.format_id(),
        engine.mime_type()
    );
    println!("Byte Size:        {} bytes", input_bytes.len());
    println!(
        "Root Type:        {}",
        match &val {
            Value::Null => "null",
            Value::Bool(_) => "boolean",
            Value::Integer(_) => "integer",
            Value::Float(_) => "float",
            Value::String(_) => "string",
            Value::Bytes(_) => "bytes",
            Value::Array(_) => "array",
            Value::Object(_) => "object",
        }
    );

    if let Value::Object(entries) = &val {
        println!("Key Count:        {}", entries.len());
    } else if let Value::Array(items) = &val {
        println!("Element Count:    {}", items.len());
    }

    fn calculate_depth(v: &Value) -> usize {
        match v {
            Value::Array(items) => 1 + items.iter().map(calculate_depth).max().unwrap_or(0),
            Value::Object(entries) => {
                1 + entries
                    .iter()
                    .map(|(_, val)| calculate_depth(val))
                    .max()
                    .unwrap_or(0)
            }
            _ => 1,
        }
    }

    println!("Max AST Depth:    {}", calculate_depth(&val));
    Ok(())
}

fn cmd_completions(args: CompletionsArgs) -> Result<(), CliError> {
    let mut cmd = Cli::command();
    let bin_name = cmd.get_name().to_string();
    generate(args.shell, &mut cmd, bin_name, &mut io::stdout());
    Ok(())
}