polyxml-cli 0.23.2

Command-line interface and polyglot XSD-to-code generator for PolyXML
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
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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
mod completions;
pub mod config;
mod options;
use options::target_options;

use std::fs;
use std::path::{Path, PathBuf};
use std::process::{self, Command};

use clap::{Args, CommandFactory, Parser, Subcommand};
use config::WorkspaceManifest;
use heck::AsPascalCase;
use polyxml::codegen::cpp::{CppBackend, CppCodegen, CppMode, CppOptions};
use polyxml::codegen::csharp::{CSharpCodegen, CSharpOptions, CSharpRecordKind};
use polyxml::codegen::go::{GoBackend, GoCodegen, GoOptions};
use polyxml::codegen::java::{JavaBackend, JavaCodegen, JavaOptions};
use polyxml::codegen::python::{PythonBackend, PythonCodegen, PythonOptions};
use polyxml::codegen::rust::{RustCodegen, RustOptions};
use polyxml::codegen::typescript::{TypeScriptBackend, TypeScriptCodegen, TypeScriptOptions};
use polyxml::ir::{SchemaIR, TypeDef};
use polyxml::schema_parser::XsdParser;

#[derive(Debug, Parser)]
#[command(
    name = "polyxml",
    about = "Modern XSD-to-code generator and streaming XML data-binding toolchain",
    version,
    propagate_version = true
)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Debug, Subcommand)]
pub enum Commands {
    /// Generate type-safe data models and codecs from XSD schema(s)
    Generate(GenerateArgs),

    /// Orchestrate multi-language project generation from polyxml.toml
    Build(BuildArgs),

    /// Validate XML schema syntax and structural invariants without generating code
    Validate(ValidateArgs),

    /// Convert complete XML or JSON documents; stdin/stdout pipes are supported
    Transcode(TranscodeArgs),

    /// Print a shell completion script with target-aware option suggestions
    Completions {
        #[arg(value_enum)]
        shell: completions::Shell,
    },

    #[command(name = "__complete", hide = true)]
    Complete {
        kind: String,
        #[arg(last = true)]
        words: Vec<String>,
    },
}

#[derive(Debug, Args)]
pub struct GenerateArgs {
    /// Path(s) to XSD schema files or glob patterns
    #[arg(value_name = "SCHEMA")]
    pub schemas: Vec<PathBuf>,

    /// Target language(s) to emit (python, rust, cpp, java, ts, go, csharp)
    #[arg(short = 'l', long = "lang", value_name = "LANG")]
    pub lang: Vec<String>,

    /// Target language backend (e.g. 'dataclass' or 'pydantic' for python)
    #[arg(short = 'b', long = "backend", value_name = "BACKEND")]
    pub backend: Option<String>,

    /// Opt-in target enhancement (repeatable; see compiler guide for supported values)
    #[arg(long = "feature", value_name = "NAME", value_delimiter = ',')]
    pub features: Vec<String>,

    /// Package or namespace for generated code (e.g. 'com.example.models' for Java, 'polyxml::models' for C++)
    #[arg(short = 'p', long = "package", alias = "namespace", value_name = "PKG")]
    pub package: Option<String>,

    /// Compilation or packaging mode (e.g. 'header' or 'modules' for C++)
    #[arg(short = 'm', long = "mode", value_name = "MODE")]
    pub mode: Option<String>,

    /// Zero-copy mode for Rust models (borrow Cow<'a, str>); pass 'false' for owned String fields (default: true)
    #[arg(long = "zero-copy", default_missing_value = "true", num_args = 0..=1)]
    pub zero_copy: Option<bool>,

    /// Emit streaming serialization and deserialization codecs (default: true)
    #[arg(long = "codecs", default_missing_value = "true", num_args = 0..=1)]
    pub codecs: Option<bool>,

    /// Target model style (e.g. record, pojo, class, record-class, record-struct)
    #[arg(long, value_name = "STYLE")]
    pub style: Option<String>,

    /// Output directory for generated source files
    #[arg(short = 'o', long = "out", alias = "out-dir", value_name = "DIR")]
    pub out: Option<PathBuf>,

