pub const INLINE_THRESHOLD_MARKER: &str = "fallow-override-threshold-next-line";
pub const INLINE_THRESHOLD_KEYS: [&str; 4] =
["maxCyclomatic", "maxCognitive", "maxCrap", "maxUnitSize"];
#[derive(Debug, Clone, PartialEq)]
pub struct InlineThresholdOverride {
pub line: u32,
pub comment_line: u32,
pub max_cyclomatic: Option<u16>,
pub max_cognitive: Option<u16>,
pub max_crap: Option<f64>,
pub max_unit_size: Option<u32>,
pub reason: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum InlineThresholdProblemKind {
MissingReason,
MissingThreshold,
InvalidKey,
InvalidValue,
Disabled,
}
impl InlineThresholdProblemKind {
#[must_use]
pub const fn to_code(self) -> u8 {
match self {
Self::MissingReason => 1,
Self::MissingThreshold => 2,
Self::InvalidKey => 3,
Self::InvalidValue => 4,
Self::Disabled => 5,
}
}
#[must_use]
pub const fn from_code(code: u8) -> Option<Self> {
match code {
1 => Some(Self::MissingReason),
2 => Some(Self::MissingThreshold),
3 => Some(Self::InvalidKey),
4 => Some(Self::InvalidValue),
5 => Some(Self::Disabled),
_ => None,
}
}
#[must_use]
pub const fn description(self) -> &'static str {
match self {
Self::MissingReason => {
"the comment has no reason; add ` -- <reason>` after the thresholds"
}
Self::MissingThreshold => {
"the comment sets no threshold; use maxCyclomatic, maxCognitive, maxCrap or maxUnitSize"
}
Self::InvalidKey => {
"unknown or repeated key; write each threshold as key=value (maxCyclomatic, maxCognitive, maxCrap or maxUnitSize), each one time"
}
Self::InvalidValue => {
"the value is not valid; use a whole number (a finite number for maxCrap), as in maxCognitive=40"
}
Self::Disabled => {
"health.inlineThresholdOverrides is false, so the comment has no effect"
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct InlineThresholdProblem {
pub line: u32,
#[serde(rename = "problem")]
pub kind: InlineThresholdProblemKind,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub token: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct InlineThresholdOverrides {
pub entries: Vec<InlineThresholdOverride>,
pub problems: Vec<InlineThresholdProblem>,
}
impl InlineThresholdOverrides {
#[must_use]
pub fn is_empty(&self) -> bool {
self.entries.is_empty() && self.problems.is_empty()
}
pub fn drop_comment_lines(&mut self, comment_lines: &[u32]) {
self.entries
.retain(|entry| !comment_lines.contains(&entry.comment_line));
self.problems
.retain(|problem| !comment_lines.contains(&problem.line));
}
}
pub fn parse_inline_threshold_override(
line: u32,
comment_line: u32,
targets: &str,
reason: Option<String>,
) -> Result<InlineThresholdOverride, InlineThresholdProblem> {
let problem = |kind, token: Option<&str>| InlineThresholdProblem {
line: comment_line,
kind,
token: token.map(str::to_string),
};
let mut entry = InlineThresholdOverride {
line,
comment_line,
max_cyclomatic: None,
max_cognitive: None,
max_crap: None,
max_unit_size: None,
reason: String::new(),
};
let mut seen: Vec<&str> = Vec::new();
for token in targets
.split(|c: char| c == ',' || c.is_whitespace())
.filter(|token| !token.is_empty())
{
let Some((key, value)) = token.split_once('=') else {
return Err(problem(InlineThresholdProblemKind::InvalidKey, Some(token)));
};
if !INLINE_THRESHOLD_KEYS.contains(&key) || seen.contains(&key) {
return Err(problem(InlineThresholdProblemKind::InvalidKey, Some(token)));
}
seen.push(key);
let invalid = || problem(InlineThresholdProblemKind::InvalidValue, Some(token));
match key {
"maxCyclomatic" => entry.max_cyclomatic = Some(value.parse().map_err(|_| invalid())?),
"maxCognitive" => entry.max_cognitive = Some(value.parse().map_err(|_| invalid())?),
"maxUnitSize" => entry.max_unit_size = Some(value.parse().map_err(|_| invalid())?),
_ => {
let crap: f64 = value.parse().map_err(|_| invalid())?;
if !crap.is_finite() || crap < 0.0 {
return Err(invalid());
}
entry.max_crap = Some(crap);
}
}
}
if seen.is_empty() {
return Err(problem(InlineThresholdProblemKind::MissingThreshold, None));
}
let Some(reason) = reason else {
return Err(problem(InlineThresholdProblemKind::MissingReason, None));
};
entry.reason = reason;
Ok(entry)
}
#[cfg(test)]
mod tests {
use super::*;
fn parse(
targets: &str,
reason: Option<&str>,
) -> Result<InlineThresholdOverride, InlineThresholdProblem> {
parse_inline_threshold_override(5, 4, targets, reason.map(str::to_string))
}
#[test]
fn parses_every_key() {
let entry = parse(
"maxCyclomatic=30 maxCognitive=40, maxCrap=45.5 maxUnitSize=120",
Some("legacy"),
)
.unwrap();
assert_eq!(entry.line, 5);
assert_eq!(entry.comment_line, 4);
assert_eq!(entry.max_cyclomatic, Some(30));
assert_eq!(entry.max_cognitive, Some(40));
assert_eq!(entry.max_crap, Some(45.5));
assert_eq!(entry.max_unit_size, Some(120));
assert_eq!(entry.reason, "legacy");
}
#[test]
fn rejects_a_comment_without_reason() {
let problem = parse("maxCognitive=40", None).unwrap_err();
assert_eq!(problem.kind, InlineThresholdProblemKind::MissingReason);
assert_eq!(problem.line, 4);
}
#[test]
fn rejects_a_comment_without_threshold() {
let problem = parse("", Some("why")).unwrap_err();
assert_eq!(problem.kind, InlineThresholdProblemKind::MissingThreshold);
}
#[test]
fn rejects_bad_keys_and_values() {
for (targets, kind, token) in [
(
"maxCognitive:40",
InlineThresholdProblemKind::InvalidKey,
"maxCognitive:40",
),
(
"maxFileLines=900",
InlineThresholdProblemKind::InvalidKey,
"maxFileLines=900",
),
(
"maxCognitive=40 maxCognitive=50",
InlineThresholdProblemKind::InvalidKey,
"maxCognitive=50",
),
(
"maxCognitive=forty",
InlineThresholdProblemKind::InvalidValue,
"maxCognitive=forty",
),
(
"maxCyclomatic=-1",
InlineThresholdProblemKind::InvalidValue,
"maxCyclomatic=-1",
),
(
"maxCrap=NaN",
InlineThresholdProblemKind::InvalidValue,
"maxCrap=NaN",
),
(
"maxCrap=-2",
InlineThresholdProblemKind::InvalidValue,
"maxCrap=-2",
),
] {
let problem = parse(targets, Some("why")).unwrap_err();
assert_eq!(problem.kind, kind, "{targets}");
assert_eq!(problem.token.as_deref(), Some(token), "{targets}");
}
}
#[test]
fn problem_codes_round_trip() {
for kind in [
InlineThresholdProblemKind::MissingReason,
InlineThresholdProblemKind::MissingThreshold,
InlineThresholdProblemKind::InvalidKey,
InlineThresholdProblemKind::InvalidValue,
InlineThresholdProblemKind::Disabled,
] {
assert_eq!(
InlineThresholdProblemKind::from_code(kind.to_code()),
Some(kind)
);
}
assert_eq!(InlineThresholdProblemKind::from_code(0), None);
}
}