rfgrep 0.5.0

Advanced recursive file grep utility with comprehensive file type classification - search, list, and analyze 153+ file formats with intelligent filtering and safety policies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! Plugin system for extensible search capabilities
use crate::error::Result as RfgrepResult;
use crate::processor::SearchMatch;
use std::collections::HashMap;
use std::path::Path;

/// Trait for search plugins
pub trait SearchPlugin: Send + Sync {
    fn name(&self) -> &str;
    fn can_handle(&self, file: &Path) -> bool;
    fn search(&self, file: &Path, pattern: &str) -> RfgrepResult<Vec<SearchMatch>>;
    fn priority(&self) -> u32 {
        100
    }
}

/// Plugin manager that coordinates different search plugins
pub struct PluginManager {
    plugins: Vec<Box<dyn SearchPlugin>>,
    plugin_cache: HashMap<String, usize>,
}

impl PluginManager {
    pub fn new() -> RfgrepResult<Self> {
        let mut manager = Self {
            plugins: Vec::new(),
            plugin_cache: HashMap::new(),
        };

        // Register built-in plugins
        manager.register_plugin(Box::new(TextSearchPlugin::new()?));
        manager.register_plugin(Box::new(BinarySearchPlugin::new()?));
        manager.register_plugin(Box::new(ArchiveSearchPlugin::new()?));
        manager.register_plugin(Box::new(DatabaseSearchPlugin::new()?));
        manager.register_plugin(Box::new(ImageSearchPlugin::new()?));

        Ok(manager)
    }

    pub fn register_plugin(&mut self, plugin: Box<dyn SearchPlugin>) {
        let name = plugin.name().to_string();
        let index = self.plugins.len();
        self.plugins.push(plugin);
        self.plugin_cache.insert(name, index);
    }

    pub fn search_file(&self, file: &Path, pattern: &str) -> RfgrepResult<Vec<SearchMatch>> {
        // Find the best plugin for this file
        let mut candidates: Vec<_> = self
            .plugins
            .iter()
            .enumerate()
            .filter(|(_, plugin)| plugin.can_handle(file))
            .collect();

        // Sort by priority (lower number = higher priority)
        candidates.sort_by_key(|(_, plugin)| plugin.priority());

        if let Some((_, plugin)) = candidates.first() {
            plugin.search(file, pattern)
        } else {
            // Fallback to text search
            Ok(vec![])
        }
    }

    pub fn get_plugin_names(&self) -> Vec<&str> {
        self.plugins.iter().map(|p| p.name()).collect()
    }
}

/// Text file search plugin
pub struct TextSearchPlugin {
    text_extensions: Vec<String>,
}

impl TextSearchPlugin {
    pub fn new() -> RfgrepResult<Self> {
        Ok(Self {
            text_extensions: vec![
                "txt".to_string(),
                "md".to_string(),
                "rs".to_string(),
                "py".to_string(),
                "js".to_string(),
                "ts".to_string(),
                "go".to_string(),
                "java".to_string(),
                "cpp".to_string(),
                "c".to_string(),
                "h".to_string(),
                "hpp".to_string(),
                "json".to_string(),
                "yaml".to_string(),
                "toml".to_string(),
                "xml".to_string(),
                "html".to_string(),
                "css".to_string(),
                "sh".to_string(),
                "bash".to_string(),
                "zsh".to_string(),
                "fish".to_string(),
                "ps1".to_string(),
                "bat".to_string(),
            ],
        })
    }
}

impl SearchPlugin for TextSearchPlugin {
    fn name(&self) -> &str {
        "text"
    }

    fn can_handle(&self, file: &Path) -> bool {
        if let Some(ext) = file.extension().and_then(|e| e.to_str()) {
            self.text_extensions
                .iter()
                .any(|e| e.eq_ignore_ascii_case(ext))
        } else {
            // Check if file is text by reading first few bytes
            if let Ok(content) = std::fs::read(file) {
                content.iter().take(1024).all(|&b| b.is_ascii() || b >= 128)
            } else {
                false
            }
        }
    }