    /// Path to workspace manifest (defaults to ./polyxml.toml if present)
    #[arg(short = 'c', long = "config", value_name = "FILE")]
    pub config: Option<PathBuf>,

    /// Enforce restriction facet validators in generated code
    #[arg(long = "strict-facets")]
    pub strict_facets: bool,

    /// Parse and validate schema without writing output files
    #[arg(long = "dry-run")]
    pub dry_run: bool,

    /// Automatically run language-specific code formatters after generation
    #[arg(long = "format")]
    pub format: bool,

    /// Custom header text to prepend to generated files
    #[arg(long = "custom-header", value_name = "TEXT")]
    pub custom_header: Option<String>,
}

#[derive(Debug, Args)]
pub struct BuildArgs {
    /// Path to workspace manifest
    #[arg(
        short = 'c',
        long = "config",
        value_name = "FILE",
        default_value = "polyxml.toml"
    )]
    pub config: PathBuf,

    /// Parse and validate without writing files
    #[arg(long = "dry-run")]
    pub dry_run: bool,

    /// Automatically run language-specific formatters
    #[arg(long = "format")]
    pub format: bool,
}

#[derive(Debug, Args)]
pub struct ValidateArgs {
    /// Path(s) to XSD schema files to validate
    #[arg(required = true, value_name = "SCHEMA")]
    pub schemas: Vec<PathBuf>,
}

#[derive(Debug, Args)]
pub struct TranscodeArgs {
    /// Input file path (or '-' / omitted for stdin)
    #[arg(value_name = "INPUT")]
    pub input: Option<PathBuf>,

    /// Output file path (or '-' / omitted for stdout)
    #[arg(short = 'o', long = "out", value_name = "OUTPUT")]
    pub output: Option<PathBuf>,

    /// Input format ('xml' or 'json', auto-detected if omitted)
    #[arg(long = "from", value_name = "FORMAT")]
    pub from: Option<String>,

    /// Output format ('xml' or 'json', auto-detected if omitted)
    #[arg(long = "to", value_name = "FORMAT")]
    pub to: Option<String>,

    /// Optional XSD schema file for typed schema-directed transcoding
    #[arg(short = 's', long = "schema", value_name = "SCHEMA")]
    pub schema: Option<PathBuf>,

    /// Root element name (used when transcoding JSON to XML)
    #[arg(short = 'r', long = "root", value_name = "ROOT")]
    pub root: Option<String>,

    /// Format output with indentation and newlines
    #[arg(long = "pretty")]
    pub pretty: bool,
}

fn main() {
    let cli = Cli::parse();

    let result = match cli.command {
        Commands::Generate(args) => run_generate(args),
        Commands::Build(args) => run_build(args),
        Commands::Validate(args) => run_validate(args),
        Commands::Transcode(args) => run_transcode(args),
        Commands::Completions { shell } => {
            completions::print_script(shell);
            Ok(())
        }
        Commands::Complete { kind, words } => {
            for value in completions::candidates(&kind, &words) {
                println!("{value}");
            }
            Ok(())
        }
    };

    if let Err(err) = result {
        if err
            .downcast_ref::<std::io::Error>()
            .is_some_and(|error| error.kind() == std::io::ErrorKind::InvalidInput)
        {
            Cli::command()
                .error(clap::error::ErrorKind::InvalidValue, err.to_string())
                .exit();
        }
        eprintln!("Error: {}", err);
        process::exit(1);
    }
}

