Skip to main content

cc_audit/discovery/
walker.rs

1//! Directory walking abstraction for consistent file discovery.
2
3use crate::discovery::filter::IgnoreFilter;
4use std::path::{Path, PathBuf};
5use walkdir::WalkDir;
6
7/// Configuration for directory walking.
8#[derive(Debug, Clone, Default)]
9pub struct WalkConfig {
10    /// Root patterns to search (e.g., [".claude/commands", "commands"]).
11    pub root_patterns: Vec<PathBuf>,
12    /// File extensions to include (e.g., ["md", "yaml", "json"]).
13    pub file_extensions: Vec<&'static str>,
14    /// Maximum depth to traverse. None means unlimited.
15    pub max_depth: Option<usize>,
16    /// Whether to follow symbolic links.
17    pub follow_symlinks: bool,
18}
19
20impl WalkConfig {
21    /// Create a new WalkConfig with specified patterns.
22    pub fn new(patterns: impl IntoIterator<Item = impl Into<PathBuf>>) -> Self {
23        Self {
24            root_patterns: patterns.into_iter().map(Into::into).collect(),
25            ..Default::default()
26        }
27    }
28
29    /// Set file extensions to include.
30    pub fn with_extensions(mut self, extensions: &[&'static str]) -> Self {
31        self.file_extensions = extensions.to_vec();
32        self
33    }
34
35    /// Set maximum depth.
36    pub fn with_max_depth(mut self, depth: usize) -> Self {
37        self.max_depth = Some(depth);
38        self
39    }
40
41    /// Set whether to follow symlinks.
42    pub fn with_follow_symlinks(mut self, follow: bool) -> Self {
43        self.follow_symlinks = follow;
44        self
45    }
46}
47
48/// Directory walker with optional ignore filter.
49pub struct DirectoryWalker {
50    config: WalkConfig,
51    ignore_filter: Option<IgnoreFilter>,
52}
53
54impl DirectoryWalker {
55    /// Create a new DirectoryWalker with the given configuration.
56    pub fn new(config: WalkConfig) -> Self {
57        Self {
58            config,
59            ignore_filter: None,
60        }
61    }
62
63    /// Set an ignore filter.
64    pub fn with_ignore_filter(mut self, filter: IgnoreFilter) -> Self {
65        self.ignore_filter = Some(filter);
66        self
67    }
68
69    /// Check if a path should be ignored.
70    fn is_ignored(&self, path: &Path) -> bool {
71        self.ignore_filter
72            .as_ref()
73            .is_some_and(|f| f.is_ignored(path))
74    }
75
76    /// Check if a path matches the configured extensions.
77    fn matches_extension(&self, path: &Path) -> bool {
78        if self.config.file_extensions.is_empty() {
79            return true;
80        }
81
82        path.extension()
83            .and_then(|ext| ext.to_str())
84            .is_some_and(|ext| self.config.file_extensions.contains(&ext))
85    }
86
87    /// Walk the directory and yield matching file paths.
88    pub fn walk<'a>(&'a self, base_dir: &'a Path) -> impl Iterator<Item = PathBuf> + 'a {
89        self.config.root_patterns.iter().flat_map(move |pattern| {
90            let target = base_dir.join(pattern);
91            if !target.exists() {
92                return Vec::new();
93            }
94
95            let mut walker = WalkDir::new(&target).follow_links(self.config.follow_symlinks);
96
97            if let Some(depth) = self.config.max_depth {
98                walker = walker.max_depth(depth);
99            }
100
101            walker
102                .into_iter()
103                .filter_map(|e| e.ok())
104                .filter(|e| e.file_type().is_file())
105                .filter(|e| self.matches_extension(e.path()))
106                .filter(|e| !self.is_ignored(e.path()))
107                .map(|e| e.path().to_path_buf())
108                .collect::<Vec<_>>()
109        })
110    }
111
112    /// Walk a single directory (not using patterns).
113    pub fn walk_single(&self, dir: &Path) -> impl Iterator<Item = PathBuf> + '_ {
114        let mut walker = WalkDir::new(dir).follow_links(self.config.follow_symlinks);
115
116        if let Some(depth) = self.config.max_depth {
117            walker = walker.max_depth(depth);
118        }
119
120        walker
121            .into_iter()
122            .filter_map(|e| e.ok())
123            .filter(|e| e.file_type().is_file())
124            .filter(|e| self.matches_extension(e.path()))
125            .filter(|e| !self.is_ignored(e.path()))
126            .map(|e| e.path().to_path_buf())
127            .collect::<Vec<_>>()
128            .into_iter()
129    }
130}
131
132#[cfg(test)]
133mod tests {
134    use super::*;
135    use std::fs;
136    use tempfile::TempDir;
137
138    fn create_test_dir() -> TempDir {
139        let dir = TempDir::new().unwrap();
140
141        // Create test structure
142        let commands = dir.path().join(".claude").join("commands");
143        fs::create_dir_all(&commands).unwrap();
144        fs::write(commands.join("test.md"), "test content").unwrap();
145        fs::write(commands.join("other.txt"), "other content").unwrap();
146
147        let scripts = dir.path().join("scripts");
148        fs::create_dir_all(&scripts).unwrap();
149        fs::write(scripts.join("script.sh"), "#!/bin/bash").unwrap();
150
151        dir
152    }
153
154    #[test]
155    fn test_walk_with_pattern() {
156        let dir = create_test_dir();
157        let config = WalkConfig::new([".claude/commands"]).with_extensions(&["md"]);
158
159        let walker = DirectoryWalker::new(config);
160        let files: Vec<_> = walker.walk(dir.path()).collect();
161
162        assert_eq!(files.len(), 1);
163        assert!(files[0].ends_with("test.md"));
164    }
165
166    #[test]
167    fn test_walk_without_extension_filter() {
168        let dir = create_test_dir();
169        let config = WalkConfig::new([".claude/commands"]);
170
171        let walker = DirectoryWalker::new(config);
172        let files: Vec<_> = walker.walk(dir.path()).collect();
173
174        assert_eq!(files.len(), 2);
175    }
176
177    #[test]
178    fn test_walk_single() {
179        let dir = create_test_dir();
180        let config = WalkConfig::default().with_extensions(&["sh"]);
181
182        let walker = DirectoryWalker::new(config);
183        let scripts_dir = dir.path().join("scripts");
184        let files: Vec<_> = walker.walk_single(&scripts_dir).collect();
185
186        assert_eq!(files.len(), 1);
187        assert!(files[0].ends_with("script.sh"));
188    }
189
190    #[test]
191    fn test_walk_nonexistent_pattern() {
192        let dir = create_test_dir();
193        let config = WalkConfig::new(["nonexistent"]);
194
195        let walker = DirectoryWalker::new(config);
196        let files: Vec<_> = walker.walk(dir.path()).collect();
197
198        assert!(files.is_empty());
199    }
200
201    #[test]
202    fn test_walk_with_max_depth() {
203        let dir = create_test_dir();
204
205        // Create nested structure
206        let nested = dir.path().join("deep").join("nested").join("dir");
207        fs::create_dir_all(&nested).unwrap();
208        fs::write(nested.join("file.md"), "content").unwrap();
209
210        let config = WalkConfig::new(["deep"]).with_max_depth(1);
211
212        let walker = DirectoryWalker::new(config);
213        let files: Vec<_> = walker.walk(dir.path()).collect();
214
215        // Should not find the deeply nested file
216        assert!(files.is_empty());
217    }
218}