    fn search(&self, file: &Path, pattern: &str) -> RfgrepResult<Vec<SearchMatch>> {
        let content = std::fs::read_to_string(file)?;
        let regex = regex::Regex::new(pattern)?;

        let mut matches = Vec::new();
        for (line_num, line) in content.lines().enumerate() {
            for mat in regex.find_iter(line) {
                matches.push(SearchMatch {
                    path: file.to_path_buf(),
                    line_number: line_num + 1,
                    line: line.to_string(),
                    context_before: Vec::new(),
                    context_after: Vec::new(),
                    matched_text: mat.as_str().to_string(),
                    column_start: mat.start(),
                    column_end: mat.end(),
                });
            }
        }

        Ok(matches)
    }

    fn priority(&self) -> u32 {
        10
    }
}

/// Binary file search plugin
pub struct BinarySearchPlugin {
    binary_extensions: Vec<String>,
}

impl BinarySearchPlugin {
    pub fn new() -> RfgrepResult<Self> {
        Ok(Self {
            binary_extensions: vec![
                "exe".to_string(),
                "dll".to_string(),
                "so".to_string(),
                "dylib".to_string(),
                "bin".to_string(),
                "obj".to_string(),
                "o".to_string(),
                "a".to_string(),
                "lib".to_string(),
            ],
        })
    }
}

impl SearchPlugin for BinarySearchPlugin {
    fn name(&self) -> &str {
        "binary"
    }

    fn can_handle(&self, file: &Path) -> bool {
        if let Some(ext) = file.extension().and_then(|e| e.to_str()) {
            self.binary_extensions
                .iter()
                .any(|e| e.eq_ignore_ascii_case(ext))
        } else {
            // Check if file is binary by reading first few bytes
            if let Ok(content) = std::fs::read(file) {
                content.iter().take(1024).any(|&b| b == 0)
            } else {
                false
            }
        }
    }

    fn search(&self, file: &Path, pattern: &str) -> RfgrepResult<Vec<SearchMatch>> {
        let content = std::fs::read(file)?;
        let pattern_bytes = pattern.as_bytes();

        let mut matches = Vec::new();
        let mut pos = 0;

        while let Some(found) = memchr::memmem::find(&content[pos..], pattern_bytes) {
            let absolute_pos = pos + found;
            matches.push(SearchMatch {
                path: file.to_path_buf(),
                line_number: 1, // Binary files don't have line numbers
                line: format!("Binary data at offset {}", absolute_pos),
                context_before: Vec::new(),
                context_after: Vec::new(),
                matched_text: pattern.to_string(),
                column_start: absolute_pos,
                column_end: absolute_pos + pattern_bytes.len(),
            });
            pos = absolute_pos + 1;
        }

        Ok(matches)
    }

    fn priority(&self) -> u32 {
        20
    }
}

/// Archive file search plugin
pub struct ArchiveSearchPlugin {
    archive_extensions: Vec<String>,
}

impl ArchiveSearchPlugin {
    pub fn new() -> RfgrepResult<Self> {
        Ok(Self {
            archive_extensions: vec![
                "zip".to_string(),
                "tar".to_string(),
                "gz".to_string(),
                "bz2".to_string(),
                "xz".to_string(),
                "7z".to_string(),
                "rar".to_string(),
                "tar.gz".to_string(),
                "tgz".to_string(),
            ],
        })
    }
}

impl SearchPlugin for ArchiveSearchPlugin {
    fn name(&self) -> &str {
        "archive"
    }

    fn can_handle(&self, file: &Path) -> bool {
        if let Some(ext) = file.extension().and_then(|e| e.to_str()) {
            self.archive_extensions
                .iter()
                .any(|e| e.eq_ignore_ascii_case(ext))
        } else {
            false
        }
    }

    fn search(&self, file: &Path, pattern: &str) -> RfgrepResult<Vec<SearchMatch>> {
        // For now, just search the archive metadata
        // In a full implementation, this would extract and search contents
        let content = std::fs::read(file)?;
        let pattern_bytes = pattern.as_bytes();

        let mut matches = Vec::new();
        let mut pos = 0;

        while let Some(found) = memchr::memmem::find(&content[pos..], pattern_bytes) {
            let absolute_pos = pos + found;
            matches.push(SearchMatch {
                path: file.to_path_buf(),
                line_number: 1,
                line: format!("Archive metadata at offset {}", absolute_pos),
                context_before: Vec::new(),
                context_after: Vec::new(),
                matched_text: pattern.to_string(),
                column_start: absolute_pos,
                column_end: absolute_pos + pattern_bytes.len(),
            });
            pos = absolute_pos + 1;
        }

        Ok(matches)
    }

