polyxml 0.35.6__tar.gz → 0.37.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. {polyxml-0.35.6 → polyxml-0.37.0}/Cargo.lock +6 -6
  2. {polyxml-0.35.6 → polyxml-0.37.0}/Cargo.toml +1 -1
  3. {polyxml-0.35.6 → polyxml-0.37.0}/PKG-INFO +1 -1
  4. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/codegen/cpp/mod.rs +373 -66
  5. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/codegen/go/mod.rs +76 -16
  6. {polyxml-0.35.6 → polyxml-0.37.0}/pyproject.toml +1 -1
  7. {polyxml-0.35.6 → polyxml-0.37.0}/README.md +0 -0
  8. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/Cargo.toml +0 -0
  9. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/README.md +0 -0
  10. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/benches/core_benchmarks.rs +0 -0
  11. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/benches/support/tag_dispatch_fixtures.rs +0 -0
  12. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/benches/tag_dispatch.rs +0 -0
  13. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/codegen/csharp/mod.rs +0 -0
  14. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/codegen/java/codec.rs +0 -0
  15. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/codegen/java/mod.rs +0 -0
  16. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/codegen/java/models.rs +0 -0
  17. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/codegen/mod.rs +0 -0
  18. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/codegen/python/aot.rs +0 -0
  19. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/codegen/python/mod.rs +0 -0
  20. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/codegen/rust/mod.rs +0 -0
  21. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/codegen/typescript/mod.rs +0 -0
  22. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/converters.rs +0 -0
  23. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/error.rs +0 -0
  24. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/integer.rs +0 -0
  25. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/ir/chunker.rs +0 -0
  26. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/ir/mod.rs +0 -0
  27. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/ir/particle.rs +0 -0
  28. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/ir/tarjan.rs +0 -0
  29. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/json.rs +0 -0
  30. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/lib.rs +0 -0
  31. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/parser.rs +0 -0
  32. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/pattern.rs +0 -0
  33. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/schema.rs +0 -0
  34. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/schema_parser/mod.rs +0 -0
  35. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/schema_parser/upa.rs +0 -0
  36. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/serializer.rs +0 -0
  37. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/transcoder.rs +0 -0
  38. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/src/value.rs +0 -0
  39. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/fixtures/lexical_union.xsd +0 -0
  40. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/fixtures/mixed_content.xsd +0 -0
  41. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/fixtures/ts_mixed_content_inheritance.xsd +0 -0
  42. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_adversarial_identifiers.rs +0 -0
  43. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_any_attribute.rs +0 -0
  44. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_any_element.rs +0 -0
  45. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_conformance.rs +0 -0
  46. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_core.rs +0 -0
  47. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_corpus_regressions.rs +0 -0
  48. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_cpp_codegen.rs +0 -0
  49. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_csharp_codegen.rs +0 -0
  50. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_default_literal_codegen.rs +0 -0
  51. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_enum_literal_codegen.rs +0 -0
  52. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_go_codegen.rs +0 -0
  53. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_inherited_simple_content.rs +0 -0
  54. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_inherited_text_codegen.rs +0 -0
  55. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_java_codegen.rs +0 -0
  56. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_json.rs +0 -0
  57. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_lexical_lists.rs +0 -0
  58. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_lexical_union_codegen.rs +0 -0
  59. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_mixed_branch_lookup.rs +0 -0
  60. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_mixed_content.rs +0 -0
  61. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_mixed_nil.rs +0 -0
  62. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_namespace_entities.rs +0 -0
  63. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_pattern_codegen.rs +0 -0
  64. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_python_codegen.rs +0 -0
  65. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_qualified_attributes.rs +0 -0
  66. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_runtime_fast_paths.rs +0 -0
  67. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_rust_codecs.rs +0 -0
  68. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_rust_codegen.rs +0 -0
  69. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_schema_attribute_entities.rs +0 -0
  70. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_schema_audit.rs +0 -0
  71. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_schema_ir.rs +0 -0
  72. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_security.rs +0 -0
  73. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_temporal_union.rs +0 -0
  74. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_transcoder.rs +0 -0
  75. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_ts_codegen.rs +0 -0
  76. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_ubl_csharp.rs +0 -0
  77. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_unbounded_choice.rs +0 -0
  78. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_upa.rs +0 -0
  79. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_value_constraints.rs +0 -0
  80. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_variant_registry.rs +0 -0
  81. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-core/tests/test_xsi_type.rs +0 -0
  82. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-python/Cargo.toml +0 -0
  83. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-python/README.md +0 -0
  84. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-python/benches/aot_vs_dataclass.py +0 -0
  85. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-python/src/lib.rs +0 -0
  86. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-python/tests/test_binary_fields.py +0 -0
  87. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-python/tests/test_conformance.py +0 -0
  88. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-python/tests/test_generated_models.py +0 -0
  89. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-python/tests/test_json.py +0 -0
  90. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-python/tests/test_mixed_content.py +0 -0
  91. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-python/tests/test_polyxml.py +0 -0
  92. {polyxml-0.35.6 → polyxml-0.37.0}/crates/polyxml-python/tests/test_xsi_type.py +0 -0
  93. {polyxml-0.35.6 → polyxml-0.37.0}/python/polyxml/AGENT_GUIDE.md +0 -0
  94. {polyxml-0.35.6 → polyxml-0.37.0}/python/polyxml/__init__.py +0 -0
  95. {polyxml-0.35.6 → polyxml-0.37.0}/python/polyxml/compat/__init__.py +0 -0
  96. {polyxml-0.35.6 → polyxml-0.37.0}/python/polyxml/compat/xsdata.py +0 -0
  97. {polyxml-0.35.6 → polyxml-0.37.0}/python/polyxml/py.typed +0 -0