fn run_generate(args: GenerateArgs) -> Result<(), Box<dyn std::error::Error>> {
    // If no schemas are passed directly, check for polyxml.toml config
    if args.schemas.is_empty() {
        if !args.lang.is_empty()
            || args.backend.is_some()
            || args.style.is_some()
            || !args.features.is_empty()
            || args.package.is_some()
            || args.mode.is_some()
            || args.zero_copy.is_some()
            || args.codecs.is_some()
            || args.out.is_some()
            || args.custom_header.is_some()
            || args.strict_facets
        {
            return Err("Generation options require explicit schema paths. For manifest builds, set target options in polyxml.toml.".into());
        }
        let config_path = args.config.unwrap_or_else(|| PathBuf::from("polyxml.toml"));
        if config_path.exists() {
            return run_build(BuildArgs {
                config: config_path,
                dry_run: args.dry_run,
                format: args.format,
            });
        } else {
            eprintln!("No schema files specified and polyxml.toml not found.");
            eprintln!("Run 'polyxml generate --help' for usage.");
            process::exit(1);
        }
    }

    let languages = if args.lang.is_empty() {
        vec!["python".to_string()]
    } else {
        args.lang.clone()
    };

    let emit_opts = TargetEmitOptions {
        backend: args.backend.as_deref(),
        features: &args.features,
        slots: None,
        kw_only: None,
        package: args.package.as_deref(),
        mode: args.mode.as_deref(),
        zero_copy: args.zero_copy,
        codecs: args.codecs,
        style: args.style.as_deref(),
        builder: None,
        codec: None,
        rkyv: None,
        phf: None,
        custom_header: args.custom_header.as_deref(),
    };
    let resolved_options = languages
        .iter()
        .map(|lang| emit_opts.resolve(lang))
        .collect::<std::io::Result<Vec<_>>>()?;

    let mut parser = XsdParser::new();
    let mut compiled_schemas = Vec::new();

    for schema_path in &args.schemas {
        if !schema_path.exists() {
            return Err(format!("Schema file not found: {}", schema_path.display()).into());
        }

        println!("Parsing schema: {}", schema_path.display());
        let ir = parser.parse_file(schema_path)?;
        report_schema_ir(&ir);
        compiled_schemas.push((schema_path.clone(), ir));
    }

    if args.dry_run {
        println!("\nDry run completed successfully. No files written.");
        return Ok(());
    }

    let base_out = args.out.unwrap_or_else(|| PathBuf::from("generated"));

    for (lang, emit_opts) in languages.iter().zip(resolved_options) {
        let lang_out = if languages.len() > 1 {
            base_out.join(lang)
        } else {
            base_out.clone()
        };

        fs::create_dir_all(&lang_out)?;

        for (schema_path, ir) in &compiled_schemas {
            emit_target_code(lang, emit_opts, &lang_out, schema_path, ir)?;
        }

        if args.format {
            run_language_formatter(lang, &lang_out);
        }
    }

    println!("Code generation complete.");
    Ok(())
}

fn run_build(args: BuildArgs) -> Result<(), Box<dyn std::error::Error>> {
    if !args.config.exists() {
        return Err(format!("Manifest not found: {}", args.config.display()).into());
    }

    println!("Loading manifest: {}", args.config.display());
    let manifest = WorkspaceManifest::from_file(&args.config)?;
    let base_dir = args.config.parent().unwrap_or_else(|| Path::new("."));

    let targets = manifest.resolved_targets();
    for target in &targets {
        target_options(target).resolve(&target.target)?;
    }

    let schema_files = manifest.expand_schemas(base_dir)?;
    if schema_files.is_empty() {
        println!("No schema files matched workspace schema patterns.");
        return Ok(());
    }

    let mut parser = XsdParser::new();
    let mut compiled_schemas = Vec::new();

    for schema_path in &schema_files {
        println!("Compiling schema: {}", schema_path.display());
        let ir = parser.parse_file(schema_path)?;
        report_schema_ir(&ir);
        compiled_schemas.push((schema_path.clone(), ir));
    }

    if targets.is_empty() {
        println!("No generation targets configured in manifest.");
        return Ok(());
    }

    if args.dry_run {
        println!("\nDry run completed. Targets configured: {}", targets.len());
        for target in &targets {
            println!(" - Target: {} -> {}", target.target, target.output);
        }
        return Ok(());
    }

    let output_base = manifest
        .workspace
        .as_ref()
        .and_then(|w| w.output_base_dir.as_ref())
        .map(|dir| {
            let p = Path::new(dir);
            if p.is_absolute() {
                p.to_path_buf()
            } else {
                base_dir.join(p)
            }
        })
        .unwrap_or_else(|| base_dir.to_path_buf());

    for target in &targets {
        let target_dir = output_base.join(&target.output);
        fs::create_dir_all(&target_dir)?;
        println!(
            "Emitting target [{}] into {}",
            target.target,
            target_dir.display()
        );

        let emit_opts = target_options(target).resolve(&target.target)?;

        for (schema_path, ir) in &compiled_schemas {
            emit_target_code(&target.target, emit_opts, &target_dir, schema_path, ir)?;
        }

        if args.format {
            run_language_formatter(&target.target, &target_dir);
        }
    }

    println!("Build finished successfully.");
    Ok(())
}