    fn priority(&self) -> u32 {
        30
    }
}

/// Database file search plugin
pub struct DatabaseSearchPlugin {
    db_extensions: Vec<String>,
}

impl DatabaseSearchPlugin {
    pub fn new() -> RfgrepResult<Self> {
        Ok(Self {
            db_extensions: vec![
                "sqlite".to_string(),
                "db".to_string(),
                "sqlite3".to_string(),
                "mdb".to_string(),
                "accdb".to_string(),
            ],
        })
    }
}

impl SearchPlugin for DatabaseSearchPlugin {
    fn name(&self) -> &str {
        "database"
    }

    fn can_handle(&self, file: &Path) -> bool {
        if let Some(ext) = file.extension().and_then(|e| e.to_str()) {
            self.db_extensions
                .iter()
                .any(|e| e.eq_ignore_ascii_case(ext))
        } else {
            false
        }
    }

    fn search(&self, file: &Path, pattern: &str) -> RfgrepResult<Vec<SearchMatch>> {
        // For now, just search the database file as binary
        // In a full implementation, this would connect to the database and search
        let content = std::fs::read(file)?;
        let pattern_bytes = pattern.as_bytes();

        let mut matches = Vec::new();
        let mut pos = 0;

        while let Some(found) = memchr::memmem::find(&content[pos..], pattern_bytes) {
            let absolute_pos = pos + found;
            matches.push(SearchMatch {
                path: file.to_path_buf(),
                line_number: 1,
                line: format!("Database content at offset {}", absolute_pos),
                context_before: Vec::new(),
                context_after: Vec::new(),
                matched_text: pattern.to_string(),
                column_start: absolute_pos,
                column_end: absolute_pos + pattern_bytes.len(),
            });
            pos = absolute_pos + 1;
        }

        Ok(matches)
    }

    fn priority(&self) -> u32 {
        40
    }
}

/// Image file search plugin
pub struct ImageSearchPlugin {
    image_extensions: Vec<String>,
}

impl ImageSearchPlugin {
    pub fn new() -> RfgrepResult<Self> {
        Ok(Self {
            image_extensions: vec![
                "jpg".to_string(),
                "jpeg".to_string(),
                "png".to_string(),
                "gif".to_string(),
                "bmp".to_string(),
                "webp".to_string(),
                "svg".to_string(),
                "ico".to_string(),
                "tiff".to_string(),
            ],
        })
    }
}

impl SearchPlugin for ImageSearchPlugin {
    fn name(&self) -> &str {
        "image"
    }

    fn can_handle(&self, file: &Path) -> bool {
        if let Some(ext) = file.extension().and_then(|e| e.to_str()) {
            self.image_extensions
                .iter()
                .any(|e| e.eq_ignore_ascii_case(ext))
        } else {
            false
        }
    }

    fn search(&self, file: &Path, pattern: &str) -> RfgrepResult<Vec<SearchMatch>> {
        // Search image metadata (EXIF, etc.)
        let content = std::fs::read(file)?;
        let pattern_bytes = pattern.as_bytes();

        let mut matches = Vec::new();
        let mut pos = 0;

        while let Some(found) = memchr::memmem::find(&content[pos..], pattern_bytes) {
            let absolute_pos = pos + found;
            matches.push(SearchMatch {
                path: file.to_path_buf(),
                line_number: 1,
                line: format!("Image metadata at offset {}", absolute_pos),
                context_before: Vec::new(),
                context_after: Vec::new(),
                matched_text: pattern.to_string(),
                column_start: absolute_pos,
                column_end: absolute_pos + pattern_bytes.len(),
            });
            pos = absolute_pos + 1;
        }

        Ok(matches)
    }

    fn priority(&self) -> u32 {
        50
    }
}

/// Plugin configuration
#[derive(Debug, Clone)]
pub struct PluginConfig {
    pub enabled_plugins: Vec<String>,
    pub plugin_settings: HashMap<String, serde_json::Value>,
}

impl Default for PluginConfig {
    fn default() -> Self {
        Self {
            enabled_plugins: vec![
                "text".to_string(),
                "binary".to_string(),
                "archive".to_string(),
                "database".to_string(),
            ],
            plugin_settings: HashMap::new(),
        }
    }
}