#[derive(Debug, Deserialize)]
struct ComplexityArgs {
paths: Vec<String>,
#[serde(default)]
top_files: Option<usize>,
#[serde(default)]
threshold: Option<u64>,
}
pub struct ComplexityTool;
impl ComplexityTool {
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn new() -> Self {
Self
}
}
impl Default for ComplexityTool {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl ToolHandler for ComplexityTool {
async fn handle(&self, args: Value, _extra: RequestHandlerExtra) -> Result<Value> {
debug!("Handling analyze.complexity with args: {}", args);
let params: ComplexityArgs = serde_json::from_value(args)
.map_err(|e| Error::validation(format!("Invalid arguments: {e}")))?;
let paths: Vec<PathBuf> = params.paths.into_iter().map(PathBuf::from).collect();
let results =
tool_functions::analyze_complexity(&paths, params.top_files, params.threshold)
.await
.map_err(|e| Error::internal(format!("Complexity analysis failed: {e}")))?;
Ok(results)
}
fn metadata(&self) -> Option<ToolInfo> {
let extra = json!({
"top_files": { "type": "integer", "description": "Return only the top N most-complex files" },
"threshold": { "type": "integer", "description": "Minimum cyclomatic complexity to report" }
});
Some(build_tool_info(
"analyze_complexity",
"Analyze cyclomatic and cognitive complexity for source files.",
paths_object_schema(extra, vec!["paths"]),
))
}
}