fn run_validate(args: ValidateArgs) -> Result<(), Box<dyn std::error::Error>> {
    let mut parser = XsdParser::new();
    let mut total_types = 0;
    let mut total_elements = 0;

    for schema_path in &args.schemas {
        if !schema_path.exists() {
            return Err(format!("Schema file not found: {}", schema_path.display()).into());
        }

        let ir = parser.parse_file(schema_path)?;
        println!("✓ Valid schema: {}", schema_path.display());
        if let Some(ref ns) = ir.target_namespace {
            println!("  targetNamespace: {}", ns);
        }
        println!(
            "  Components: {} types, {} root elements",
            ir.types.len(),
            ir.elements.len()
        );

        total_types += ir.types.len();
        total_elements += ir.elements.len();
    }

    println!(
        "\nAll schemas valid (Total: {} types, {} elements).",
        total_types, total_elements
    );
    Ok(())
}

fn report_schema_ir(ir: &SchemaIR) {
    let mut structs = 0;
    let mut enums = 0;
    let mut unions = 0;
    let mut simples = 0;
    let mut cycle_cuts = 0;

    for type_def in ir.types.values() {
        match type_def {
            TypeDef::Struct(s) => {
                structs += 1;
                for f in &s.fields {
                    if f.is_cycle_cut {
                        cycle_cuts += 1;
                    }
                }
            }
            TypeDef::Enum(_) => enums += 1,
            TypeDef::Union(_) => unions += 1,
            TypeDef::Simple(_) => simples += 1,
        }
    }

    if let Some(ref ns) = ir.target_namespace {
        println!("  Namespace: {}", ns);
    }
    println!(
        "  Types: {} total ({} structs, {} enums, {} unions, {} simple restrictions)",
        ir.types.len(),
        structs,
        enums,
        unions,
        simples
    );
    println!("  Root elements: {}", ir.elements.len());
    if cycle_cuts > 0 {
        println!("  Tarjan SCC: Boxed {} recursive cut points", cycle_cuts);
    }
}

#[derive(Debug, Default, Clone, Copy)]
pub struct TargetEmitOptions<'a> {
    pub backend: Option<&'a str>,
    pub features: &'a [String],
    pub slots: Option<bool>,
    pub kw_only: Option<bool>,
    pub package: Option<&'a str>,
    pub mode: Option<&'a str>,
    pub zero_copy: Option<bool>,
    pub codecs: Option<bool>,
    pub style: Option<&'a str>,
    pub builder: Option<bool>,
    pub codec: Option<&'a str>,
    pub rkyv: Option<bool>,
    pub phf: Option<bool>,
    pub custom_header: Option<&'a str>,
}

