polyxml 0.36.0__tar.gz → 0.38.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.36.0 → polyxml-0.38.0}/Cargo.lock +6 -6
  2. {polyxml-0.36.0 → polyxml-0.38.0}/Cargo.toml +1 -1
  3. {polyxml-0.36.0 → polyxml-0.38.0}/PKG-INFO +1 -1
  4. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/codegen/cpp/mod.rs +391 -66
  5. {polyxml-0.36.0 → polyxml-0.38.0}/pyproject.toml +1 -1
  6. {polyxml-0.36.0 → polyxml-0.38.0}/README.md +0 -0
  7. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/Cargo.toml +0 -0
  8. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/README.md +0 -0
  9. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/benches/core_benchmarks.rs +0 -0
  10. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/benches/support/tag_dispatch_fixtures.rs +0 -0
  11. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/benches/tag_dispatch.rs +0 -0
  12. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/codegen/csharp/mod.rs +0 -0
  13. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/codegen/go/mod.rs +0 -0
  14. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/codegen/java/codec.rs +0 -0
  15. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/codegen/java/mod.rs +0 -0
  16. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/codegen/java/models.rs +0 -0
  17. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/codegen/mod.rs +0 -0
  18. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/codegen/python/aot.rs +0 -0
  19. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/codegen/python/mod.rs +0 -0
  20. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/codegen/rust/mod.rs +0 -0
  21. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/codegen/typescript/mod.rs +0 -0
  22. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/converters.rs +0 -0
  23. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/error.rs +0 -0
  24. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/integer.rs +0 -0
  25. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/ir/chunker.rs +0 -0
  26. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/ir/mod.rs +0 -0
  27. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/ir/particle.rs +0 -0
  28. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/ir/tarjan.rs +0 -0
  29. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/json.rs +0 -0
  30. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/lib.rs +0 -0
  31. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/parser.rs +0 -0
  32. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/pattern.rs +0 -0
  33. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/schema.rs +0 -0
  34. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/schema_parser/mod.rs +0 -0
  35. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/schema_parser/upa.rs +0 -0
  36. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/serializer.rs +0 -0
  37. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/transcoder.rs +0 -0
  38. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/src/value.rs +0 -0
  39. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/fixtures/lexical_union.xsd +0 -0
  40. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/fixtures/mixed_content.xsd +0 -0
  41. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/fixtures/ts_mixed_content_inheritance.xsd +0 -0
  42. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_adversarial_identifiers.rs +0 -0
  43. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_any_attribute.rs +0 -0
  44. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_any_element.rs +0 -0
  45. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_conformance.rs +0 -0
  46. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_core.rs +0 -0
  47. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_corpus_regressions.rs +0 -0
  48. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_cpp_codegen.rs +0 -0
  49. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_csharp_codegen.rs +0 -0
  50. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_default_literal_codegen.rs +0 -0
  51. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_enum_literal_codegen.rs +0 -0
  52. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_go_codegen.rs +0 -0
  53. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_inherited_simple_content.rs +0 -0
  54. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_inherited_text_codegen.rs +0 -0
  55. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_java_codegen.rs +0 -0
  56. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_json.rs +0 -0
  57. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_lexical_lists.rs +0 -0
  58. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_lexical_union_codegen.rs +0 -0
  59. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_mixed_branch_lookup.rs +0 -0
  60. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_mixed_content.rs +0 -0
  61. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_mixed_nil.rs +0 -0
  62. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_namespace_entities.rs +0 -0
  63. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_pattern_codegen.rs +0 -0
  64. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_python_codegen.rs +0 -0
  65. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_qualified_attributes.rs +0 -0
  66. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_runtime_fast_paths.rs +0 -0
  67. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_rust_codecs.rs +0 -0
  68. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_rust_codegen.rs +0 -0
  69. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_schema_attribute_entities.rs +0 -0
  70. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_schema_audit.rs +0 -0
  71. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_schema_ir.rs +0 -0
  72. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_security.rs +0 -0
  73. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_temporal_union.rs +0 -0
  74. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_transcoder.rs +0 -0
  75. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_ts_codegen.rs +0 -0
  76. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_ubl_csharp.rs +0 -0
  77. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_unbounded_choice.rs +0 -0
  78. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_upa.rs +0 -0
  79. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_value_constraints.rs +0 -0
  80. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_variant_registry.rs +0 -0
  81. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-core/tests/test_xsi_type.rs +0 -0
  82. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-python/Cargo.toml +0 -0
  83. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-python/README.md +0 -0
  84. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-python/benches/aot_vs_dataclass.py +0 -0
  85. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-python/src/lib.rs +0 -0
  86. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-python/tests/test_binary_fields.py +0 -0
  87. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-python/tests/test_conformance.py +0 -0
  88. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-python/tests/test_generated_models.py +0 -0
  89. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-python/tests/test_json.py +0 -0
  90. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-python/tests/test_mixed_content.py +0 -0
  91. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-python/tests/test_polyxml.py +0 -0
  92. {polyxml-0.36.0 → polyxml-0.38.0}/crates/polyxml-python/tests/test_xsi_type.py +0 -0
  93. {polyxml-0.36.0 → polyxml-0.38.0}/python/polyxml/AGENT_GUIDE.md +0 -0
  94. {polyxml-0.36.0 → polyxml-0.38.0}/python/polyxml/__init__.py +0 -0
  95. {polyxml-0.36.0 → polyxml-0.38.0}/python/polyxml/compat/__init__.py +0 -0
  96. {polyxml-0.36.0 → polyxml-0.38.0}/python/polyxml/compat/xsdata.py +0 -0
  97. {polyxml-0.36.0 → polyxml-0.38.0}/python/polyxml/py.typed +0 -0
