Skip to main content

cc_audit/engine/scanners/
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        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(|entry| match entry {
104                    Ok(e) => Some(e),
105                    Err(e) => {
106                        tracing::warn!(error = %e, "ディレクトリエントリの読み取りに失敗。スキップします");
107                        None
108                    }
109                })
110                .filter(|e| e.file_type().is_file())
111                .filter(|e| self.matches_extension(e.path()))
112                .filter(|e| !self.is_ignored(e.path()))
113                .map(|e| e.path().to_path_buf())
114                .collect::<Vec<_>>()
115        })
116    }
117
118    /// Walk a single directory (not using patterns).
119    pub fn walk_single(&self, dir: &Path) -> impl Iterator<Item = PathBuf> + '_ {
120        let mut walker = WalkDir::new(dir).follow_links(self.config.follow_symlinks);
121
122        if let Some(depth) = self.config.max_depth {
123            walker = walker.max_depth(depth);
124        }
125
126        walker
127            .into_iter()
128            .filter_map(|entry| match entry {
129                Ok(e) => Some(e),
130                Err(e) => {
131                    tracing::warn!(error = %e, "ディレクトリエントリの読み取りに失敗。スキップします");
132                    None
133                }
134            })
135            .filter(|e| e.file_type().is_file())
136            .filter(|e| self.matches_extension(e.path()))
137            .filter(|e| !self.is_ignored(e.path()))
138            .map(|e| e.path().to_path_buf())
139            .collect::<Vec<_>>()
140            .into_iter()
141    }
142}
143
144#[cfg(test)]
145mod tests {
146    use super::*;
147    use std::fs;
148    use tempfile::TempDir;
149
150    fn create_test_dir() -> TempDir {
151        let dir = TempDir::new().unwrap();
152
153        // Create test structure
154        let commands = dir.path().join(".claude").join("commands");
155        fs::create_dir_all(&commands).unwrap();
156        fs::write(commands.join("test.md"), "test content").unwrap();
157        fs::write(commands.join("other.txt"), "other content").unwrap();
158
159        let scripts = dir.path().join("scripts");
160        fs::create_dir_all(&scripts).unwrap();
161        fs::write(scripts.join("script.sh"), "#!/bin/bash").unwrap();
162
163        dir
164    }
165
166    #[test]
167    fn test_walk_with_pattern() {
168        let dir = create_test_dir();
169        let config = WalkConfig::new([".claude/commands"]).with_extensions(&["md"]);
170
171        let walker = DirectoryWalker::new(config);
172        let files: Vec<_> = walker.walk(dir.path()).collect();
173
174        assert_eq!(files.len(), 1);
175        assert!(files[0].ends_with("test.md"));
176    }
177
178    #[test]
179    fn test_walk_without_extension_filter() {
180        let dir = create_test_dir();
181        let config = WalkConfig::new([".claude/commands"]);
182
183        let walker = DirectoryWalker::new(config);
184        let files: Vec<_> = walker.walk(dir.path()).collect();
185
186        assert_eq!(files.len(), 2);
187    }
188
189    #[test]
190    fn test_walk_single() {
191        let dir = create_test_dir();
192        let config = WalkConfig::default().with_extensions(&["sh"]);
193
194        let walker = DirectoryWalker::new(config);
195        let scripts_dir = dir.path().join("scripts");
196        let files: Vec<_> = walker.walk_single(&scripts_dir).collect();
197
198        assert_eq!(files.len(), 1);
199        assert!(files[0].ends_with("script.sh"));
200    }
201
202    #[test]
203    fn test_walk_nonexistent_pattern() {
204        let dir = create_test_dir();
205        let config = WalkConfig::new(["nonexistent"]);
206
207        let walker = DirectoryWalker::new(config);
208        let files: Vec<_> = walker.walk(dir.path()).collect();
209
210        assert!(files.is_empty());
211    }
212
213    #[test]
214    fn test_walk_with_max_depth() {
215        let dir = create_test_dir();
216
217        // Create nested structure
218        let nested = dir.path().join("deep").join("nested").join("dir");
219        fs::create_dir_all(&nested).unwrap();
220        fs::write(nested.join("file.md"), "content").unwrap();
221
222        let config = WalkConfig::new(["deep"]).with_max_depth(1);
223
224        let walker = DirectoryWalker::new(config);
225        let files: Vec<_> = walker.walk(dir.path()).collect();
226
227        // Should not find the deeply nested file
228        assert!(files.is_empty());
229    }
230}