| 8656 | // --- Rust impl block extraction --- |
| 8657 | |
| 8658 | static void extract_rust_impl(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec) { |
| 8659 | CBMArena *a = ctx->arena; |
| 8660 | |
| 8661 | TSNode type_node = ts_node_child_by_field_name(node, TS_FIELD("type")); |
| 8662 | if (ts_node_is_null(type_node)) { |
| 8663 | return; |
| 8664 | } |
| 8665 | |
| 8666 | char *type_name = cbm_node_text(a, type_node, ctx->source); |
| 8667 | if (!type_name || !type_name[0]) { |
| 8668 | return; |
| 8669 | } |
| 8670 | /* Strip generic args from the implementing type: `Buffer<T>` → `Buffer`, |
| 8671 | * `Wrapper<T>` → `Wrapper`. The struct identity is the base name. */ |
| 8672 | { |
| 8673 | char *lt = strchr(type_name, '<'); |
| 8674 | if (lt) { |
| 8675 | *lt = '\0'; |
| 8676 | } |
| 8677 | } |
| 8678 | |
| 8679 | const char *type_qn = cbm_fqn_compute(a, ctx->project, ctx->rel_path, type_name); |
| 8680 | |
| 8681 | // Check for "impl Trait for Struct" pattern |
| 8682 | const char *impl_trait = NULL; |
| 8683 | TSNode trait_node = ts_node_child_by_field_name(node, TS_FIELD("trait")); |
| 8684 | if (!ts_node_is_null(trait_node)) { |
| 8685 | char *trait_name = cbm_node_text(a, trait_node, ctx->source); |
| 8686 | /* Strip generic args from the trait: `From<Feet>` → `From`, |
| 8687 | * `Index<usize>` → `Index`, `AsRef<str>` → `AsRef`. Qualified paths |
| 8688 | * like `io::Write` / `fmt::Display` have no `<` and are preserved. */ |
| 8689 | if (trait_name) { |
| 8690 | char *lt = strchr(trait_name, '<'); |
| 8691 | if (lt) { |
| 8692 | *lt = '\0'; |
| 8693 | } |
| 8694 | } |
| 8695 | if (trait_name && trait_name[0]) { |
| 8696 | CBMImplTrait it = {0}; |
| 8697 | it.trait_name = trait_name; |
| 8698 | it.struct_name = type_name; |
| 8699 | it.struct_qn = type_qn; |
| 8700 | cbm_impltrait_push(&ctx->result->impl_traits, a, it); |
| 8701 | impl_trait = trait_name; |
| 8702 | } |
| 8703 | } |
| 8704 | |
| 8705 | // Extract methods inside impl body |
| 8706 | TSNode body = ts_node_child_by_field_name(node, TS_FIELD("body")); |
| 8707 | if (ts_node_is_null(body)) { |
| 8708 | return; |
| 8709 | } |
| 8710 | |
| 8711 | uint32_t count = ts_node_child_count(body); |
| 8712 | for (uint32_t i = 0; i < count; i++) { |
| 8713 | TSNode child = ts_node_child(body, i); |
| 8714 | if (ts_node_is_null(child)) { |
| 8715 | continue; |
no test coverage detected