fn emit_target_code(
    lang: &str,
    opts: TargetEmitOptions<'_>,
    out_dir: &Path,
    schema_path: &Path,
    ir: &SchemaIR,
) -> std::io::Result<()> {
    let use_records = !matches!(opts.style, Some("pojo" | "class" | "dataclass"));
    let direct_codec = opts.codec == Some("direct");
    let language = lang.to_lowercase();
    match language.as_str() {
        "python" | "py" => {
            let py_backend = opts
                .backend
                .and_then(PythonBackend::from_str_loose)
                .unwrap_or(PythonBackend::Dataclass);

            let options = PythonOptions {
                backend: py_backend,
                slots: opts.slots.unwrap_or(true),
                kw_only: opts.kw_only.unwrap_or(true),
                pep695_aliases: true,
                emit_meta: true,
                emit_root_aliases: true,
                emit_codecs: opts.codecs.unwrap_or(true),
                emit_json_metadata: true,
                custom_header: opts.custom_header.map(|s| s.to_string()),
            };

            let codegen = PythonCodegen::new(options);
            let code = codegen.generate_module(ir);

            let file_stem = schema_path
                .file_stem()
                .map(|s| s.to_string_lossy())
                .unwrap_or_else(|| "models".into());

            let file_path = out_dir.join(format!("{}.py", file_stem));
            fs::write(file_path, code)?;

            let init_path = out_dir.join("__init__.py");
            if !init_path.exists() {
                let _ = fs::write(&init_path, "# Package generated by PolyXML\n");
            }
            Ok(())
        }
        "rust" | "rs" => {
            let options = RustOptions {
                zero_copy: opts.zero_copy.unwrap_or(true),
                derive_serde: true,
                derive_default: true,
                emit_polyxml_attrs: false,
                emit_root_aliases: true,
                emit_codecs: opts.codecs.unwrap_or(true),
                emit_rkyv: opts.rkyv.unwrap_or(false),
                phf: opts.phf.unwrap_or(false),
                custom_header: opts.custom_header.map(|s| s.to_string()),
            };

            let codegen = RustCodegen::new(options);
            let code = codegen.generate_module(ir);

            let file_stem = schema_path
                .file_stem()
                .map(|s| s.to_string_lossy())
                .unwrap_or_else(|| "models".into());

            let file_path = out_dir.join(format!("{}.rs", file_stem));
            fs::write(file_path, code)?;

            let mod_path = out_dir.join("mod.rs");
            if !mod_path.exists() {
                let _ = fs::write(
                    &mod_path,
                    format!("pub mod {};\npub use {}::*;\n", file_stem, file_stem),
                );
            }
            Ok(())
        }
        "ts" | "typescript" => {
            let ts_backend = opts
                .backend
                .and_then(TypeScriptBackend::from_str_loose)
                .unwrap_or(TypeScriptBackend::None);

            let options = TypeScriptOptions {
                backend: ts_backend,
                // Backend selection controls Zod emission.
                emit_zod: false,
                use_interface: true,
                readonly_fields: false,
                emit_root_aliases: true,
                custom_header: opts.custom_header.map(|s| s.to_string()),
            };

            let codegen = TypeScriptCodegen::new(options);
            let code = codegen.generate_module(ir);

            let file_stem = schema_path
                .file_stem()
                .map(|s| s.to_string_lossy())
                .unwrap_or_else(|| "models".into());

            let file_path = out_dir.join(format!("{}.ts", file_stem));
            fs::write(file_path, code)?;

            let index_path = out_dir.join("index.ts");
            if !index_path.exists() {
                let _ = fs::write(&index_path, format!("export * from \"./{}\";\n", file_stem));
            }
            Ok(())
        }
        "java" => {
            let pkg = opts.package.unwrap_or("generated.models").to_string();
            let java_backend = opts
                .backend
                .and_then(JavaBackend::from_str_loose)
                .unwrap_or(JavaBackend::Standard);

            let options = JavaOptions {
                package_name: pkg,
                backend: java_backend,
                use_records,
                emit_builder: opts.builder.unwrap_or(false),
                emit_direct_codec: direct_codec,
                validate_facets: true,
                emit_root_aliases: true,
                custom_header: opts.custom_header.map(|s| s.to_string()),
            };

            let codegen = JavaCodegen::new(options);
            if direct_codec {
                codegen.validate_direct_codecs(ir).map_err(|message| {
                    std::io::Error::new(std::io::ErrorKind::InvalidInput, message)
                })?;
            }
            let files = codegen.generate_files(ir);

            for (filename, code) in files {
                let file_path = out_dir.join(filename);
                fs::write(file_path, code)?;
            }
            Ok(())
        }
        "cpp" | "c++" => {
            let ns = opts.package.unwrap_or("polyxml::generated");
            let file_stem = schema_path
                .file_stem()
                .map(|s| s.to_string_lossy())
                .unwrap_or_else(|| "models".into());

            let cpp_mode = opts
                .mode
                .and_then(CppMode::from_str_loose)
                .unwrap_or(CppMode::HeaderOnly);

            let cpp_backend = opts
                .backend
                .and_then(CppBackend::from_str_loose)
                .unwrap_or(CppBackend::Standard);

            let options = CppOptions {
                namespace: ns.to_string(),
                mode: cpp_mode,
                backend: cpp_backend,
                standard: "c++20".to_string(),
                emit_equality_operators: true,
                emit_enum_converters: true,
                validate_facets: true,
                emit_root_aliases: true,
                emit_cmake: false,
                emit_meson: false,
                custom_header: opts.custom_header.map(|s| s.to_string()),
            };

            let codegen = CppCodegen::new(options);
            let files = codegen.generate_files(ir, &file_stem);

            for (filename, code) in files {
                let file_path = out_dir.join(filename);
                fs::write(file_path, code)?;
            }
            Ok(())
        }
        "go" => {
            let pkg = opts.package.unwrap_or("models");
            let file_stem = schema_path
                .file_stem()
                .map(|s| s.to_string_lossy())
                .unwrap_or_else(|| "models".into());

            let go_backend = opts
                .backend
                .and_then(GoBackend::from_str_loose)
                .unwrap_or(GoBackend::Standard);

            let options = GoOptions {
                backend: go_backend,
                package_name: pkg.to_string(),
                emit_xml_tags: true,
                emit_json_tags: true,
                validate_choice_exclusivity: true,
                validate_facets: true,
                emit_root_aliases: true,
                custom_header: opts.custom_header.map(|s| s.to_string()),
            };

            let codegen = GoCodegen::new(options);
            let files = codegen.generate_files(ir, &file_stem);

            for (filename, code) in files {
                let file_path = out_dir.join(filename);
                fs::write(file_path, code)?;
            }
            Ok(())
        }
        "csharp" | "c#" | "cs" => {
            let ns = opts.package.unwrap_or("Generated");
            let file_stem = schema_path
                .file_stem()
                .map(|s| s.to_string_lossy())
                .unwrap_or_else(|| "Models".into());

            let record_kind = if matches!(opts.style, Some("record-struct")) {
                CSharpRecordKind::Struct
            } else {
                CSharpRecordKind::Class
            };

            let options = CSharpOptions {
                namespace: ns.to_string(),
                emit_xml_attributes: true,
                emit_json_attributes: true,
                emit_validation: true,
                record_kind,
                use_records,
                use_file_scoped_namespaces: true,
                emit_root_records: true,
                emit_source_gen: opts.backend == Some("source-gen"),
                source_gen_context_name: format!("{}JsonContext", AsPascalCase(&file_stem)),
                custom_header: opts.custom_header.map(|s| s.to_string()),
            };

            let codegen = CSharpCodegen::new(options);
            let files = codegen.generate_files(ir, &file_stem);

            for (filename, code) in files {
                let file_path = out_dir.join(filename);
                fs::write(file_path, code)?;
            }
            Ok(())
        }
        _ => Err(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            format!("Unknown target: {lang}"),
        )),
    }
}

