Skip to main content

cc_audit/discovery/
walker.rs

1//! Directory walking abstraction for consistent file discovery.
2
3use crate::ignore::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        // Case-insensitive so `.MD`/`.SH` aren't skipped on case-sensitive FS. (#228)
83        path.extension()
84            .and_then(|ext| ext.to_str())
85            .is_some_and(|ext| {
86                self.config
87                    .file_extensions
88                    .contains(&ext.to_lowercase().as_str())
89            })
90    }
91
92    /// Walk the directory and yield matching file paths.
93    pub fn walk<'a>(&'a self, base_dir: &'a Path) -> impl Iterator<Item = PathBuf> + 'a {
94        self.config.root_patterns.iter().flat_map(move |pattern| {
95            let target = base_dir.join(pattern);
96            if !target.exists() {
97                return Vec::new();
98            }
99
100            let mut walker = WalkDir::new(&target).follow_links(self.config.follow_symlinks);
101
102            if let Some(depth) = self.config.max_depth {
103                walker = walker.max_depth(depth);
104            }
105
106            walker
107                .into_iter()
108                .filter_map(|e| e.ok())
109                .filter(|e| e.file_type().is_file())
110                .filter(|e| self.matches_extension(e.path()))
111                .filter(|e| !self.is_ignored(e.path()))
112                .map(|e| e.path().to_path_buf())
113                .collect::<Vec<_>>()
114        })
115    }
116
117    /// Walk a single directory (not using patterns).
118    pub fn walk_single(&self, dir: &Path) -> Vec<PathBuf> {
119        let mut walker = WalkDir::new(dir).follow_links(self.config.follow_symlinks);
120
121        if let Some(depth) = self.config.max_depth {
122            walker = walker.max_depth(depth);
123        }
124
125        walker
126            .into_iter()
127            .filter_map(|e| e.ok())
128            .filter(|e| e.file_type().is_file())
129            .filter(|e| self.matches_extension(e.path()))
130            .filter(|e| !self.is_ignored(e.path()))
131            .map(|e| e.path().to_path_buf())
132            .collect()
133    }
134}
135
136#[cfg(test)]
137mod tests {
138    use super::*;
139    use std::fs;
140    use tempfile::TempDir;
141
142    fn create_test_dir() -> TempDir {
143        let dir = TempDir::new().unwrap();
144
145        // Create test structure
146        let commands = dir.path().join(".claude").join("commands");
147        fs::create_dir_all(&commands).unwrap();
148        fs::write(commands.join("test.md"), "test content").unwrap();
149        fs::write(commands.join("other.txt"), "other content").unwrap();
150
151        let scripts = dir.path().join("scripts");
152        fs::create_dir_all(&scripts).unwrap();
153        fs::write(scripts.join("script.sh"), "#!/bin/bash").unwrap();
154
155        dir
156    }
157
158    #[test]
159    fn test_walk_with_pattern() {
160        let dir = create_test_dir();
161        let config = WalkConfig::new([".claude/commands"]).with_extensions(&["md"]);
162
163        let walker = DirectoryWalker::new(config);
164        let files: Vec<_> = walker.walk(dir.path()).collect();
165
166        assert_eq!(files.len(), 1);
167        assert!(files[0].ends_with("test.md"));
168    }
169
170    #[test]
171    fn test_walk_without_extension_filter() {
172        let dir = create_test_dir();
173        let config = WalkConfig::new([".claude/commands"]);
174
175        let walker = DirectoryWalker::new(config);
176        let files: Vec<_> = walker.walk(dir.path()).collect();
177
178        assert_eq!(files.len(), 2);
179    }
180
181    #[test]
182    fn test_walk_single() {
183        let dir = create_test_dir();
184        let config = WalkConfig::default().with_extensions(&["sh"]);
185
186        let walker = DirectoryWalker::new(config);
187        let scripts_dir = dir.path().join("scripts");
188        let files = walker.walk_single(&scripts_dir);
189
190        assert_eq!(files.len(), 1);
191        assert!(files[0].ends_with("script.sh"));
192    }
193
194    #[test]
195    fn test_walk_nonexistent_pattern() {
196        let dir = create_test_dir();
197        let config = WalkConfig::new(["nonexistent"]);
198
199        let walker = DirectoryWalker::new(config);
200        let files: Vec<_> = walker.walk(dir.path()).collect();
201
202        assert!(files.is_empty());
203    }
204
205    #[test]
206    fn test_walk_with_max_depth() {
207        let dir = create_test_dir();
208
209        // Create nested structure
210        let nested = dir.path().join("deep").join("nested").join("dir");
211        fs::create_dir_all(&nested).unwrap();
212        fs::write(nested.join("file.md"), "content").unwrap();
213
214        let config = WalkConfig::new(["deep"]).with_max_depth(1);
215
216        let walker = DirectoryWalker::new(config);
217        let files: Vec<_> = walker.walk(dir.path()).collect();
218
219        // Should not find the deeply nested file
220        assert!(files.is_empty());
221    }
222}