cargo-dry4rust 0.2.0

A cargo subcommand that detects duplicate and near-duplicate code blocks in Rust codebases using AST normalization
Documentation
// Copyright (c) 2026 Matjaz Domen Pecan
// Copyright 2026 Umberto Gotti <umberto.gotti@umbertogotti.dev>
// Licensed under the MIT License
// SPDX-License-Identifier: MIT

use std::path::PathBuf;

use crate::fingerprint::Fingerprint;
use crate::node::NormalizedNode;
use std::fmt;

/// The kind of code unit extracted from source.
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize)]
pub enum CodeUnitKind {
    Function,
    Method,
    Closure,
    Class,
    ImplBlock,
    TraitImplBlock,
    // Sub-function kinds
    IfBranch,
    MatchArm,
    LoopBody,
    Block,
}

impl fmt::Display for CodeUnitKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Function => write!(f, "function"),
            Self::Method => write!(f, "method"),
            Self::Closure => write!(f, "closure"),
            Self::Class => write!(f, "class"),
            Self::ImplBlock => write!(f, "impl block"),
            Self::TraitImplBlock => write!(f, "trait impl block"),
            Self::IfBranch => write!(f, "if branch"),
            Self::MatchArm => write!(f, "match arm"),
            Self::LoopBody => write!(f, "loop body"),
            Self::Block => write!(f, "block"),
        }
    }
}

/// A unit of code extracted and normalized for duplication analysis.
#[derive(Debug, Clone)]
pub struct CodeUnit {
    pub kind: CodeUnitKind,
    pub name: String,
    pub file: PathBuf,
    pub line_start: usize,
    pub line_end: usize,
    pub signature: NormalizedNode,
    pub body: NormalizedNode,
    pub fingerprint: Fingerprint,
    pub node_count: usize,
    /// For sub-function units, the name of the parent function.
    pub parent_name: Option<String>,
    /// Whether this code unit was identified as test code by the language analyzer.
    pub is_test: bool,
}