cargo-coupling 0.3.5

A coupling analysis tool for Rust projects - measuring the 'right distance' in your code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! External dependency coupling analysis.
//!
//! This module aggregates `DifferentCrate` coupling records into a snapshot of
//! how broadly third-party crates are used across internal modules.

use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::fs;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::balance::action::RefactoringAction;
use crate::balance::issue::CouplingIssue;
use crate::balance::issue_type::IssueType;
use crate::balance::severity::Severity;
use crate::metrics::coupling::CouplingMetrics;
use crate::metrics::dimensions::{Distance, IntegrationStrength};
use crate::metrics::project::ProjectMetrics;

/// Number of internal modules above which direct third-party usage is considered scattered.
pub const SCATTERED_EXTERNAL_BREADTH_THRESHOLD: usize = 3;

/// External dependency coupling report.
#[derive(Debug, Clone)]
pub struct ExternalDependencyReport {
    /// External crates sorted by breadth, then references.
    pub dependencies: Vec<ExternalDependencyUsage>,
    /// Scattered external coupling findings.
    pub scattered_couplings: Vec<CouplingIssue>,
}

/// Aggregated usage of one external crate.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ExternalDependencyUsage {
    /// Crate name.
    pub crate_name: String,
    /// Resolved versions from Cargo.lock, when available.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub versions: Vec<String>,
    /// Number of distinct internal modules with direct coupling to this crate.
    pub breadth: usize,
    /// Total direct references observed in coupling metrics.
    pub total_references: usize,
    /// Most common integration strength, breaking ties toward stronger coupling.
    pub dominant_strength: String,
    /// Internal modules with direct coupling to this crate.
    pub source_modules: Vec<String>,
}

#[derive(Debug, Default)]
struct ExternalAccumulator {
    source_modules: BTreeSet<String>,
    total_references: usize,
    strength_counts: [usize; 4],
}

impl ExternalAccumulator {
    fn add(&mut self, coupling: &CouplingMetrics) {
        self.source_modules.insert(coupling.source.clone());
        self.total_references += 1;
        self.strength_counts[strength_index(coupling.strength)] += 1;
    }

    fn dominant_strength(&self) -> IntegrationStrength {
        let (idx, _) = self
            .strength_counts
            .iter()
            .enumerate()
            .max_by(|(left_idx, left_count), (right_idx, right_count)| {
                left_count
                    .cmp(right_count)
                    .then_with(|| strength_rank(*left_idx).cmp(&strength_rank(*right_idx)))
            })
            .unwrap_or((0, &0));
        strength_from_index(idx)
    }
}

/// Analyze external dependency coupling using optional lockfile versions.
pub fn analyze_external_dependencies(
    metrics: &ProjectMetrics,
    versions: &HashMap<String, Vec<String>>,
) -> ExternalDependencyReport {
    let known_external_crates = known_external_crates(metrics, versions);
    let mut by_crate: BTreeMap<String, ExternalAccumulator> = BTreeMap::new();

    for coupling in metrics
        .couplings
        .iter()
        .filter(|coupling| coupling.distance == Distance::DifferentCrate)
    {
        let crate_name = external_crate_name(coupling);
        if !known_external_crates.is_empty()
            && !known_external_crates.contains(&normalize_crate_name(&crate_name))
        {
            continue;
        }
        by_crate.entry(crate_name).or_default().add(coupling);
    }

    let mut dependencies: Vec<_> = by_crate
        .into_iter()
        .map(|(crate_name, accumulator)| {
            let dominant_strength = accumulator.dominant_strength();
            let source_modules: Vec<_> = accumulator.source_modules.into_iter().collect();
            let mut crate_versions = versions.get(&crate_name).cloned().unwrap_or_default();
            crate_versions.sort();
            crate_versions.dedup();

            ExternalDependencyUsage {
                crate_name,
                versions: crate_versions,
                breadth: source_modules.len(),
                total_references: accumulator.total_references,
                dominant_strength: strength_label(dominant_strength).to_string(),
                source_modules,
            }
        })
        .collect();

    dependencies.sort_by(|left, right| {
        right
            .breadth
            .cmp(&left.breadth)
            .then_with(|| right.total_references.cmp(&left.total_references))
            .then_with(|| left.crate_name.cmp(&right.crate_name))
    });

    let scattered_couplings = detect_scattered_external_coupling(&dependencies);

    ExternalDependencyReport {
        dependencies,
        scattered_couplings,
    }
}

