Skip to main content

dry4rust/
extractor.rs

1// Copyright (c) 2026 Matjaz Domen Pecan
2// Copyright 2026 Umberto Gotti <umberto.gotti@umbertogotti.dev>
3// Licensed under the MIT License
4// SPDX-License-Identifier: MIT
5
6use crate::code_unit::CodeUnitKind;
7use crate::node::NormalizedNode;
8use crate::sub_unit_extractor::SubUnitExtractor;
9
10/// A sub-unit extracted from a normalized function body.
11pub struct SubUnit {
12    pub kind: CodeUnitKind,
13    pub node: NormalizedNode,
14    pub node_count: usize,
15    pub description: String,
16}
17
18/// Extract candidate sub-units from a normalized AST node.
19/// Walks the tree recursively and extracts natural compound structures
20/// (if branches, match arm bodies, loop bodies, closure bodies).
21/// Each sub-tree is re-indexed to canonical placeholder form.
22/// Only sub-trees meeting `min_node_count` are returned.
23#[must_use]
24pub fn extract_sub_units(node: &NormalizedNode, min_node_count: usize) -> Vec<SubUnit> {
25    SubUnitExtractor::new(min_node_count).extract(node)
26}