babbel-cli 0.3.0

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
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
//! # 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,
    infer_schema, jsonpath_query, transform,
};
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),

    /// Set key or path values in documents (e.g. 'server.port=8080')
    Set(SetArgs),

    /// Sequentially merge multiple documents using RFC 7396 Merge Patch
    Merge(MergeArgs),

    /// Generate JSON Schema Draft 2020-12 from document structure
    Schema(SchemaArgs),

    /// 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", default_value = "$")]
    query: String,

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

    /// Transform pipeline expression (e.g. 'map(.name) | sort | unique')
    #[arg(long = "transform")]
    transform: Option<String>,

    /// Target output format (default: json)
    #[arg(short = 't', long = "to", default_value = "json")]
    to: 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>,

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

#[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(Args, Debug)]
struct SetArgs {
    /// Path to input file (use '-' for stdin)
    input: String,

    /// Key-value assignment pairs (e.g. 'foo.bar=123', 'active=true')
    #[arg(required = true, num_args = 1..)]
    assignments: Vec<String>,

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

    /// Target format (auto-detected from input format if omitted)
    #[arg(short = 't', long = "to")]
    to: Option<String>,

    /// 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>,

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

#[derive(Args, Debug)]
struct MergeArgs {
    /// Paths to input files to merge sequentially (left-to-right)
    #[arg(required = true, num_args = 1..)]
    inputs: Vec<String>,

    /// Output target format (defaults to format of first input file)
    #[arg(short = 't', long = "to")]
    to: Option<String>,

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

    /// 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 SchemaArgs {
    /// Path to input file (use '-' for stdin)
    input: String,

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

    /// Target schema output format (default: json)
    #[arg(short = 't', long = "to", default_value = "json")]
    to: String,

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

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

#[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),
        Commands::Set(args) => cmd_set(args),
        Commands::Merge(args) => cmd_merge(args),
        Commands::Schema(args) => cmd_schema(args),
    }
}

fn read_input(path: &str) -> Result<(Vec<u8>, Option<String>), CliError> {
    let mut bytes = 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)))?;
        buffer
    } else {
        fs::read(path)
            .map_err(|e| CliError::Io(format!("failed to read file '{}': {}", path, e)))?
    };

    if bytes.starts_with(&[0xef, 0xbb, 0xbf]) {
        bytes.drain(0..3);
    }

    let ext = if path == "-" {
        None
    } else {
        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 = if pretty {
        FormatOptions::pretty().with_indent(args.indent)
    } else {
        FormatOptions::compact()
    };
    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 mut output_val = if args.query.trim() == "$" || args.query.trim().is_empty() {
        val
    } else {
        let results = jsonpath_query(&val, &args.query)
            .map_err(|e| CliError::Format(format!("query evaluation error: {}", e)))?;
        if args.first {
            results.into_iter().next().cloned().unwrap_or(Value::Null)
        } else {
            Value::Array(results.into_iter().cloned().collect())
        }
    };

    if let Some(ref expr) = args.transform {
        output_val = transform(&output_val, expr)
            .map_err(|e| CliError::Format(format!("transform error: {}", e)))?;
    }

    let dst_engine = detect_engine(Some(&args.to), None)?;
    let mut dest = Buffer::new();
    let options = FormatOptions::pretty().with_indent(args.indent);
    dst_engine
        .serialize(&output_val, &mut dest, &options)
        .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(), dst_engine.is_binary())
}

fn parse_literal_value(s: &str) -> Value {
    let trimmed = s.trim();
    if trimmed == "null" {
        Value::Null
    } else if trimmed == "true" {
        Value::Bool(true)
    } else if trimmed == "false" {
        Value::Bool(false)
    } else if let Ok(i) = trimmed.parse::<i128>() {
        Value::Integer(i)
    } else if let Ok(f) = trimmed.parse::<f64>() {
        Value::Float(f)
    } else if (trimmed.starts_with('{') && trimmed.ends_with('}'))
        || (trimmed.starts_with('[') && trimmed.ends_with(']'))
        || (trimmed.starts_with('"') && trimmed.ends_with('"'))
    {
        if let Some(json_engine) = default_registry().get("json") {
            if let Ok(v) = json_engine.parse_str(trimmed) {
                return v;
            }
        }
        Value::String(trimmed.to_string())
    } else {
        Value::String(trimmed.to_string())
    }
}