fn known_external_crates(
    metrics: &ProjectMetrics,
    versions: &HashMap<String, Vec<String>>,
) -> BTreeSet<String> {
    let mut crates: BTreeSet<String> = metrics
        .crate_dependencies
        .values()
        .flatten()
        .map(|name| normalize_crate_name(name))
        .collect();

    if crates.is_empty() {
        crates.extend(versions.keys().map(|name| normalize_crate_name(name)));
    }

    crates
}

/// Detect external crates used directly from many internal modules.
pub fn detect_scattered_external_coupling(
    dependencies: &[ExternalDependencyUsage],
) -> Vec<CouplingIssue> {
    dependencies
        .iter()
        .filter(|dependency| dependency.breadth > SCATTERED_EXTERNAL_BREADTH_THRESHOLD)
        .map(|dependency| {
            let severity = scattered_severity(dependency.breadth);
            CouplingIssue {
                issue_type: IssueType::ScatteredExternalCoupling,
                severity,
                source: format!("{} internal modules", dependency.breadth),
                target: dependency.crate_name.clone(),
                description: format!(
                    "{} is directly used from {} internal modules ({} references). Third-party upgrade risk is spread across the codebase.",
                    dependency.crate_name, dependency.breadth, dependency.total_references
                ),
                refactoring: RefactoringAction::General {
                    action: format!(
                        "Introduce a `{}` facade/wrapper module and route direct crate usage through it",
                        facade_module_name(&dependency.crate_name)
                    ),
                },
                balance_score: 1.0 - (dependency.breadth as f64 / 12.0).min(1.0),
            }
        })
        .collect()
}

/// Read resolved package versions from the nearest Cargo.lock. Missing or
/// unparsable lockfiles are treated as absent.
pub fn load_lock_versions_near(start: &Path) -> HashMap<String, Vec<String>> {
    let Some(lock_path) = find_cargo_lock(start) else {
        return HashMap::new();
    };
    let Ok(content) = fs::read_to_string(lock_path) else {
        return HashMap::new();
    };
    parse_lock_versions(&content).unwrap_or_default()
}

fn parse_lock_versions(content: &str) -> Option<HashMap<String, Vec<String>>> {
    let lock: CargoLock = toml::from_str(content).ok()?;
    let mut versions: HashMap<String, Vec<String>> = HashMap::new();

    for package in lock.package {
        versions
            .entry(package.name)
            .or_default()
            .push(package.version);
    }

    for package_versions in versions.values_mut() {
        package_versions.sort();
        package_versions.dedup();
    }

    Some(versions)
}

#[derive(Debug, Deserialize)]
struct CargoLock {
    package: Vec<CargoLockPackage>,
}

#[derive(Debug, Deserialize)]
struct CargoLockPackage {
    name: String,
    version: String,
}

fn find_cargo_lock(start: &Path) -> Option<PathBuf> {
    let mut current = if start.is_file() {
        start.parent().map(Path::to_path_buf)
    } else {
        Some(start.to_path_buf())
    };

    while let Some(dir) = current {
        let lock_path = dir.join("Cargo.lock");
        if lock_path.exists() {
            return Some(lock_path);
        }
        current = dir.parent().map(Path::to_path_buf);
    }

    None
}

fn external_crate_name(coupling: &CouplingMetrics) -> String {
    coupling.target_crate.clone().unwrap_or_else(|| {
        coupling
            .target
            .split("::")
            .next()
            .unwrap_or(&coupling.target)
            .to_string()
    })
}

fn normalize_crate_name(crate_name: &str) -> String {
    crate_name.replace('-', "_")
}

fn scattered_severity(breadth: usize) -> Severity {
    if breadth >= 10 {
        Severity::Critical
    } else if breadth >= 6 {
        Severity::High
    } else {
        Severity::Medium
    }
}

fn facade_module_name(crate_name: &str) -> String {
    format!("{}_facade", crate_name.replace('-', "_"))
}

fn strength_index(strength: IntegrationStrength) -> usize {
    match strength {
        IntegrationStrength::Contract => 0,
        IntegrationStrength::Model => 1,
        IntegrationStrength::Functional => 2,
        IntegrationStrength::Intrusive => 3,
    }
}

fn strength_rank(index: usize) -> usize {
    index
}

fn strength_from_index(index: usize) -> IntegrationStrength {
    match index {
        0 => IntegrationStrength::Contract,
        1 => IntegrationStrength::Model,
        2 => IntegrationStrength::Functional,
        _ => IntegrationStrength::Intrusive,
    }
}

