(data_enum: &DataEnum, default_variant_value: u32)
| 359 | } |
| 360 | |
| 361 | fn xlang_variant_branches(data_enum: &DataEnum, default_variant_value: u32) -> Vec<TokenStream> { |
| 362 | let is_union_compatible = is_union_compatible_enum(data_enum); |
| 363 | |
| 364 | data_enum |
| 365 | .variants |
| 366 | .iter() |
| 367 | .enumerate() |
| 368 | .map(|(idx, v)| { |
| 369 | let ident = &v.ident; |
| 370 | if is_runtime_unknown_variant(v) { |
| 371 | return quote! { |
| 372 | Self::#ident(ref unknown) => { |
| 373 | context.writer.write_var_u32(unknown.case_id()); |
| 374 | fory_core::serializer::unknown_case::write_payload(context, unknown)?; |
| 375 | } |
| 376 | }; |
| 377 | } |
| 378 | |
| 379 | let mut tag_value = if is_union_compatible { |
| 380 | xlang_union_case_id(data_enum, idx, v) |
| 381 | } else { |
| 382 | idx as u32 |
| 383 | }; |
| 384 | if is_skip_enum_variant(v) { |
| 385 | tag_value = default_variant_value; |
| 386 | } |
| 387 | |
| 388 | match &v.fields { |
| 389 | Fields::Unit => { |
| 390 | if is_union_compatible { |
| 391 | // Union-compatible: write tag + null flag (matches Java/C++ Union with null value) |
| 392 | quote! { |
| 393 | Self::#ident => { |
| 394 | context.writer.write_var_u32(#tag_value); |
| 395 | // Write null flag for unit variant (no value) |
| 396 | context.writer.write_i8(fory_core::RefFlag::Null as i8); |
| 397 | } |
| 398 | } |
| 399 | } else { |
| 400 | quote! { |
| 401 | Self::#ident => { |
| 402 | context.writer.write_var_u32(#tag_value); |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | Fields::Unnamed(fields_unnamed) => { |
| 408 | if is_union_compatible && fields_unnamed.unnamed.len() == 1 { |
| 409 | let source_fields = unnamed_source_fields(fields_unnamed); |
| 410 | let write_payload = |
| 411 | gen_write_single_payload(&source_fields, quote! { value }); |
| 412 | quote! { |
| 413 | Self::#ident(ref value) => { |
| 414 | context.writer.write_var_u32(#tag_value); |
| 415 | #write_payload |
| 416 | } |
| 417 | } |
| 418 | } else { |
no test coverage detected