fn run_language_formatter(lang: &str, dir: &Path) {
    let dir_str = match dir.to_str() {
        Some(s) => s,
        None => return,
    };

    match lang.to_lowercase().as_str() {
        "python" | "py" => {
            let _ = Command::new("ruff").args(["format", dir_str]).status();
            let _ = Command::new("ruff")
                .args(["check", "--fix", "--silent", dir_str])
                .status();
        }
        "rust" | "rs" => {
            let _ = Command::new("cargo").args(["fmt"]).status();
        }
        "go" => {
            let _ = Command::new("gofmt").args(["-w", dir_str]).status();
        }
        "cpp" | "c++" => {
            if let Ok(entries) = fs::read_dir(dir) {
                let cpp_files: Vec<_> = entries
                    .filter_map(|e| e.ok())
                    .map(|e| e.path())
                    .filter(|p| {
                        p.extension()
                            .map(|ext| ext == "hpp" || ext == "h" || ext == "cpp" || ext == "cppm")
                            .unwrap_or(false)
                    })
                    .collect();
                if !cpp_files.is_empty() {
                    let mut cmd = Command::new("clang-format");
                    cmd.arg("-i");
                    for f in cpp_files {
                        cmd.arg(f);
                    }
                    let _ = cmd.status();
                }
            }
        }
        "ts" | "typescript" => {
            let _ = Command::new("npx")
                .args(["prettier", "--write", dir_str])
                .status();
        }
        "java" => {
            let _ = Command::new("google-java-format")
                .args(["-i", dir_str])
                .status();
        }
        "csharp" | "c#" | "cs" => {
            let status = Command::new("csharpier").args([dir_str]).status();
            if status.is_err() || !status.as_ref().map(|s| s.success()).unwrap_or(false) {
                let _ = Command::new("dotnet")
                    .env("DOTNET_NOLOGO", "1")
                    .env("DOTNET_CLI_TELEMETRY_OPTOUT", "1")
                    .env("DOTNET_SKIP_FIRST_TIME_EXPERIENCE", "1")
                    .args(["format", "whitespace", dir_str])
                    .status();
            }
        }
        _ => {}
    }
}

