use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Thresholds {
#[serde(default = "default_max_lines")]
pub max_lines: usize,
#[serde(default = "default_max_depth")]
pub max_depth: usize,
#[serde(default = "default_max_imports")]
pub max_imports: usize,
#[serde(default = "default_max_repetition")]
pub max_repetition: f64,
#[serde(default = "default_min_duplicate_lines")]
pub min_duplicate_lines: usize,
}
#[must_use]
pub const fn default_max_lines() -> usize {
250
}
#[must_use]
pub const fn default_max_depth() -> usize {
5
}
#[must_use]
pub const fn default_max_imports() -> usize {
20
}
#[must_use]
pub const fn default_max_repetition() -> f64 {
10.0
}
#[must_use]
pub const fn default_min_duplicate_lines() -> usize {
4
}
impl Default for Thresholds {
fn default() -> Self {
Self {
max_lines: 250,
max_depth: 5,
max_imports: 20,
max_repetition: 10.0,
min_duplicate_lines: 4,
}
}
}
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct ThresholdsConfig {
#[serde(default)]
pub global: Thresholds,
#[serde(default)]
pub overrides: ThresholdsOverrides,
}
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct ThresholdsOverrides {
pub rs: Option<PartialThresholds>,
pub py: Option<PartialThresholds>,
pub js: Option<PartialThresholds>,
pub ts: Option<PartialThresholds>,
pub java: Option<PartialThresholds>,
pub cs: Option<PartialThresholds>,
#[serde(flatten)]
pub custom: HashMap<String, PartialThresholds>,
}
impl ThresholdsOverrides {
#[must_use]
pub fn get(&self, ext: &str) -> Option<&PartialThresholds> {
match ext {
"rs" => self.rs.as_ref(),
"py" => self.py.as_ref(),
"js" | "mjs" | "cjs" => self.js.as_ref(),
"ts" | "tsx" => self.ts.as_ref(),
"java" => self.java.as_ref(),
"cs" => self.cs.as_ref(),
_ => self.custom.get(ext),
}
}
pub fn extend(&mut self, other: Self) {
if other.rs.is_some() {
self.rs = other.rs;
}
if other.py.is_some() {
self.py = other.py;
}
if other.js.is_some() {
self.js = other.js;
}
if other.ts.is_some() {
self.ts = other.ts;
}
if other.java.is_some() {
self.java = other.java;
}
if other.cs.is_some() {
self.cs = other.cs;
}
for (ext, partial) in other.custom {
self.custom.insert(ext, partial);
}
}
}
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct PartialThresholds {
pub max_lines: Option<usize>,
pub max_depth: Option<usize>,
pub max_imports: Option<usize>,
pub max_repetition: Option<f64>,
pub min_duplicate_lines: Option<usize>,
}