@@ -610,7 +610,7 @@ dependencies = [
610
610
 
611
611
  [[package]]
612
612
  name = "polyxml"
613
- version = "0.36.0"
613
+ version = "0.38.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.36.0"
634
+ version = "0.38.0"
635
635
  dependencies = [
636
636
  "polyxml",
637
637
  ]
638
638
 
639
639
  [[package]]
640
640
  name = "polyxml-cli"
641
- version = "0.36.0"
641
+ version = "0.38.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.36.0"
655
+ version = "0.38.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.36.0"
665
+ version = "0.38.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.36.0"
677
+ version = "0.38.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.36.0"
6
+ version = "0.38.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.36.0
3
+ Version: 0.38.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,33 @@ 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
+ let mut large_union_idx = 0usize;
413
+ for chunk in &plan.chunks {
414
+ let mut regular_types = Vec::with_capacity(chunk.types.len());
415
+ for qname in &chunk.types {
416
+ if matches!(
417
+ ir.types.get(qname),
418
+ Some(TypeDef::Union(u)) if u.branches.len() > 64
419
+ ) {
420
+ files.push((
421
+ format!("{}_union_{:02}.cpp", stem, large_union_idx),
422
+ self.generate_chunk_impl(ir, stem, std::slice::from_ref(qname)),
423
+ ));
424
+ large_union_idx += 1;
425
+ } else {
426
+ regular_types.push(qname.clone());
427
+ }
428
+ }
429
+ if !regular_types.is_empty() {
430
+ files.push((
431
+ format!("{}_{}.cpp", stem, chunk.name),
432
+ self.generate_chunk_impl(ir, stem, &regular_types),
433
+ ));
434
+ }
435
+ }
436
+ }
402
437
  }