fn strength_label(strength: IntegrationStrength) -> &'static str {
    match strength {
        IntegrationStrength::Intrusive => "Intrusive",
        IntegrationStrength::Functional => "Functional",
        IntegrationStrength::Model => "Model",
        IntegrationStrength::Contract => "Contract",
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::metrics::coupling::CouplingMetrics;
    use crate::volatility::Volatility;

    fn external_coupling(
        source: &str,
        target: &str,
        strength: IntegrationStrength,
    ) -> CouplingMetrics {
        let mut coupling = CouplingMetrics::new(
            source.to_string(),
            format!("{target}::Item"),
            strength,
            Distance::DifferentCrate,
            Volatility::Low,
        );
        coupling.target_crate = Some(target.to_string());
        coupling
    }

    #[test]
    fn aggregation_counts_breadth_per_external_crate() {
        let mut metrics = ProjectMetrics::new();
        metrics.add_coupling(external_coupling(
            "app::a",
            "serde",
            IntegrationStrength::Model,
        ));
        metrics.add_coupling(external_coupling(
            "app::a",
            "serde",
            IntegrationStrength::Functional,
        ));
        metrics.add_coupling(external_coupling(
            "app::b",
            "serde",
            IntegrationStrength::Functional,
        ));
        metrics.add_coupling(external_coupling(
            "app::c",
            "clap",
            IntegrationStrength::Model,
        ));
        metrics.add_coupling(CouplingMetrics::new(
            "app::a".to_string(),
            "app::internal".to_string(),
            IntegrationStrength::Model,
            Distance::DifferentModule,
            Volatility::Low,
        ));

        let mut versions = HashMap::new();
        versions.insert("serde".to_string(), vec!["1.0.0".to_string()]);
        let report = analyze_external_dependencies(&metrics, &versions);

        let serde = report
            .dependencies
            .iter()
            .find(|dependency| dependency.crate_name == "serde")
            .unwrap();
        assert_eq!(serde.breadth, 2);
        assert_eq!(serde.total_references, 3);
        assert_eq!(serde.dominant_strength, "Functional");
        assert_eq!(serde.versions, vec!["1.0.0"]);
        assert_eq!(serde.source_modules, vec!["app::a", "app::b"]);
    }

    #[test]
    fn aggregation_filters_unresolved_type_names_when_dependency_inventory_exists() {
        let mut metrics = ProjectMetrics::new();
        metrics
            .crate_dependencies
            .insert("app".to_string(), vec!["serde".to_string()]);
        metrics.add_coupling(external_coupling(
            "app::a",
            "serde",
            IntegrationStrength::Model,
        ));
        metrics.add_coupling(external_coupling(
            "app::b",
            "Vec",
            IntegrationStrength::Functional,
        ));

        let report = analyze_external_dependencies(&metrics, &HashMap::new());

        assert_eq!(report.dependencies.len(), 1);
        assert_eq!(report.dependencies[0].crate_name, "serde");
    }

    #[test]
    fn scattered_coupling_is_flagged_above_threshold() {
        let dependency = ExternalDependencyUsage {
            crate_name: "reqwest".to_string(),
            versions: vec![],
            breadth: 4,
            total_references: 9,
            dominant_strength: "Functional".to_string(),
            source_modules: vec![
                "a".to_string(),
                "b".to_string(),
                "c".to_string(),
                "d".to_string(),
            ],
        };

        let issues = detect_scattered_external_coupling(&[dependency]);

        assert_eq!(issues.len(), 1);
        assert_eq!(issues[0].issue_type, IssueType::ScatteredExternalCoupling);
        assert_eq!(issues[0].severity, Severity::Medium);
        assert_eq!(issues[0].target, "reqwest");
        assert!(format!("{}", issues[0].refactoring).contains("facade"));
    }

    #[test]
    fn cargo_lock_versions_are_parsed() {
        let content = r#"
[[package]]
name = "serde"
version = "1.0.228"

[[package]]
name = "serde"
version = "1.0.229"

[[package]]
name = "clap"
version = "4.6.0"
"#;

        let versions = parse_lock_versions(content).unwrap();

        assert_eq!(
            versions.get("serde").unwrap(),
            &vec!["1.0.228".to_string(), "1.0.229".to_string()]
        );
        assert_eq!(versions.get("clap").unwrap(), &vec!["4.6.0".to_string()]);
    }
}