fn run_transcode(args: TranscodeArgs) -> Result<(), Box<dyn std::error::Error>> {
    use std::io::{Read, Write};

    // 1. Read input bytes
    let input_bytes = if let Some(ref path) = args.input {
        if path.as_os_str() == "-" {
            let mut buf = Vec::new();
            std::io::stdin().read_to_end(&mut buf)?;
            buf
        } else {
            fs::read(path)?
        }
    } else {
        let mut buf = Vec::new();
        std::io::stdin().read_to_end(&mut buf)?;
        buf
    };

    // 2. Determine from and to formats
    let from_format = if let Some(ref f) = args.from {
        f.to_lowercase()
    } else if let Some(ref path) = args.input {
        match path.extension().and_then(|e| e.to_str()).unwrap_or("") {
            "xml" => "xml".to_string(),
            "json" => "json".to_string(),
            _ => {
                if input_bytes.iter().find(|&&b| !b.is_ascii_whitespace()) == Some(&b'<') {
                    "xml".to_string()
                } else {
                    "json".to_string()
                }
            }
        }
    } else if input_bytes.iter().find(|&&b| !b.is_ascii_whitespace()) == Some(&b'<') {
        "xml".to_string()
    } else {
        "json".to_string()
    };

    let to_format = if let Some(ref t) = args.to {
        t.to_lowercase()
    } else if let Some(ref path) = args.output {
        match path.extension().and_then(|e| e.to_str()).unwrap_or("") {
            "xml" => "xml".to_string(),
            "json" => "json".to_string(),
            _ => {
                if from_format == "xml" {
                    "json".to_string()
                } else {
                    "xml".to_string()
                }
            }
        }
    } else if from_format == "xml" {
        "json".to_string()
    } else {
        "xml".to_string()
    };

    // 3. Load optional ModelSchema
    let model_schema = if let Some(ref schema_path) = args.schema {
        let mut parser = XsdParser::new();
        let ir = parser.parse_file(schema_path)?;
        Some(polyxml::ModelSchema::from_ir(&ir, args.root.as_deref())?)
    } else {
        None
    };

    let indent = if args.pretty { Some(2) } else { None };

    // 4. Perform transcoding
    let output_bytes = match (from_format.as_str(), to_format.as_str()) {
        ("xml", "json") => polyxml::xml_to_json(&input_bytes, model_schema, indent, true)?,
        ("json", "xml") => polyxml::json_to_xml(
            &input_bytes,
            model_schema,
            args.root.as_deref(),
            indent,
            None,
            None,
        )?,
        _ => {
            return Err(format!(
                "Unsupported transcoding direction from '{}' to '{}'",
                from_format, to_format
            )
            .into())
        }
    };

    // 5. Write output bytes
    if let Some(ref path) = args.output {
        if path.as_os_str() == "-" {
            std::io::stdout().write_all(&output_bytes)?;
        } else {
            fs::write(path, output_bytes)?;
        }
    } else {
        std::io::stdout().write_all(&output_bytes)?;
    }

    Ok(())
}