cargo_coupling/metrics/
dimensions.rs1use std::fmt;
2use std::path::Path;
3
4use crate::volatility::Volatility;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
7pub enum Visibility {
8 Public,
10 PubCrate,
12 PubSuper,
14 PubIn,
16 #[default]
18 Private,
19}
20
21impl Visibility {
22 pub fn allows_external_access(&self) -> bool {
24 matches!(self, Visibility::Public | Visibility::PubCrate)
25 }
26
27 pub fn is_intrusive_from(&self, same_crate: bool, same_module: bool) -> bool {
32 if same_module {
33 return false;
35 }
36
37 match self {
38 Visibility::Public => false, Visibility::PubCrate => !same_crate, Visibility::PubSuper | Visibility::PubIn => true, Visibility::Private => true, }
43 }
44
45 pub fn intrusive_penalty(&self) -> f64 {
49 match self {
50 Visibility::Public => 0.0, Visibility::PubCrate => 0.25, Visibility::PubSuper => 0.5, Visibility::PubIn => 0.5, Visibility::Private => 1.0, }
56 }
57}
58
59impl fmt::Display for Visibility {
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61 match self {
62 Visibility::Public => write!(f, "pub"),
63 Visibility::PubCrate => write!(f, "pub(crate)"),
64 Visibility::PubSuper => write!(f, "pub(super)"),
65 Visibility::PubIn => write!(f, "pub(in ...)"),
66 Visibility::Private => write!(f, "private"),
67 }
68 }
69}
70
71#[derive(Debug, Clone, Copy, PartialEq)]
73pub enum IntegrationStrength {
74 Intrusive,
76 Functional,
78 Model,
80 Contract,
82}
83
84impl IntegrationStrength {
85 pub fn value(&self) -> f64 {
87 match self {
88 IntegrationStrength::Intrusive => 1.0,
89 IntegrationStrength::Functional => 0.75,
90 IntegrationStrength::Model => 0.5,
91 IntegrationStrength::Contract => 0.25,
92 }
93 }
94}
95
96#[derive(Debug, Clone, Copy, PartialEq)]
98pub enum Distance {
99 SameFunction,
101 SameModule,
103 DifferentModule,
105 DifferentCrate,
107}
108
109impl Distance {
110 pub fn value(&self) -> f64 {
112 match self {
113 Distance::SameFunction => 0.0,
114 Distance::SameModule => 0.25,
115 Distance::DifferentModule => 0.5,
116 Distance::DifferentCrate => 1.0,
117 }
118 }
119}
120
121#[derive(Debug, Clone, Copy, PartialEq, Eq)]
123pub enum Subdomain {
124 Core,
126 Supporting,
128 Generic,
130}
131
132impl Subdomain {
133 pub fn expected_volatility(&self) -> Volatility {
135 match self {
136 Subdomain::Core => Volatility::High,
137 Subdomain::Supporting => Volatility::Low,
138 Subdomain::Generic => Volatility::Low,
139 }
140 }
141}
142
143impl fmt::Display for Subdomain {
144 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145 match self {
146 Subdomain::Core => write!(f, "Core"),
147 Subdomain::Supporting => write!(f, "Supporting"),
148 Subdomain::Generic => write!(f, "Generic"),
149 }
150 }
151}
152
153pub trait MetricsConfig {
155 fn config_root(&self) -> Option<&Path>;
157 fn has_volatility_overrides(&self) -> bool;
159 fn has_subdomain_config(&self) -> bool;
161 fn get_subdomain(&self, path: &str) -> Option<Subdomain>;
163 fn get_volatility_override(&mut self, path: &str) -> Option<Volatility>;
165}
166
167