@@ -610,7 +610,7 @@ dependencies = [
610
610
 
611
611
  [[package]]
612
612
  name = "polyxml"
613
- version = "0.35.6"
613
+ version = "0.37.0"
614
614
  dependencies = [
615
615
  "criterion",
616
616
  "heck",
@@ -631,14 +631,14 @@ dependencies = [
631
631
 
632
632
  [[package]]
633
633
  name = "polyxml-c"
634
- version = "0.35.6"
634
+ version = "0.37.0"
635
635
  dependencies = [
636
636
  "polyxml",
637
637
  ]
638
638
 
639
639
  [[package]]
640
640
  name = "polyxml-cli"
641
- version = "0.35.6"
641
+ version = "0.37.0"
642
642
  dependencies = [
643
643
  "clap",
644
644
  "glob",
@@ -652,7 +652,7 @@ dependencies = [
652
652
 
653
653
  [[package]]
654
654
  name = "polyxml-js"
655
- version = "0.35.6"
655
+ version = "0.37.0"
656
656
  dependencies = [
657
657
  "napi",
658
658
  "napi-build",
@@ -662,7 +662,7 @@ dependencies = [
662
662
 
663
663
  [[package]]
664
664
  name = "polyxml-python"
665
- version = "0.35.6"
665
+ version = "0.37.0"
666
666
  dependencies = [
667
667
  "base64",
668
668
  "hex",
@@ -674,7 +674,7 @@ dependencies = [
674
674
 
675
675
  [[package]]
676
676
  name = "polyxml-wasm"
677
- version = "0.35.6"
677
+ version = "0.37.0"
678
678
  dependencies = [
679
679
  "polyxml",
680
680
  "quick-xml",
@@ -3,7 +3,7 @@ resolver = "2"
3
3
  members = ["crates/polyxml-core", "crates/polyxml-python"]
4
4
 
5
5
  [workspace.package]
6
- version = "0.35.6"
6
+ version = "0.37.0"
7
7
  edition = "2021"
8
8
  rust-version = "1.80"
9
9
  license = "MIT"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: polyxml
3
- Version: 0.35.6
3
+ Version: 0.37.0
4
4
  Classifier: Development Status :: 4 - Beta
5
5
  Classifier: Intended Audience :: Developers
6
6
  Classifier: License :: OSI Approved :: MIT License
@@ -244,6 +244,7 @@ pub fn to_cpp_enum_variant(raw: &str) -> String {
244
244
  pub struct CppCodegen {
245
245
  options: CppOptions,
246
246
  context: CppLanguageContext,
247
+ split_units: Option<usize>,
247
248
  }
248
249
 
249
250
  /// Emitted C++ identifier for a named type, disambiguated across namespaces
@@ -257,9 +258,16 @@ impl CppCodegen {
257
258
  Self {
258
259
  options,
259
260
  context: CppLanguageContext,
261
+ split_units: None,
260
262
  }
261
263
  }
262
264
 
265
+ /// Emit declarations in `.hpp` and out-of-line definitions split into chunked `.cpp` units.
266
+ pub fn with_split_units(mut self, chunk_size: usize) -> Self {
267
+ self.split_units = Some(chunk_size.max(1));
268
+ self
269
+ }
270
+
263
271
  fn unique_field_name(
264
272
  &self,
265
273
  struct_name: &str,
@@ -399,6 +407,15 @@ impl CppCodegen {
399
407
  match self.options.mode {
400
408
  CppMode::HeaderOnly => {
401
409
  files.push((format!("{}.hpp", stem), self.generate_header(ir)));
410
+ if let Some(chunk_size) = self.split_units {
411
+ let plan = ir.partition_topological_chunks(chunk_size);
412
+ for chunk in &plan.chunks {
413
+ files.push((
414
+ format!("{}_{}.cpp", stem, chunk.name),
415
+ self.generate_chunk_impl(ir, stem, &chunk.types),
416
+ ));
417
+ }
418
+ }
402
419
  }
403
420
  CppMode::Module => {
404
421
  files.push((
@@ -423,6 +440,162 @@ impl CppCodegen {
423
440
  files
424
441
  }
425
442
 
443
+ fn generate_chunk_impl(&self, ir: &SchemaIR, header_stem: &str, types: &[QName]) -> String {
444
+ set_type_name_map(build_type_name_map(ir, to_cpp_type_name));
445
+ let mut out = String::new();
446
+ writeln!(
447
+ out,
448
+ "// @generated by PolyXML Compiler (https://github.com/polyxml/PolyXML)"
449
+ )
450
+ .unwrap();
451
+ writeln!(out, "#include \"{}.hpp\"\n", header_stem).unwrap();
452
+ writeln!(out, "namespace {} {{\n", self.options.namespace).unwrap();
453
+
454
+ for qname in types {
455
+ let Some(type_def) = ir.types.get(qname) else {
456
+ continue;
457
+ };
458
+ match type_def {
459
+ TypeDef::Enum(enum_def) if self.options.emit_enum_converters => {
460
+ let enum_name = type_ident(&enum_def.qname);
461
+ let mut seen = HashSet::new();
462
+ let variant_names: Vec<String> = enum_def
463
+ .variants
464
+ .iter()
465
+ .map(|v| self.unique_enum_variant(&v.name, &mut seen))
466
+ .collect();
467
+ let natural = to_cpp_type_name(&enum_def.qname.local);
468
+ let stem = if enum_name == natural {
469
+ AsSnakeCase(&enum_def.qname.local).to_string()
470
+ } else {
471
+ AsSnakeCase(&enum_name).to_string()
472
+ };
473
+ let func_name = format!("{stem}_from_string");
474
+ self.emit_enum_converters_impl(
475
+ &mut out,
476
+ enum_def,
477
+ &enum_name,
478
+ &variant_names,
479
+ &func_name,
480
+ false,
481
+ );
482
+ }
483
+ TypeDef::Union(u) => {
484
+ let union_name = type_ident(&u.qname);
485
+ let mut branch_types = Vec::new();
486
+ let mut seen_types = HashSet::new();
487
+ let mut needs_wrappers = false;
488
+ for b in &u.branches {
489
+ let mapped = self.context.map_type_ref(&b.type_ref);
490
+ if matches!(b.type_ref, TypeRef::Primitive(_))
491
+ || seen_types.contains(&mapped)
492
+ {
493
+ needs_wrappers = true;
494
+ }
495
+ seen_types.insert(mapped.clone());
496
+ branch_types.push((b, mapped));
497
+ }
498
+ let mut variant_params = Vec::new();
499
+ if needs_wrappers {
500
+ let mut seen_wrappers = HashSet::new();
501
+ for (branch, _) in &branch_types {
502
+ let base_wrapper =
503
+ format!("{}{}", union_name, to_cpp_type_name(&branch.variant_name));
504
+ let mut wrapper_name = base_wrapper.clone();
505
+ let mut idx = 2;
506
+ while !seen_wrappers.insert(wrapper_name.clone()) {
507
+ wrapper_name = format!("{base_wrapper}_{idx}");
508
+ idx += 1;
509
+ }
510
+ if self.options.emit_equality_operators {
511
+ writeln!(
512
+ out,
513
+ "bool {}::operator==(const {}&) const = default;",
514
+ wrapper_name, wrapper_name
515
+ )
516
+ .unwrap();
517
+ }
518
+ variant_params.push(wrapper_name);
519
+ }
520
+ } else {
521
+ for (_, mapped) in &branch_types {
522
+ variant_params.push(mapped.clone());
523
+ }
524
+ }
525
+ if variant_params.len() > 16 {
526
+ for (idx, param) in variant_params.iter().enumerate() {
527
+ writeln!(
528
+ out,
529
+ "{}::{}({} v) : kind({}), data(std::make_shared<{}>(std::move(v))) {{}}",
530
+ union_name, union_name, param, idx, param
531
+ )
532
+ .unwrap();
533
+ }
534
+ writeln!(
535
+ out,
536
+ "std::size_t {}::index() const noexcept {{ return kind; }}",
537
+ union_name
538
+ )
539
+ .unwrap();
540
+ if self.options.emit_equality_operators {
541
+ writeln!(
542
+ out,
543
+ "bool {}::operator==(const {}& other) const {{",
544
+ union_name, union_name
545
+ )
546
+ .unwrap();
547
+ writeln!(out, " if (kind != other.kind) return false;").unwrap();
548
+ writeln!(out, " if (!data && !other.data) return true;").unwrap();
549
+ writeln!(out, " if (!data || !other.data) return false;").unwrap();
550
+ writeln!(out, " switch (kind) {{").unwrap();
551
+ for (idx, param) in variant_params.iter().enumerate() {
552
+ writeln!(
553
+ out,
554
+ " case {}: return *static_cast<const {}*>(data.get()) == *static_cast<const {}*>(other.data.get());",
555
+ idx, param, param
556
+ )
557
+ .unwrap();
558
+ }
559
+ writeln!(out, " default: return false;").unwrap();
560
+ writeln!(out, " }}").unwrap();
561
+ writeln!(out, "}}\n").unwrap();
562
+ }
563
+ }
564
+ }
565
+ TypeDef::Struct(s) => {
566
+ let struct_name = type_ident(&s.qname);
567
+ if self.options.emit_equality_operators {
568
+ writeln!(
569
+ out,
570
+ "bool {}::operator==(const {}&) const = default;",
571
+ struct_name, struct_name
572
+ )
573
+ .unwrap();
574
+ }
575
+ if self.options.validate_facets {
576
+ let has_simple_content_base =
577
+ s.base_type.as_ref().is_some_and(|base_qname| {
578
+ !matches!(ir.types.get(base_qname), Some(TypeDef::Struct(_)))
579
+ && (PrimitiveType::from_xsd_name(&base_qname.local).is_some()
580
+ || matches!(
581
+ ir.types.get(base_qname),
582
+ Some(TypeDef::Simple(_))
583
+ ))
584
+ });
585
+ let field_names =
586
+ self.struct_field_names(s, &struct_name, has_simple_content_base);
587
+ let sig = format!("bool {}::validate() const noexcept {{", struct_name);
588
+ self.emit_struct_validator_with_sig(&mut out, s, ir, &field_names, &sig);
589
+ }
590
+ }
591
+ _ => {}
592
+ }
593
+ }
594
+
595
+ writeln!(out, "\n}} // namespace {}", self.options.namespace).unwrap();
596
+ out
597
+ }
598
+
426
599
  /// Generate CMakeLists.txt definition for integration via add_subdirectory or FetchContent.
427
600
  pub fn generate_cmake(&self, project_name: &str) -> String {
428
601
  let safe_name = AsSnakeCase(project_name).to_string();
@@ -779,31 +952,6 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
779
952
  writeln!(out, "}};\n").unwrap();
780
953
 
781
954
  if self.options.emit_enum_converters {
782
- // to_string
783
- writeln!(
784
- out,
785
- "[[nodiscard]] inline constexpr std::string_view to_string({} value) noexcept {{",
786
- enum_name
787
- )
788
- .unwrap();
789
- writeln!(out, " switch (value) {{").unwrap();
790
- for (variant, var_name) in enum_def.variants.iter().zip(&variant_names) {
791
- writeln!(
792
- out,
793
- " case {}::{}: return {};",
794
- enum_name,
795
- var_name,
796
- super::cpp_string_literal(&variant.value)
797
- )
798
- .unwrap();
799
- }
800
- writeln!(out, " }}").unwrap();
801
- writeln!(out, " return \"\";").unwrap();
802
- writeln!(out, "}}\n").unwrap();
803
-
804
- // from_string — stem follows the disambiguated type name only when
805
- // it differs from the natural one, preserving the historical
806
- // snake-of-local form otherwise.
807
955
  let natural = to_cpp_type_name(&enum_def.qname.local);
808
956
  let stem = if enum_name == natural {
809
957
  AsSnakeCase(&enum_def.qname.local).to_string()
@@ -811,27 +959,88 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
811
959
  AsSnakeCase(&enum_name).to_string()
812
960
  };
813
961
  let func_name = format!("{stem}_from_string");
814
- writeln!(
815
- out,
816
- "[[nodiscard]] inline std::optional<{}> {}(std::string_view s) noexcept {{",
817
- enum_name, func_name
818
- )
819
- .unwrap();
820
- for (variant, var_name) in enum_def.variants.iter().zip(&variant_names) {
962
+ if self.split_units.is_some() {
963
+ writeln!(
964
+ out,
965
+ "[[nodiscard]] std::string_view to_string({} value) noexcept;",
966
+ enum_name
967
+ )
968
+ .unwrap();
821
969
  writeln!(
822
970
  out,
823
- " if (s == {}) return {}::{};",
824
- super::cpp_string_literal(&variant.value),
825
- enum_name,
826
- var_name
971
+ "[[nodiscard]] std::optional<{}> {}(std::string_view s) noexcept;\n",
972
+ enum_name, func_name
827
973
  )
828
974
  .unwrap();
975
+ } else {
976
+ self.emit_enum_converters_impl(
977
+ out,
978
+ enum_def,
979
+ &enum_name,
980
+ &variant_names,
981
+ &func_name,
982
+ true,
983
+ );
829
984
  }
830
- writeln!(out, " return std::nullopt;").unwrap();
831
- writeln!(out, "}}\n").unwrap();
832
985
  }
833
986
  }
834
987
 
988
+ fn emit_enum_converters_impl(
989
+ &self,
990
+ out: &mut String,
991
+ enum_def: &EnumDef,
992
+ enum_name: &str,
993
+ variant_names: &[String],
994
+ func_name: &str,
995
+ inline: bool,
996
+ ) {
997
+ let qualifier = if inline {
998
+ "[[nodiscard]] inline constexpr "
999
+ } else {
1000
+ ""
1001
+ };
1002
+ writeln!(
1003
+ out,
1004
+ "{}std::string_view to_string({} value) noexcept {{",
1005
+ qualifier, enum_name
1006
+ )
1007
+ .unwrap();
1008
+ writeln!(out, " switch (value) {{").unwrap();
1009
+ for (variant, var_name) in enum_def.variants.iter().zip(variant_names) {
1010
+ writeln!(
1011
+ out,
1012
+ " case {}::{}: return {};",
1013
+ enum_name,
1014
+ var_name,
1015
+ super::cpp_string_literal(&variant.value)
1016
+ )
1017
+ .unwrap();
1018
+ }
1019
+ writeln!(out, " }}").unwrap();
1020
+ writeln!(out, " return \"\";").unwrap();
1021
+ writeln!(out, "}}\n").unwrap();
1022
+
1023
+ let from_qualifier = if inline { "[[nodiscard]] inline " } else { "" };
1024
+ writeln!(
1025
+ out,
1026
+ "{}std::optional<{}> {}(std::string_view s) noexcept {{",
1027
+ from_qualifier, enum_name, func_name
1028
+ )
1029
+ .unwrap();
1030
+ for (variant, var_name) in enum_def.variants.iter().zip(variant_names) {
1031
+ writeln!(
1032
+ out,
1033
+ " if (s == {}) return {}::{};",
1034
+ super::cpp_string_literal(&variant.value),
1035
+ enum_name,
1036
+ var_name
1037
+ )
1038
+ .unwrap();
1039
+ }
1040
+ writeln!(out, " return std::nullopt;").unwrap();
1041
+ writeln!(out, "}}\n").unwrap();
1042
+ }
1043
+
835
1044
  fn emit_union(&self, out: &mut String, u: &UnionDef) {
836
1045
  if let Some(ref doc) = u.documentation {
837
1046
  for line in doc.lines() {
@@ -875,10 +1084,15 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
875
1084
  writeln!(out, "struct {} {{", wrapper_name).unwrap();
876
1085
  writeln!(out, " {} value = {{}};", mapped_type).unwrap();
877
1086
  if self.options.emit_equality_operators {
1087
+ let eq_suffix = if self.split_units.is_some() {
1088
+ ";"
1089
+ } else {
1090
+ " = default;"
1091
+ };
878
1092
  writeln!(
879
1093
  out,
880
- " bool operator==(const {}&) const = default;",
881
- wrapper_name
1094
+ " bool operator==(const {}&) const{}",
1095
+ wrapper_name, eq_suffix
882
1096
  )
883
1097
  .unwrap();
884
1098
  }
@@ -891,13 +1105,72 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
891
1105
  }
892
1106
  }
893
1107
 
894
- writeln!(
895
- out,
896
- "using {} = std::variant<{}>;\n",
897
- union_name,
898
- variant_params.join(", ")
899
- )
900
- .unwrap();
1108
+ if variant_params.len() > 16 {
1109
+ writeln!(out, "struct {} {{", union_name).unwrap();
1110
+ writeln!(out, " std::size_t kind = 0;").unwrap();
1111
+ writeln!(out, " std::shared_ptr<const void> data = {{}};").unwrap();
1112
+ writeln!(out, " {}() = default;", union_name).unwrap();
1113
+ if self.split_units.is_some() {
1114
+ for param in &variant_params {
1115
+ writeln!(out, " {}({} v);", union_name, param).unwrap();
1116
+ }
1117
+ writeln!(out, " [[nodiscard]] std::size_t index() const noexcept;").unwrap();
1118
+ if self.options.emit_equality_operators {
1119
+ writeln!(
1120
+ out,
1121
+ " bool operator==(const {}& other) const;",
1122
+ union_name
1123
+ )
1124
+ .unwrap();
1125
+ }
1126
+ } else {
1127
+ for (idx, param) in variant_params.iter().enumerate() {
1128
+ writeln!(
1129
+ out,
1130
+ " {}({} v) : kind({}), data(std::make_shared<{}>(std::move(v))) {{}}",
1131
+ union_name, param, idx, param
1132
+ )
1133
+ .unwrap();
1134
+ }
1135
+ writeln!(
1136
+ out,
1137
+ " [[nodiscard]] std::size_t index() const noexcept {{ return kind; }}"
1138
+ )
1139
+ .unwrap();
1140
+ if self.options.emit_equality_operators {
1141
+ writeln!(
1142
+ out,
1143
+ " bool operator==(const {}& other) const {{",
1144
+ union_name
1145
+ )
1146
+ .unwrap();
1147
+ writeln!(out, " if (kind != other.kind) return false;").unwrap();
1148
+ writeln!(out, " if (!data && !other.data) return true;").unwrap();
1149
+ writeln!(out, " if (!data || !other.data) return false;").unwrap();
1150
+ writeln!(out, " switch (kind) {{").unwrap();
1151
+ for (idx, param) in variant_params.iter().enumerate() {
1152
+ writeln!(
1153
+ out,
1154
+ " case {}: return *static_cast<const {}*>(data.get()) == *static_cast<const {}*>(other.data.get());",
1155
+ idx, param, param
1156
+ )
1157
+ .unwrap();
1158
+ }
1159
+ writeln!(out, " default: return false;").unwrap();
1160
+ writeln!(out, " }}").unwrap();
1161
+ writeln!(out, " }}").unwrap();
1162
+ }
1163
+ }
1164
+ writeln!(out, "}};\n").unwrap();
1165
+ } else {
1166
+ writeln!(
1167
+ out,
1168
+ "using {} = std::variant<{}>;\n",
1169
+ union_name,
1170
+ variant_params.join(", ")
1171
+ )
1172
+ .unwrap();
1173
+ }
901
1174
  }
902
1175
 
903
1176
  fn emit_struct(&self, out: &mut String, s: &StructDef, ir: &SchemaIR) {
@@ -942,21 +1215,7 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
942
1215
  }
943
1216
  }
944
1217
 
945
- let mut seen = HashSet::new();
946
- if simple_content_base.is_some() {
947
- let has_value_field = s
948
- .fields
949
- .iter()
950
- .any(|f| f.name == "value" || f.kind == crate::ir::FieldKind::Text);
951
- if !has_value_field {
952
- seen.insert("value".to_string());
953
- }
954
- }
955
- let field_names: Vec<String> = s
956
- .fields
957
- .iter()
958
- .map(|f| self.unique_field_name(&struct_name, &f.name, &mut seen))
959
- .collect();
1218
+ let field_names = self.struct_field_names(s, &struct_name, simple_content_base.is_some());
960
1219
 
961
1220
  // Fields
962
1221
  for (f, field_name) in s.fields.iter().zip(&field_names) {
@@ -968,16 +1227,25 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
968
1227
  }
969
1228
 
970
1229
  if self.options.emit_equality_operators {
1230
+ let eq_suffix = if self.split_units.is_some() {
1231
+ ";"
1232
+ } else {
1233
+ " = default;"
1234
+ };
971
1235
  writeln!(
972
1236
  out,
973
- "\n bool operator==(const {}&) const = default;",
974
- struct_name
1237
+ "\n bool operator==(const {}&) const{}",
1238
+ struct_name, eq_suffix
975
1239
  )
976
1240
  .unwrap();
977
1241
  }
978
1242
 
979
1243
  if self.options.validate_facets {
980
- self.emit_struct_validator(out, s, ir, &field_names);
1244
+ if self.split_units.is_some() {
1245
+ writeln!(out, "\n [[nodiscard]] bool validate() const noexcept;").unwrap();
1246
+ } else {
1247
+ self.emit_struct_validator(out, s, ir, &field_names);
1248
+ }
981
1249
  }
982
1250
 
983
1251
  writeln!(out, "}};\n").unwrap();
@@ -1086,6 +1354,28 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
1086
1354
  }
1087
1355
  }
1088
1356
 
1357
+ fn struct_field_names(
1358
+ &self,
1359
+ s: &StructDef,
1360
+ struct_name: &str,
1361
+ has_simple_content_base: bool,
1362
+ ) -> Vec<String> {
1363
+ let mut seen = HashSet::new();
1364
+ if has_simple_content_base {
1365
+ let has_value_field = s
1366
+ .fields
1367
+ .iter()
1368
+ .any(|f| f.name == "value" || f.kind == crate::ir::FieldKind::Text);
1369
+ if !has_value_field {
1370
+ seen.insert("value".to_string());
1371
+ }
1372
+ }
1373
+ s.fields
1374
+ .iter()
1375
+ .map(|f| self.unique_field_name(struct_name, &f.name, &mut seen))
1376
+ .collect()
1377
+ }
1378
+
1089
1379
  fn emit_struct_validator(
1090
1380
  &self,
1091
1381
  out: &mut String,
@@ -1093,7 +1383,24 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
1093
1383
  ir: &SchemaIR,
1094
1384
  field_names: &[String],
1095
1385
  ) {
1096
- writeln!(out, "\n [[nodiscard]] bool validate() const noexcept {{").unwrap();
1386
+ self.emit_struct_validator_with_sig(
1387
+ out,
1388
+ s,
1389
+ ir,
1390
+ field_names,
1391
+ "\n [[nodiscard]] bool validate() const noexcept {",
1392
+ );
1393
+ }
1394
+
1395
+ fn emit_struct_validator_with_sig(
1396
+ &self,
1397
+ out: &mut String,
1398
+ s: &StructDef,
1399
+ ir: &SchemaIR,
1400
+ field_names: &[String],
1401
+ signature: &str,
1402
+ ) {
1403
+ writeln!(out, "{}", signature).unwrap();
1097
1404
 
1098
1405
  let mut has_checks = false;
1099
1406
  for (f, field_name) in s.fields.iter().zip(field_names) {
@@ -242,6 +242,7 @@ pub fn to_go_package_name(raw: &str) -> String {
242
242
  pub struct GoCodegen {
243
243
  options: GoOptions,
244
244
  context: GoLanguageContext,
245
+ shared_utils_import: Option<String>,
245
246
  }
246
247
 
247
248
  /// Emitted Go identifier for a named type, disambiguated across namespaces
@@ -295,9 +296,41 @@ impl GoCodegen {
295
296
  Self {
296
297
  options,
297
298
  context: GoLanguageContext,
299
+ shared_utils_import: None,
298
300
  }
299
301
  }
300
302
 
303
+ /// Share temporal, integer, and wildcard helper types from an external `utils` package.
304
+ pub fn with_shared_utils_import(mut self, import_path: impl Into<String>) -> Self {
305
+ self.shared_utils_import = Some(import_path.into());
306
+ self
307
+ }
308
+
309
+ /// Generate a standalone shared `utils` package containing temporal, integer, and wildcard types.
310
+ pub fn generate_shared_utils(&self, package_name: &str) -> String {
311
+ let mut out = String::new();
312
+ writeln!(
313
+ out,
314
+ "// Code generated by PolyXML Compiler (https://github.com/polyxml/PolyXML). DO NOT EDIT."
315
+ )
316
+ .unwrap();
317
+ writeln!(
318
+ out,
319
+ "// @generated by PolyXML Compiler (https://github.com/polyxml/PolyXML)\n"
320
+ )
321
+ .unwrap();
322
+ writeln!(out, "package {}\n", to_go_package_name(package_name)).unwrap();
323
+ writeln!(
324
+ out,
325
+ "import (\n \"encoding/xml\"\n \"fmt\"\n \"math/big\"\n \"strings\"\n \"time\"\n)\n"
326
+ )
327
+ .unwrap();
328
+ self.emit_temporal_types(&mut out);
329
+ self.emit_integer_types(&mut out);
330
+ out.push_str("// AnyElement represents an unmapped wildcard XML element (xs:any).\ntype AnyElement struct {\n XMLName xml.Name\n Attrs []xml.Attr `xml:\",any,attr\" json:\"attrs,omitempty\"`\n Content string `xml:\",innerxml\" json:\"content,omitempty\"`\n}\n");
331
+ out
332
+ }
333
+
301
334
  /// Generate complete Go module content.
302
335
  pub fn generate_module(&self, ir: &SchemaIR) -> String {
303
336
  set_type_name_map(build_type_name_map(ir, to_go_type_name));
@@ -372,16 +405,16 @@ impl GoCodegen {
372
405
 
373
406
  // Generate types
374
407
  self.emit_types(&mut body, ir);
375
- if has_time {
408
+ if self.shared_utils_import.is_none() && has_time {
376
409
  self.emit_temporal_types(&mut body);
377
410
  }
378
411
  self.emit_root_aliases(&mut body, ir);
379
- if body.contains("PolyxmlInteger")
412
+ let uses_integer_types = body.contains("PolyxmlInteger")
380
413
  || body.contains("PolyxmlPositiveInteger")
381
414
  || body.contains("PolyxmlNegativeInteger")
382
415
  || body.contains("PolyxmlNonPositiveInteger")
383
- || body.contains("PolyxmlNonNegativeInteger")
384
- {
416
+ || body.contains("PolyxmlNonNegativeInteger");
417
+ if self.shared_utils_import.is_none() && uses_integer_types {
385
418
  self.emit_integer_types(&mut body);
386
419
  }
387
420
 
@@ -389,6 +422,28 @@ impl GoCodegen {
389
422
  self.emit_content_helpers(&mut body);
390
423
  }
391
424
 
425
+ let mut shared_aliases = Vec::new();
426
+ if self.shared_utils_import.is_some() {
427
+ for sym in [
428
+ "PolyxmlDate",
429
+ "PolyxmlTime",
430
+ "PolyxmlDateTime",
431
+ "PolyxmlInteger",
432
+ "PolyxmlPositiveInteger",
433
+ "PolyxmlNegativeInteger",
434
+ "PolyxmlNonPositiveInteger",
435
+ "PolyxmlNonNegativeInteger",
436
+ "AnyElement",
437
+ ] {
438
+ if body.contains(sym) {
439
+ shared_aliases.push(sym);
440
+ }
441
+ }
442
+ for sym in &shared_aliases {
443
+ writeln!(body, "type {sym} = polyxmlutils.{sym}").unwrap();
444
+ }
445
+ }
446
+
392
447
  // Assemble final output with package and imports
393
448
  let mut out = String::new();
394
449
  if let Some(ref header) = self.options.custom_header {
@@ -412,24 +467,29 @@ impl GoCodegen {
412
467
  let pkg = to_go_package_name(&self.options.package_name);
413
468
  writeln!(out, "package {}\n", pkg).unwrap();
414
469
 
415
- let mut imports = Vec::new();
470
+ let mut imports: Vec<String> = Vec::new();
471
+ if let Some(ref utils_import) = self.shared_utils_import {
472
+ if !shared_aliases.is_empty() {
473
+ imports.push(format!("polyxmlutils \"{utils_import}\""));
474
+ }
475
+ }
416
476
  if body.contains("big.") {
417
- imports.push("\"math/big\"");
477
+ imports.push("\"math/big\"".to_string());
418
478
  }
419
479
  if body.contains("xml.") {
420
- imports.push("\"encoding/xml\"");
480
+ imports.push("\"encoding/xml\"".to_string());
421
481
  }
422
482
  if body.contains("bytes.") {
423
- imports.push("\"bytes\"");
483
+ imports.push("\"bytes\"".to_string());
424
484
  }
425
485
  if has_fmt || body.contains("fmt.") {
426
- imports.push("\"fmt\"");
486
+ imports.push("\"fmt\"".to_string());
427
487
  }
428
488
  if has_io || body.contains("io.") {
429
- imports.push("\"io\"");
489
+ imports.push("\"io\"".to_string());
430
490
  }
431
491
  if has_patterns || body.contains("regexp.") {
432
- imports.push("\"regexp\"");
492
+ imports.push("\"regexp\"".to_string());
433
493
  }
434
494
  if ir
435
495
  .types
@@ -437,13 +497,13 @@ impl GoCodegen {
437
497
  .any(|def| matches!(def, TypeDef::Union(u) if u.is_lexical()))
438
498
  && body.contains("strconv.")
439
499
  {
440
- imports.push("\"strconv\"");
500
+ imports.push("\"strconv\"".to_string());
441
501
  }
442
502
  if body.contains("strings.") {
443
- imports.push("\"strings\"");
503
+ imports.push("\"strings\"".to_string());
444
504
  }
445
- if has_time {
446
- imports.push("\"time\"");
505
+ if has_time && self.shared_utils_import.is_none() {
506
+ imports.push("\"time\"".to_string());
447
507
  }
448
508
 
449
509
  if !imports.is_empty() {
@@ -519,7 +579,7 @@ impl GoCodegen {
519
579
  TypeDef::Struct(s) => s.fields.iter().any(|f| f.kind == FieldKind::Any),
520
580
  _ => false,
521
581
  });
522
- if has_any_element {
582
+ if has_any_element && self.shared_utils_import.is_none() {
523
583
  out.push_str("// AnyElement represents an unmapped wildcard XML element (xs:any).\ntype AnyElement struct {\n XMLName xml.Name\n Attrs []xml.Attr `xml:\",any,attr\" json:\"attrs,omitempty\"`\n Content string `xml:\",innerxml\" json:\"content,omitempty\"`\n}\n\n");
524
584
  }
525
585
  }
@@ -4,7 +4,7 @@ build-backend = "maturin"
4
4
 
5
5
  [project]
6
6
  name = "polyxml"
7
- version = "0.35.6"
7
+ version = "0.37.0"
8
8
  description = "High-performance, polyglot XML data-binding engine built in Rust"
9
9
  authors = [
10
10
  { name = "Bailey Nguyen", email = "bailey.tan.nguyen@gmail.com" }
File without changes