403
438
  CppMode::Module => {
404
439
  files.push((
@@ -423,6 +458,162 @@ impl CppCodegen {
423
458
  files
424
459
  }
425
460
 
461
+ fn generate_chunk_impl(&self, ir: &SchemaIR, header_stem: &str, types: &[QName]) -> String {
462
+ set_type_name_map(build_type_name_map(ir, to_cpp_type_name));
463
+ let mut out = String::new();
464
+ writeln!(
465
+ out,
466
+ "// @generated by PolyXML Compiler (https://github.com/polyxml/PolyXML)"
467
+ )
468
+ .unwrap();
469
+ writeln!(out, "#include \"{}.hpp\"\n", header_stem).unwrap();
470
+ writeln!(out, "namespace {} {{\n", self.options.namespace).unwrap();
471
+
472
+ for qname in types {
473
+ let Some(type_def) = ir.types.get(qname) else {
474
+ continue;
475
+ };
476
+ match type_def {
477
+ TypeDef::Enum(enum_def) if self.options.emit_enum_converters => {
478
+ let enum_name = type_ident(&enum_def.qname);
479
+ let mut seen = HashSet::new();
480
+ let variant_names: Vec<String> = enum_def
481
+ .variants
482
+ .iter()
483
+ .map(|v| self.unique_enum_variant(&v.name, &mut seen))
484
+ .collect();
485
+ let natural = to_cpp_type_name(&enum_def.qname.local);
486
+ let stem = if enum_name == natural {
487
+ AsSnakeCase(&enum_def.qname.local).to_string()
488
+ } else {
489
+ AsSnakeCase(&enum_name).to_string()
490
+ };
491
+ let func_name = format!("{stem}_from_string");
492
+ self.emit_enum_converters_impl(
493
+ &mut out,
494
+ enum_def,
495
+ &enum_name,
496
+ &variant_names,
497
+ &func_name,
498
+ false,
499
+ );
500
+ }
501
+ TypeDef::Union(u) => {
502
+ let union_name = type_ident(&u.qname);
503
+ let mut branch_types = Vec::new();
504
+ let mut seen_types = HashSet::new();
505
+ let mut needs_wrappers = false;
506
+ for b in &u.branches {
507
+ let mapped = self.context.map_type_ref(&b.type_ref);
508
+ if matches!(b.type_ref, TypeRef::Primitive(_))
509
+ || seen_types.contains(&mapped)
510
+ {
511
+ needs_wrappers = true;
512
+ }
513
+ seen_types.insert(mapped.clone());
514
+ branch_types.push((b, mapped));
515
+ }
516
+ let mut variant_params = Vec::new();
517
+ if needs_wrappers {
518
+ let mut seen_wrappers = HashSet::new();
519
+ for (branch, _) in &branch_types {
520
+ let base_wrapper =
521
+ format!("{}{}", union_name, to_cpp_type_name(&branch.variant_name));
522
+ let mut wrapper_name = base_wrapper.clone();
523
+ let mut idx = 2;
524
+ while !seen_wrappers.insert(wrapper_name.clone()) {
525
+ wrapper_name = format!("{base_wrapper}_{idx}");
526
+ idx += 1;
527
+ }
528
+ if self.options.emit_equality_operators {
529
+ writeln!(
530
+ out,
531
+ "bool {}::operator==(const {}&) const = default;",
532
+ wrapper_name, wrapper_name
533
+ )
534
+ .unwrap();
535
+ }
536
+ variant_params.push(wrapper_name);
537
+ }
538
+ } else {
539
+ for (_, mapped) in &branch_types {
540
+ variant_params.push(mapped.clone());
541
+ }
542
+ }
543
+ if variant_params.len() > 16 {
544
+ for (idx, param) in variant_params.iter().enumerate() {
545
+ writeln!(
546
+ out,
547
+ "{}::{}({} v) : kind({}), data(std::make_shared<{}>(std::move(v))) {{}}",
548
+ union_name, union_name, param, idx, param
549
+ )
550
+ .unwrap();
551
+ }
552
+ writeln!(
553
+ out,
554
+ "std::size_t {}::index() const noexcept {{ return kind; }}",
555
+ union_name
556
+ )
557
+ .unwrap();
558
+ if self.options.emit_equality_operators {
559
+ writeln!(
560
+ out,
561
+ "bool {}::operator==(const {}& other) const {{",
562
+ union_name, union_name
563
+ )
564
+ .unwrap();
565
+ writeln!(out, " if (kind != other.kind) return false;").unwrap();
566
+ writeln!(out, " if (!data && !other.data) return true;").unwrap();
567
+ writeln!(out, " if (!data || !other.data) return false;").unwrap();
568
+ writeln!(out, " switch (kind) {{").unwrap();
569
+ for (idx, param) in variant_params.iter().enumerate() {
570
+ writeln!(
571
+ out,
572
+ " case {}: return *static_cast<const {}*>(data.get()) == *static_cast<const {}*>(other.data.get());",
573
+ idx, param, param
574
+ )
575
+ .unwrap();
576
+ }
577
+ writeln!(out, " default: return false;").unwrap();
578
+ writeln!(out, " }}").unwrap();
579
+ writeln!(out, "}}\n").unwrap();
580
+ }
581
+ }
582
+ }
583
+ TypeDef::Struct(s) => {
584
+ let struct_name = type_ident(&s.qname);
585
+ if self.options.emit_equality_operators {
586
+ writeln!(
587
+ out,
588
+ "bool {}::operator==(const {}&) const = default;",
589
+ struct_name, struct_name
590
+ )
591
+ .unwrap();
592
+ }
593
+ if self.options.validate_facets {
594
+ let has_simple_content_base =
595
+ s.base_type.as_ref().is_some_and(|base_qname| {
596
+ !matches!(ir.types.get(base_qname), Some(TypeDef::Struct(_)))
597
+ && (PrimitiveType::from_xsd_name(&base_qname.local).is_some()
598
+ || matches!(
599
+ ir.types.get(base_qname),
600
+ Some(TypeDef::Simple(_))
601
+ ))
602
+ });
603
+ let field_names =
604
+ self.struct_field_names(s, &struct_name, has_simple_content_base);
605
+ let sig = format!("bool {}::validate() const noexcept {{", struct_name);
606
+ self.emit_struct_validator_with_sig(&mut out, s, ir, &field_names, &sig);
607
+ }
608
+ }
609
+ _ => {}
610
+ }
611
+ }
612
+
613
+ writeln!(out, "\n}} // namespace {}", self.options.namespace).unwrap();
614
+ out
615
+ }
616
+
426
617
  /// Generate CMakeLists.txt definition for integration via add_subdirectory or FetchContent.
427
618
  pub fn generate_cmake(&self, project_name: &str) -> String {
428
619
  let safe_name = AsSnakeCase(project_name).to_string();
@@ -779,31 +970,6 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
779
970
  writeln!(out, "}};\n").unwrap();
780
971
 
781
972
  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
973
  let natural = to_cpp_type_name(&enum_def.qname.local);
808
974
  let stem = if enum_name == natural {
809
975
  AsSnakeCase(&enum_def.qname.local).to_string()
@@ -811,27 +977,88 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
811
977
  AsSnakeCase(&enum_name).to_string()
812
978
  };
813
979
  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) {
980
+ if self.split_units.is_some() {
821
981
  writeln!(
822
982
  out,
823
- " if (s == {}) return {}::{};",
824
- super::cpp_string_literal(&variant.value),
825
- enum_name,
826
- var_name
983
+ "[[nodiscard]] std::string_view to_string({} value) noexcept;",
984
+ enum_name
827
985
  )
828
986
  .unwrap();
987
+ writeln!(
988
+ out,
989
+ "[[nodiscard]] std::optional<{}> {}(std::string_view s) noexcept;\n",
990
+ enum_name, func_name
991
+ )
992
+ .unwrap();
993
+ } else {
994
+ self.emit_enum_converters_impl(
995
+ out,
996
+ enum_def,
997
+ &enum_name,
998
+ &variant_names,
999
+ &func_name,
1000
+ true,
1001
+ );
829
1002
  }
830
- writeln!(out, " return std::nullopt;").unwrap();
831
- writeln!(out, "}}\n").unwrap();
832
1003
  }
833
1004
  }
834
1005
 
1006
+ fn emit_enum_converters_impl(
1007
+ &self,
1008
+ out: &mut String,
1009
+ enum_def: &EnumDef,
1010
+ enum_name: &str,
1011
+ variant_names: &[String],
1012
+ func_name: &str,
1013
+ inline: bool,
1014
+ ) {
1015
+ let qualifier = if inline {
1016
+ "[[nodiscard]] inline constexpr "
1017
+ } else {
1018
+ ""
1019
+ };
1020
+ writeln!(
1021
+ out,
1022
+ "{}std::string_view to_string({} value) noexcept {{",
1023
+ qualifier, enum_name
1024
+ )
1025
+ .unwrap();
1026
+ writeln!(out, " switch (value) {{").unwrap();
1027
+ for (variant, var_name) in enum_def.variants.iter().zip(variant_names) {
1028
+ writeln!(
1029
+ out,
1030
+ " case {}::{}: return {};",
1031
+ enum_name,
1032
+ var_name,
1033
+ super::cpp_string_literal(&variant.value)
1034
+ )
1035
+ .unwrap();
1036
+ }
1037
+ writeln!(out, " }}").unwrap();
1038
+ writeln!(out, " return \"\";").unwrap();
1039
+ writeln!(out, "}}\n").unwrap();
1040
+
1041
+ let from_qualifier = if inline { "[[nodiscard]] inline " } else { "" };
1042
+ writeln!(
1043
+ out,
1044
+ "{}std::optional<{}> {}(std::string_view s) noexcept {{",
1045
+ from_qualifier, enum_name, func_name
1046
+ )
1047
+ .unwrap();
1048
+ for (variant, var_name) in enum_def.variants.iter().zip(variant_names) {
1049
+ writeln!(
1050
+ out,
1051
+ " if (s == {}) return {}::{};",
1052
+ super::cpp_string_literal(&variant.value),
1053
+ enum_name,
1054
+ var_name
1055
+ )
1056
+ .unwrap();
1057
+ }
1058
+ writeln!(out, " return std::nullopt;").unwrap();
1059
+ writeln!(out, "}}\n").unwrap();
1060
+ }
1061
+
835
1062
  fn emit_union(&self, out: &mut String, u: &UnionDef) {
836
1063
  if let Some(ref doc) = u.documentation {
837
1064
  for line in doc.lines() {
@@ -875,10 +1102,15 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
875
1102
  writeln!(out, "struct {} {{", wrapper_name).unwrap();
876
1103
  writeln!(out, " {} value = {{}};", mapped_type).unwrap();
877
1104
  if self.options.emit_equality_operators {
1105
+ let eq_suffix = if self.split_units.is_some() {
1106
+ ";"
1107
+ } else {
1108
+ " = default;"
1109
+ };
878
1110
  writeln!(
879
1111
  out,
880
- " bool operator==(const {}&) const = default;",
881
- wrapper_name
1112
+ " bool operator==(const {}&) const{}",
1113
+ wrapper_name, eq_suffix
882
1114
  )
883
1115
  .unwrap();
884
1116
  }
@@ -891,13 +1123,72 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
891
1123
  }
892
1124
  }
893
1125
 
894
- writeln!(
895
- out,
896
- "using {} = std::variant<{}>;\n",
897
- union_name,
898
- variant_params.join(", ")
899
- )
900
- .unwrap();
1126
+ if variant_params.len() > 16 {
1127
+ writeln!(out, "struct {} {{", union_name).unwrap();
1128
+ writeln!(out, " std::size_t kind = 0;").unwrap();
1129
+ writeln!(out, " std::shared_ptr<const void> data = {{}};").unwrap();
1130
+ writeln!(out, " {}() = default;", union_name).unwrap();
1131
+ if self.split_units.is_some() {
1132
+ for param in &variant_params {
1133
+ writeln!(out, " {}({} v);", union_name, param).unwrap();
1134
+ }
1135
+ writeln!(out, " [[nodiscard]] std::size_t index() const noexcept;").unwrap();
1136
+ if self.options.emit_equality_operators {
1137
+ writeln!(
1138
+ out,
1139
+ " bool operator==(const {}& other) const;",
1140
+ union_name
1141
+ )
1142
+ .unwrap();
1143
+ }
1144
+ } else {
1145
+ for (idx, param) in variant_params.iter().enumerate() {
1146
+ writeln!(
1147
+ out,
1148
+ " {}({} v) : kind({}), data(std::make_shared<{}>(std::move(v))) {{}}",
1149
+ union_name, param, idx, param
1150
+ )
1151
+ .unwrap();
1152
+ }
1153
+ writeln!(
1154
+ out,
1155
+ " [[nodiscard]] std::size_t index() const noexcept {{ return kind; }}"
1156
+ )
1157
+ .unwrap();
1158
+ if self.options.emit_equality_operators {
1159
+ writeln!(
1160
+ out,
1161
+ " bool operator==(const {}& other) const {{",
1162
+ union_name
1163
+ )
1164
+ .unwrap();
1165
+ writeln!(out, " if (kind != other.kind) return false;").unwrap();
1166
+ writeln!(out, " if (!data && !other.data) return true;").unwrap();
1167
+ writeln!(out, " if (!data || !other.data) return false;").unwrap();
1168
+ writeln!(out, " switch (kind) {{").unwrap();
1169
+ for (idx, param) in variant_params.iter().enumerate() {
1170
+ writeln!(
1171
+ out,
1172
+ " case {}: return *static_cast<const {}*>(data.get()) == *static_cast<const {}*>(other.data.get());",
1173
+ idx, param, param
1174
+ )
1175
+ .unwrap();
1176
+ }
1177
+ writeln!(out, " default: return false;").unwrap();
1178
+ writeln!(out, " }}").unwrap();
1179
+ writeln!(out, " }}").unwrap();
1180
+ }
1181
+ }
1182
+ writeln!(out, "}};\n").unwrap();
1183
+ } else {
1184
+ writeln!(
1185
+ out,
1186
+ "using {} = std::variant<{}>;\n",
1187
+ union_name,
1188
+ variant_params.join(", ")
1189
+ )
1190
+ .unwrap();
1191
+ }
901
1192
  }
902
1193
 
903
1194
  fn emit_struct(&self, out: &mut String, s: &StructDef, ir: &SchemaIR) {
@@ -942,21 +1233,7 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
942
1233
  }
943
1234
  }
944
1235
 
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();
1236
+ let field_names = self.struct_field_names(s, &struct_name, simple_content_base.is_some());
960
1237
 
961
1238
  // Fields
962
1239
  for (f, field_name) in s.fields.iter().zip(&field_names) {
@@ -968,16 +1245,25 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
968
1245
  }
969
1246
 
970
1247
  if self.options.emit_equality_operators {
1248
+ let eq_suffix = if self.split_units.is_some() {
1249
+ ";"
1250
+ } else {
1251
+ " = default;"
1252
+ };
971
1253
  writeln!(
972
1254
  out,
973
- "\n bool operator==(const {}&) const = default;",
974
- struct_name
1255
+ "\n bool operator==(const {}&) const{}",
1256
+ struct_name, eq_suffix
975
1257
  )
976
1258
  .unwrap();
977
1259
  }
978
1260
 
979
1261
  if self.options.validate_facets {
980
- self.emit_struct_validator(out, s, ir, &field_names);
1262
+ if self.split_units.is_some() {
1263
+ writeln!(out, "\n [[nodiscard]] bool validate() const noexcept;").unwrap();
1264
+ } else {
1265
+ self.emit_struct_validator(out, s, ir, &field_names);
1266
+ }
981
1267
  }
982
1268
 
983
1269
  writeln!(out, "}};\n").unwrap();
@@ -1086,6 +1372,28 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
1086
1372
  }
