Skip to main content

dupes_core/
code_unit.rs

1use std::path::PathBuf;
2
3use crate::fingerprint::Fingerprint;
4use crate::node::NormalizedNode;
5
6/// The kind of code unit extracted from source.
7#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize)]
8pub enum CodeUnitKind {
9    Function,
10    Method,
11    Closure,
12    ImplBlock,
13    TraitImplBlock,
14    // Sub-function kinds
15    IfBranch,
16    MatchArm,
17    LoopBody,
18    Block,
19}
20
21impl std::fmt::Display for CodeUnitKind {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        match self {
24            Self::Function => write!(f, "function"),
25            Self::Method => write!(f, "method"),
26            Self::Closure => write!(f, "closure"),
27            Self::ImplBlock => write!(f, "impl block"),
28            Self::TraitImplBlock => write!(f, "trait impl block"),
29            Self::IfBranch => write!(f, "if branch"),
30            Self::MatchArm => write!(f, "match arm"),
31            Self::LoopBody => write!(f, "loop body"),
32            Self::Block => write!(f, "block"),
33        }
34    }
35}
36
37/// A unit of code extracted and normalized for duplication analysis.
38#[derive(Debug, Clone)]
39pub struct CodeUnit {
40    pub kind: CodeUnitKind,
41    pub name: String,
42    pub file: PathBuf,
43    pub line_start: usize,
44    pub line_end: usize,
45    pub signature: NormalizedNode,
46    pub body: NormalizedNode,
47    pub fingerprint: Fingerprint,
48    pub node_count: usize,
49    /// For sub-function units, the name of the parent function.
50    pub parent_name: Option<String>,
51    /// Whether this code unit was identified as test code by the language analyzer.
52    pub is_test: bool,
53}