use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FunctionContext {
Utility,
Handler,
Core,
Internal,
Test,
}
impl FunctionContext {
pub const ALL: [FunctionContext; 5] = [
FunctionContext::Utility,
FunctionContext::Handler,
FunctionContext::Core,
FunctionContext::Internal,
FunctionContext::Test,
];
pub fn index(&self) -> usize {
match self {
FunctionContext::Utility => 0,
FunctionContext::Handler => 1,
FunctionContext::Core => 2,
FunctionContext::Internal => 3,
FunctionContext::Test => 4,
}
}
pub fn from_index(i: usize) -> Self {
match i {
0 => FunctionContext::Utility,
1 => FunctionContext::Handler,
2 => FunctionContext::Core,
3 => FunctionContext::Internal,
_ => FunctionContext::Test,
}
}
pub fn skip_coupling(&self) -> bool {
matches!(
self,
FunctionContext::Utility | FunctionContext::Handler | FunctionContext::Test
)
}
pub fn skip_dead_code(&self) -> bool {
matches!(self, FunctionContext::Handler | FunctionContext::Test)
}
#[allow(dead_code)] pub fn coupling_multiplier(&self) -> f64 {
match self {
FunctionContext::Utility => 3.0, FunctionContext::Handler => 2.5, FunctionContext::Core => 1.0, FunctionContext::Internal => 1.5, FunctionContext::Test => 5.0, }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum FileContext {
TestFile, UtilFile, HandlerFile, InternalFile, #[default]
SourceFile, }
impl FileContext {
pub fn from_path(path: &str) -> Self {
let path_lower = path.to_lowercase();
if path_lower.contains("/test")
|| path_lower.contains("_test.")
|| path_lower.contains(".test.")
|| path_lower.contains(".spec.")
|| path_lower.contains("/__tests__")
|| path_lower.contains("/__mocks__")
{
return FileContext::TestFile;
}
if path_lower.contains("/util")
|| path_lower.contains("/utils")
|| path_lower.contains("/helper")
|| path_lower.contains("/helpers")
|| path_lower.contains("/common")
|| path_lower.contains("/shared")
|| path_lower.contains("/lib/")
{
return FileContext::UtilFile;
}
if path_lower.contains("/handler")
|| path_lower.contains("/callback")
|| path_lower.contains("/hook")
|| path_lower.contains("/events")
{
return FileContext::HandlerFile;
}
if path_lower.contains("/internal")
|| path_lower.contains("/private")
|| path_lower.contains("/_")
|| path_lower.contains("/pkg/")
{
return FileContext::InternalFile;
}
FileContext::SourceFile
}
pub fn function_bias(&self) -> Option<FunctionContext> {
match self {
FileContext::TestFile => Some(FunctionContext::Test),
FileContext::UtilFile => None, FileContext::HandlerFile => None,
FileContext::InternalFile => None,
FileContext::SourceFile => None,
}
}
}