fn set_value_at_path(val: &mut Value, path: &str, new_val: Value) -> Result<(), CliError> {
    let clean = path.trim_start_matches('$').trim_start_matches('.');
    let parts: Vec<&str> = clean.split('.').filter(|s| !s.is_empty()).collect();
    if parts.is_empty() {
        *val = new_val;
        return Ok(());
    }

    let mut current = val;
    for (i, part) in parts.iter().enumerate() {
        let is_last = i == parts.len() - 1;
        if let Some(bracket_idx) = part.find('[') {
            if !part.ends_with(']') {
                return Err(CliError::Usage(format!("invalid path segment '{}'", part)));
            }
            let field_name = &part[..bracket_idx];
            let idx_str = &part[bracket_idx + 1..part.len() - 1];
            let idx: usize = idx_str.parse().map_err(|_| {
                CliError::Usage(format!(
                    "invalid array index '{}' in path '{}'",
                    idx_str, part
                ))
            })?;

            if !field_name.is_empty() {
                if !matches!(current, Value::Object(_)) {
                    *current = Value::Object(Vec::new());
                }
                if let Value::Object(entries) = current {
                    let pos = entries.iter().position(|(k, _)| k == field_name);
                    let idx_entry = match pos {
                        Some(p) => p,
                        None => {
                            entries.push((field_name.to_string(), Value::Array(Vec::new())));
                            entries.len() - 1
                        }
                    };
                    current = &mut entries[idx_entry].1;
                }
            }

            if !matches!(current, Value::Array(_)) {
                *current = Value::Array(Vec::new());
            }

            if let Value::Array(items) = current {
                while items.len() <= idx {
                    items.push(Value::Null);
                }
                if is_last {
                    items[idx] = new_val;
                    return Ok(());
                } else {
                    current = &mut items[idx];
                }
            }
        } else if is_last {
            if !matches!(current, Value::Object(_)) {
                *current = Value::Object(Vec::new());
            }
            if let Value::Object(entries) = current {
                if let Some((_, v)) = entries.iter_mut().find(|(k, _)| k == *part) {
                    *v = new_val;
                } else {
                    entries.push((part.to_string(), new_val));
                }
            }
            return Ok(());
        } else {
            if !matches!(current, Value::Object(_)) {
                *current = Value::Object(Vec::new());
            }
            if let Value::Object(entries) = current {
                let pos = entries.iter().position(|(k, _)| k == *part);
                let entry_idx = match pos {
                    Some(p) => p,
                    None => {
                        entries.push((part.to_string(), Value::Object(Vec::new())));
                        entries.len() - 1
                    }
                };
                current = &mut entries[entry_idx].1;
            }
        }
    }
    Ok(())
}

fn cmd_set(args: SetArgs) -> Result<(), CliError> {
    let (input_bytes, ext) = read_input(&args.input)?;
    let src_engine = detect_engine(args.from.as_deref(), ext.as_deref())?;
    let mut val = src_engine
        .parse_bytes(&input_bytes)
        .map_err(|e| CliError::Format(format!("failed to parse input: {}", e)))?;

    for assignment in &args.assignments {
        let (path, value_str) = assignment.split_once('=').ok_or_else(|| {
            CliError::Usage(format!(
                "invalid assignment '{}': expected format <path>=<value>",
                assignment
            ))
        })?;
        let parsed_val = parse_literal_value(value_str);
        set_value_at_path(&mut val, path.trim(), parsed_val)?;
    }

    let dst_engine = if let Some(target_fmt) = &args.to {
        detect_engine(Some(target_fmt), None)?
    } else {
        src_engine
    };

    let mut dest = Buffer::new();
    let options = FormatOptions::pretty().with_indent(args.indent);
    dst_engine
        .serialize(&val, &mut dest, &options)
        .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!("Updated '{}'", args.input);
        Ok(())
    } else {
        write_output(&out_bytes, args.output.as_deref(), dst_engine.is_binary())
    }
}

fn cmd_merge(args: MergeArgs) -> Result<(), CliError> {
    if args.inputs.is_empty() {
        return Err(CliError::Usage(
            "at least one input file required for merge".into(),
        ));
    }

    let (first_bytes, first_ext) = read_input(&args.inputs[0])?;
    let first_engine = detect_engine(None, first_ext.as_deref())?;
    let mut base_val = first_engine
        .parse_bytes(&first_bytes)
        .map_err(|e| CliError::Format(format!("failed to parse '{}': {}", args.inputs[0], e)))?;

    for input_path in &args.inputs[1..] {
        let (bytes, ext) = read_input(input_path)?;
        let engine = detect_engine(None, ext.as_deref())?;
        let next_val = engine
            .parse_bytes(&bytes)
            .map_err(|e| CliError::Format(format!("failed to parse '{}': {}", input_path, e)))?;
        apply_merge_patch(&mut base_val, &next_val);
    }

    let dst_engine = if let Some(target_fmt) = &args.to {
        detect_engine(Some(target_fmt), None)?
    } else {
        first_engine
    };

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

    write_output(&out_bytes, args.output.as_deref(), dst_engine.is_binary())
}

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

    let schema_val = infer_schema(&val);
    let dst_engine = detect_engine(Some(&args.to), None)?;

    let mut dest = Buffer::new();
    let options = FormatOptions::pretty().with_indent(args.indent);
    dst_engine
        .serialize(&schema_val, &mut dest, &options)
        .map_err(|e| CliError::Format(format!("failed to format schema: {}", e)))?;
    let out_bytes = dest.into_vec();

    write_output(&out_bytes, args.output.as_deref(), dst_engine.is_binary())
}

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(())
}