1087
1373
  }
1088
1374
 
1375
+ fn struct_field_names(
1376
+ &self,
1377
+ s: &StructDef,
1378
+ struct_name: &str,
1379
+ has_simple_content_base: bool,
1380
+ ) -> Vec<String> {
1381
+ let mut seen = HashSet::new();
1382
+ if has_simple_content_base {
1383
+ let has_value_field = s
1384
+ .fields
1385
+ .iter()
1386
+ .any(|f| f.name == "value" || f.kind == crate::ir::FieldKind::Text);
1387
+ if !has_value_field {
1388
+ seen.insert("value".to_string());
1389
+ }
1390
+ }
1391
+ s.fields
1392
+ .iter()
1393
+ .map(|f| self.unique_field_name(struct_name, &f.name, &mut seen))
1394
+ .collect()
1395
+ }
1396
+
1089
1397
  fn emit_struct_validator(
1090
1398
  &self,
1091
1399
  out: &mut String,
@@ -1093,7 +1401,24 @@ inline int polyxml_integer_compare(std::string_view a,std::string_view b) noexce
1093
1401
  ir: &SchemaIR,
1094
1402
  field_names: &[String],
1095
1403
  ) {
1096
- writeln!(out, "\n [[nodiscard]] bool validate() const noexcept {{").unwrap();
1404
+ self.emit_struct_validator_with_sig(
1405
+ out,
1406
+ s,
1407
+ ir,
1408
+ field_names,
1409
+ "\n [[nodiscard]] bool validate() const noexcept {",
1410
+ );
1411
+ }
1412
+
1413
+ fn emit_struct_validator_with_sig(
1414
+ &self,
1415
+ out: &mut String,
1416
+ s: &StructDef,
1417
+ ir: &SchemaIR,
1418
+ field_names: &[String],
1419
+ signature: &str,
1420
+ ) {
1421
+ writeln!(out, "{}", signature).unwrap();
1097
1422
 
1098
1423
  let mut has_checks = false;
1099
1424
  for (f, field_name) in s.fields.iter().zip(field_names) {
@@ -4,7 +4,7 @@ build-backend = "maturin"
4
4
 
5
5
  [project]
6
6
  name = "polyxml"
7
- version = "0.36.0"
7
+ version = "0.38.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