Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 32 additions & 26 deletions crates/core/src/baseline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{cmp::Reverse, collections::BTreeMap};

use serde::Serialize;

use crate::FunctionMetrics;
use crate::{FunctionMetrics, Metric};

const ANONYMOUS: &str = "<anonymous>";

Expand Down Expand Up @@ -64,30 +64,33 @@ pub struct MetricValues {
}

impl MetricValues {
pub fn get(&self, metric: &str) -> Option<usize> {
Some(match metric {
"complexity" => self.complexity,
"cognitive" => self.cognitive,
"depth" => self.depth,
"lines" => self.lines,
"params" => self.params,
"bool_ops" => self.bool_ops,
"widget_depth" => self.widget_depth,
_ => return None,
})
pub fn get(&self, name: &str) -> Option<usize> {
Metric::parse(name).map(|metric| self.value(metric))
}

pub fn value(&self, metric: Metric) -> usize {
match metric {
Metric::Complexity => self.complexity,
Metric::Cognitive => self.cognitive,
Metric::Depth => self.depth,
Metric::Lines => self.lines,
Metric::Params => self.params,
Metric::BoolOps => self.bool_ops,
Metric::WidgetDepth => self.widget_depth,
}
}
}

impl From<&FunctionMetrics> for MetricValues {
fn from(unit: &FunctionMetrics) -> Self {
Self {
complexity: unit.complexity,
cognitive: unit.cognitive,
depth: unit.depth,
lines: unit.lines,
params: unit.params,
bool_ops: unit.bool_ops,
widget_depth: unit.widget_depth,
complexity: unit.value(Metric::Complexity),
cognitive: unit.value(Metric::Cognitive),
depth: unit.value(Metric::Depth),
lines: unit.value(Metric::Lines),
params: unit.value(Metric::Params),
bool_ops: unit.value(Metric::BoolOps),
widget_depth: unit.value(Metric::WidgetDepth),
}
}
}
Expand Down Expand Up @@ -127,11 +130,11 @@ pub fn pair_units(current: &[FunctionMetrics], base: &[FunctionMetrics]) -> Vec<
pub fn status(
base: Option<&FunctionMetrics>,
pairing: Pairing,
metric: &str,
metric: Metric,
value: usize,
) -> Status {
let base_value = match (pairing, base) {
(Pairing::Paired(_), Some(unit)) => MetricValues::from(unit).get(metric),
(Pairing::Paired(_), Some(unit)) => Some(unit.value(metric)),
(Pairing::Unmatched, _) => return Status::Unmatched,
_ => None,
};
Expand Down Expand Up @@ -290,20 +293,23 @@ mod tests {
let base = unit("f", (0, 10), 16);
let paired = Pairing::Paired(0);
assert_eq!(
status(Some(&base), paired, "cognitive", 18),
status(Some(&base), paired, Metric::Cognitive, 18),
Status::Worsened
);
assert_eq!(
status(Some(&base), paired, "cognitive", 16),
status(Some(&base), paired, Metric::Cognitive, 16),
Status::Unchanged
);
assert_eq!(
status(Some(&base), paired, "cognitive", 15),
status(Some(&base), paired, Metric::Cognitive, 15),
Status::Improved
);
assert_eq!(status(None, Pairing::New, "cognitive", 18), Status::New);
assert_eq!(
status(None, Pairing::Unmatched, "lines", 3),
status(None, Pairing::New, Metric::Cognitive, 18),
Status::New
);
assert_eq!(
status(None, Pairing::Unmatched, Metric::Lines, 3),
Status::Unmatched
);
assert_eq!(Status::parse("worsened"), Some(Status::Worsened));
Expand Down
69 changes: 50 additions & 19 deletions crates/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@ use globset::{Glob, GlobSet, GlobSetBuilder};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

use crate::Metric;

const DEFAULTS: &str = include_str!("../../../config.default.json");
const LIMIT_KEYS: &[&str] = &[
"complexity",
"cognitive",
"depth",
"lines",
"params",
"bool_ops",
"widget_depth",
];

/// A `None` limit is `null` in config: the metric is measured but never fails.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
Expand All @@ -33,6 +26,32 @@ pub struct Limits {
pub widget_depth: Option<usize>,
}

impl Limits {
pub fn get(&self, metric: Metric) -> Option<usize> {
match metric {
Metric::Complexity => self.complexity,
Metric::Cognitive => self.cognitive,
Metric::Depth => self.depth,
Metric::Lines => self.lines,
Metric::Params => self.params,
Metric::BoolOps => self.bool_ops,
Metric::WidgetDepth => self.widget_depth,
}
}

pub fn get_mut(&mut self, metric: Metric) -> &mut Option<usize> {
match metric {
Metric::Complexity => &mut self.complexity,
Metric::Cognitive => &mut self.cognitive,
Metric::Depth => &mut self.depth,
Metric::Lines => &mut self.lines,
Metric::Params => &mut self.params,
Metric::BoolOps => &mut self.bool_ops,
Metric::WidgetDepth => &mut self.widget_depth,
}
}
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct TestsConfig {
Expand Down Expand Up @@ -96,6 +115,20 @@ pub struct LimitOverrides {
pub widget_depth: Option<Option<usize>>,
}

impl LimitOverrides {
pub fn get(&self, metric: Metric) -> Option<Option<usize>> {
match metric {
Metric::Complexity => self.complexity,
Metric::Cognitive => self.cognitive,
Metric::Depth => self.depth,
Metric::Lines => self.lines,
Metric::Params => self.params,
Metric::BoolOps => self.bool_ops,
Metric::WidgetDepth => self.widget_depth,
}
}
}

fn explicit<'de, D>(deserializer: D) -> Result<Option<Option<usize>>, D::Error>
where
D: serde::Deserializer<'de>,
Expand Down Expand Up @@ -199,7 +232,7 @@ fn validate_keys(value: &Value, path: &Path) -> Result<()> {
"",
path,
)?;
nested_keys(object, "limits", LIMIT_KEYS, path)?;
nested_keys(object, "limits", &Metric::ALL.map(Metric::name), path)?;
nested_keys(object, "tests", &["patterns", "exempt"], path)?;
validate_test_exempt(object, path)?;
nested_keys(object, "hook", &["max_blocks"], path)?;
Expand Down Expand Up @@ -249,7 +282,7 @@ fn validate_language_keys(root: &Map<String, Value>, path: &Path) -> Result<()>
.as_object()
.ok_or_else(|| anyhow::anyhow!("languages.{name} must be an object"))?;
allowed(object, &["limits"], &format!("languages.{name}"), path)?;
nested_keys(object, "limits", LIMIT_KEYS, path)?;
nested_keys(object, "limits", &Metric::ALL.map(Metric::name), path)?;
}
Ok(())
}
Expand Down Expand Up @@ -295,15 +328,13 @@ impl Config {
else {
return self.limits.clone();
};
Limits {
complexity: overrides.complexity.unwrap_or(self.limits.complexity),
cognitive: overrides.cognitive.unwrap_or(self.limits.cognitive),
depth: overrides.depth.unwrap_or(self.limits.depth),
lines: overrides.lines.unwrap_or(self.limits.lines),
params: overrides.params.unwrap_or(self.limits.params),
bool_ops: overrides.bool_ops.unwrap_or(self.limits.bool_ops),
widget_depth: overrides.widget_depth.unwrap_or(self.limits.widget_depth),
let mut limits = self.limits.clone();
for metric in Metric::ALL {
if let Some(limit) = overrides.get(metric) {
*limits.get_mut(metric) = limit;
}
}
limits
}

pub fn matcher(patterns: &[String]) -> Result<GlobSet> {
Expand Down
2 changes: 2 additions & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod cognitive;
pub mod config;
pub mod diff;
pub mod language;
mod metric;
pub mod scan;

pub use baseline::{MetricValues, Pairing, Status, pair_units};
Expand All @@ -14,4 +15,5 @@ pub use diff::{
parse_diff_hunks,
};
pub use language::{FunctionMetrics, Language, coverage_unknowns, grammar_inventory, parse_source};
pub use metric::Metric;
pub use scan::{Baseline, ScanOptions, ScanResult, Unverified, Violation, scan};
Loading
Loading