ricecoder_storage/industry/
aider.rs

1//! Aider configuration adapter
2//!
3//! Reads and converts Aider configuration files (.aider.conf.yml)
4//! into RiceCoder's internal configuration format.
5
6use crate::config::{Config, SteeringRule};
7use crate::error::StorageResult;
8use crate::types::DocumentFormat;
9use std::path::Path;
10use tracing::debug;
11
12use super::adapter::IndustryFileAdapter;
13
14/// Aider adapter
15pub struct AiderAdapter;
16
17impl AiderAdapter {
18    /// Create a new Aider adapter
19    pub fn new() -> Self {
20        AiderAdapter
21    }
22
23    /// Read .aider.conf.yml file
24    fn read_aider_config(&self, project_root: &Path) -> StorageResult<Option<String>> {
25        let aider_config_path = project_root.join(".aider.conf.yml");
26
27        if !aider_config_path.exists() {
28            debug!("No .aider.conf.yml file found at {:?}", aider_config_path);
29            return Ok(None);
30        }
31
32        debug!("Reading .aider.conf.yml from {:?}", aider_config_path);
33        let content = std::fs::read_to_string(&aider_config_path).map_err(|e| {
34            crate::error::StorageError::io_error(
35                aider_config_path.clone(),
36                crate::error::IoOperation::Read,
37                e,
38            )
39        })?;
40
41        Ok(Some(content))
42    }
43}
44
45impl Default for AiderAdapter {
46    fn default() -> Self {
47        Self::new()
48    }
49}
50
51impl IndustryFileAdapter for AiderAdapter {
52    fn name(&self) -> &'static str {
53        "aider"
54    }
55
56    fn can_handle(&self, project_root: &Path) -> bool {
57        project_root.join(".aider.conf.yml").exists()
58    }
59
60    fn read_config(&self, project_root: &Path) -> StorageResult<Config> {
61        let mut config = Config::default();
62
63        if let Ok(Some(aider_config)) = self.read_aider_config(project_root) {
64            debug!("Adding Aider configuration as steering rule");
65            config.steering.push(SteeringRule {
66                name: "aider-config".to_string(),
67                content: aider_config,
68                format: DocumentFormat::Markdown,
69            });
70        }
71
72        Ok(config)
73    }
74
75    fn priority(&self) -> u32 {
76        // Aider has medium priority
77        50
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84    use std::fs;
85    use tempfile::TempDir;
86
87    #[test]
88    fn test_aider_adapter_detects_config() {
89        let temp_dir = TempDir::new().unwrap();
90        let aider_config_path = temp_dir.path().join(".aider.conf.yml");
91        fs::write(&aider_config_path, "model: gpt-4").unwrap();
92
93        let adapter = AiderAdapter::new();
94        assert!(adapter.can_handle(temp_dir.path()));
95    }
96
97    #[test]
98    fn test_aider_adapter_no_file() {
99        let temp_dir = TempDir::new().unwrap();
100
101        let adapter = AiderAdapter::new();
102        assert!(!adapter.can_handle(temp_dir.path()));
103    }
104
105    #[test]
106    fn test_aider_adapter_reads_config() {
107        let temp_dir = TempDir::new().unwrap();
108        let aider_config_path = temp_dir.path().join(".aider.conf.yml");
109        let config_content = "model: gpt-4\ntemperature: 0.7";
110        fs::write(&aider_config_path, config_content).unwrap();
111
112        let adapter = AiderAdapter::new();
113        let config = adapter.read_config(temp_dir.path()).unwrap();
114
115        assert_eq!(config.steering.len(), 1);
116        assert_eq!(config.steering[0].name, "aider-config");
117        assert_eq!(config.steering[0].content, config_content);
118        assert_eq!(config.steering[0].format, DocumentFormat::Markdown);
119    }
120
121    #[test]
122    fn test_aider_adapter_priority() {
123        let adapter = AiderAdapter::new();
124        assert_eq!(adapter.priority(), 50);
125    }
126
127    #[test]
128    fn test_aider_adapter_name() {
129        let adapter = AiderAdapter::new();
130        assert_eq!(adapter.name(), "aider");
131    }
132}