Skip to main content

aurora_autodebug/
engine.rs

1use std::path::Path;
2use aurora_core::{Command, CommandCategory, Pipeline, Engine, AuroraResult, AuroraError};
3use crate::analyzer;
4use crate::suggestions;
5
6pub struct AutoDebugEngine;
7
8impl Engine for AutoDebugEngine {
9    fn name(&self) -> &'static str { "autodebug" }
10
11    fn can_handle(&self, cmd: &Command) -> bool {
12        matches!(cmd.category, CommandCategory::AutoDebug)
13    }
14
15    fn execute(&self, cmd: &Command, _input: &Pipeline) -> AuroraResult<Pipeline> {
16        match cmd.action.as_str() {
17            "analyze" => {
18                let target = cmd.args.first().ok_or_else(|| {
19                    AuroraError::InvalidInput("Specify target to analyze (file, PID)".to_string())
20                })?;
21                analyzer::analyze_target(target)
22            }
23            "log" => {
24                let path = cmd.args.first().map(|s| s.as_str()).unwrap_or(".");
25                let path_str = path.to_string();
26
27                if Path::new(&path_str).is_file() {
28                    analyzer::analyze_log_file(&path_str)
29                } else {
30                    let logs = analyzer::find_log_files(&path_str);
31                    if logs.is_empty() {
32                        Ok(Pipeline::single(
33                            aurora_core::Value::String("No .log files found.".to_string())
34                        ))
35                    } else {
36                        analyzer::analyze_log_file(&logs[0])
37                    }
38                }
39            }
40            "suggest" => {
41                let errors = &cmd.args;
42                if errors.is_empty() {
43                    return Err(AuroraError::InvalidInput(
44                        "Specify error messages to get suggestions".to_string()
45                    ));
46                }
47                suggestions::suggest_fix(errors)
48            }
49            _ => Err(AuroraError::CommandNotFound(
50                format!("autodebug.{}", cmd.action)
51            )),
52        }
53    }
54}