liblevenshtein 0.9.1

Levenshtein/Universal Automata for approximate string matching using various dictionary backends
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! Support for grepping through compressed and archived files.
//!
//! This module provides streaming support for searching through:
//! - Compressed files: `.gz`, `.zst`, `.xz`, `.lzma`, `.bz2`
//! - Archive files: `.tar`, `.tar.gz`, `.tar.zst`, `.tar.xz`, `.tar.bz2`, `.zip`
//!
//! All operations are streaming (chunk-by-chunk) for memory efficiency.
//!
//! # Features
//!
//! This module requires one or more optional features:
//! - `grep-compression`: Enables compression support (gzip, zstd, xz, bzip2)
//! - `grep-archives`: Enables archive support (tar, zip) - implies `grep-compression`
//! - `grep-full`: Enables all grep features including phonetic rules
//!
//! # Usage
//!
//! ## Searching a compressed file
//!
//! ```ignore
//! use liblevenshtein::grep::{GrepSource, GrepPath, GrepConfig};
//!
//! let path = GrepPath::parse("logs.gz")?;
//! let source = GrepSource::new(GrepConfig::default());
//!
//! for entry in source.open(&path)? {
//!     let entry = entry?;
//!     // Process entry.content() with your grep matcher
//! }
//! ```
//!
//! ## Searching within archives
//!
//! ```ignore
//! use liblevenshtein::grep::{GrepSource, GrepPath, GrepConfig};
//!
//! // Search all files in archive
//! let path = GrepPath::parse("source.tar.gz")?;
//!
//! // Or filter to specific entries
//! let path = GrepPath::parse("logs.tar.gz:*.log")?;
//!
//! let source = GrepSource::new(GrepConfig::default());
//! for entry in source.open(&path)? {
//!     let entry = entry?;
//!     println!("Searching: {}", entry.source_id());
//!     // Process entry.content()
//! }
//! ```

pub mod compression;
pub mod error;
pub mod position_tracker;
pub mod result;
pub mod source;

#[cfg(any(feature = "tar", feature = "zip"))]
pub mod archive;

#[cfg(any(
    feature = "grep-pdf",
    feature = "grep-docx",
    feature = "grep-xlsx",
    feature = "grep-epub",
    feature = "grep-odt"
))]
pub mod document;

// Re-export main types at module level for convenient access
pub use compression::CompressionFormat;
pub use error::{GrepError, GrepResult};
pub use position_tracker::PositionTracker;
pub use result::{GrepMatchResult, MatchLocation, SourceId};
pub use source::GrepPath;

#[cfg(any(feature = "tar", feature = "zip"))]
pub use archive::ArchiveFormat;

#[cfg(any(
    feature = "grep-pdf",
    feature = "grep-docx",
    feature = "grep-xlsx",
    feature = "grep-epub",
    feature = "grep-odt"
))]
pub use document::{DocumentExtractor, DocumentExtractorConfig, DocumentFormat};

use std::fs::File;
use std::io::{self, BufReader, Read};
use std::path::Path;

use compression::create_decompressor;

#[cfg(feature = "tar")]
use archive::tar::TarArchiveReader;

#[cfg(feature = "zip")]
use archive::zip::ZipArchiveReader;

/// Configuration for grep source operations.
#[derive(Debug, Clone)]
pub struct GrepConfig {
    /// Whether to auto-detect compression format.
    /// If false, files are treated as plain text.
    pub auto_decompress: bool,

    /// Maximum file size to read (bytes). Files larger than this are skipped.
    /// `None` means no limit.
    pub max_file_size: Option<u64>,

    /// Skip files that appear to be binary (contain NUL bytes).
    pub skip_binary: bool,

    /// Whether to include hidden files (starting with `.`).
    pub include_hidden: bool,

    /// Default glob filter for archives when none is specified.
    /// `None` means search all entries.
    pub default_archive_filter: Option<String>,

    /// Whether to extract text from document files (PDF, DOCX, etc.).
    pub extract_documents: bool,

    /// Enable OCR for image-based PDFs.
    pub enable_ocr: bool,

    /// OCR language code (Tesseract format, e.g., "eng").
    pub ocr_language: String,
}

impl Default for GrepConfig {
    fn default() -> Self {
        Self {
            auto_decompress: true,
            max_file_size: Some(100 * 1024 * 1024), // 100 MB default limit
            skip_binary: true,
            include_hidden: false,
            default_archive_filter: None,
            // Auto-enable document extraction when document features are available
            #[cfg(any(
                feature = "grep-pdf",
                feature = "grep-docx",
                feature = "grep-xlsx",
                feature = "grep-epub",
                feature = "grep-odt"
            ))]
            extract_documents: true,
            #[cfg(not(any(
                feature = "grep-pdf",
                feature = "grep-docx",
                feature = "grep-xlsx",
                feature = "grep-epub",
                feature = "grep-odt"
            )))]
            extract_documents: false,
            enable_ocr: false,
            ocr_language: "eng".to_string(),
        }
    }
}

impl GrepConfig {
    /// Create a new config with default settings.
    pub fn new() -> Self {
        Self::default()
    }

    /// Disable auto-decompression (treat compressed files as plain text).
    pub fn no_decompress(mut self) -> Self {
        self.auto_decompress = false;
        self
    }

    /// Set maximum file size limit.
    pub fn max_size(mut self, bytes: u64) -> Self {
        self.max_file_size = Some(bytes);
        self
    }

    /// Remove file size limit.
    pub fn no_size_limit(mut self) -> Self {
        self.max_file_size = None;
        self
    }

    /// Skip binary files.
    pub fn skip_binary_files(mut self, skip: bool) -> Self {
        self.skip_binary = skip;
        self
    }

    /// Include hidden files.
    pub fn include_hidden_files(mut self, include: bool) -> Self {
        self.include_hidden = include;
        self
    }

    /// Set default archive filter pattern.
    pub fn default_filter(mut self, pattern: impl Into<String>) -> Self {
        self.default_archive_filter = Some(pattern.into());
        self
    }

    /// Enable document text extraction (PDF, DOCX, etc.).
    pub fn extract_documents(mut self, extract: bool) -> Self {
        self.extract_documents = extract;
        self
    }

    /// Enable OCR for image-based PDFs.
    pub fn enable_ocr(mut self, enable: bool) -> Self {
        self.enable_ocr = enable;
        self
    }

    /// Set OCR language code (Tesseract format, e.g., "eng", "deu").
    pub fn ocr_language(mut self, lang: impl Into<String>) -> Self {
        self.ocr_language = lang.into();
        self
    }

    /// Enable document extraction with OCR.
    pub fn with_ocr(self, language: impl Into<String>) -> Self {
        self.extract_documents(true)
            .enable_ocr(true)
            .ocr_language(language)
    }
}

/// A source for grep operations supporting compressed and archived files.
///
/// # Example
///
/// ```ignore
/// use liblevenshtein::grep::{GrepSource, GrepPath, GrepConfig};
///
/// let source = GrepSource::new(GrepConfig::default());
/// let path = GrepPath::parse("data.tar.gz:*.log")?;
///
/// for entry in source.open(&path)? {
///     let entry = entry?;
///     // entry.source_id() identifies the file/archive entry
///     // entry.content() provides the decompressed content
/// }
/// ```
#[derive(Debug, Clone)]
pub struct GrepSource {
    config: GrepConfig,
}

impl GrepSource {
    /// Create a new grep source with the given configuration.
    pub fn new(config: GrepConfig) -> Self {
        Self { config }
    }

    /// Create a grep source with default configuration.
    pub fn with_defaults() -> Self {
        Self::new(GrepConfig::default())
    }

    /// Get the configuration.
    pub fn config(&self) -> &GrepConfig {
        &self.config
    }

    /// Open a path and return an iterator over its entries.
    ///
    /// For plain files, returns a single entry.
    /// For archives, returns an entry for each matching file within.
    pub fn open(&self, path: &GrepPath) -> GrepResult<GrepEntryIterator> {
        let fs_path = &path.filesystem_path;

        // Check file exists
        if !fs_path.exists() {
            return Err(GrepError::Io(io::Error::new(
                io::ErrorKind::NotFound,
                format!("file not found: {}", fs_path.display()),
            )));
        }

        // Check file size limit
        if let Some(max_size) = self.config.max_file_size {
            let metadata = fs_path.metadata()?;
            if metadata.len() > max_size {
                return Err(GrepError::Io(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!(
                        "file too large: {} bytes (limit: {} bytes)",
                        metadata.len(),
                        max_size
                    ),
                )));
            }
        }

        // Determine how to handle based on format
        let compression = if self.config.auto_decompress {
            path.compression
        } else {
            CompressionFormat::None
        };

        #[cfg(any(feature = "tar", feature = "zip"))]
        let archive = if self.config.auto_decompress {
            path.archive.clone()
        } else {
            ArchiveFormat::None
        };

        #[cfg(not(any(feature = "tar", feature = "zip")))]
        let archive = ();

        // Get filter pattern
        let filter = path
            .archive_filter
            .clone()
            .or_else(|| self.config.default_archive_filter.clone());

        // Create appropriate iterator
        #[cfg(any(feature = "tar", feature = "zip"))]
        match archive {
            ArchiveFormat::None => {
                // Plain file (possibly compressed)
                self.open_plain_file(fs_path, compression)
            }
            #[cfg(feature = "tar")]
            ArchiveFormat::Tar {
                compression: tar_compression,
            } => self.open_tar_archive(fs_path, tar_compression, filter),
            #[cfg(not(feature = "tar"))]
            ArchiveFormat::Tar { .. } => Err(GrepError::FeatureNotEnabled {
                feature: "tar".to_string(),
            }),
            #[cfg(feature = "zip")]
            ArchiveFormat::Zip => self.open_zip_archive(fs_path, filter),
            #[cfg(not(feature = "zip"))]
            ArchiveFormat::Zip => Err(GrepError::FeatureNotEnabled {
                feature: "zip".to_string(),
            }),
        }

        #[cfg(not(any(feature = "tar", feature = "zip")))]
        {
            let _ = (archive, filter);
            self.open_plain_file(fs_path, compression)
        }
    }

    /// Open a plain (non-archive) file.
    fn open_plain_file(
        &self,
        path: &Path,
        compression: CompressionFormat,
    ) -> GrepResult<GrepEntryIterator> {
        let file = File::open(path)?;
        let reader = BufReader::new(file);
        let decompressed = create_decompressor(reader, compression)?;

        // Read all content (streaming would require different architecture)
        let mut content = Vec::new();
        let mut reader = decompressed;
        reader.read_to_end(&mut content)?;

        // Try document extraction if enabled
        #[cfg(any(
            feature = "grep-pdf",
            feature = "grep-docx",
            feature = "grep-xlsx",
            feature = "grep-epub",
            feature = "grep-odt"
        ))]
        if self.config.extract_documents {
            let doc_format = document::detect_document_format(&content, Some(path));
            if doc_format != document::DocumentFormat::None {
                let extractor_config = document::DocumentExtractorConfig {
                    enable_ocr: self.config.enable_ocr,
                    ocr_language: self.config.ocr_language.clone(),
                    max_size: self.config.max_file_size,
                    skip_on_error: false,
                };
                let extractor = document::DocumentExtractor::with_config(extractor_config);
                let text = extractor.extract_format(&content, doc_format)?;
                content = text.into_bytes();
            }
        }

        // Check for binary content (skip if document extraction was performed)
        if self.config.skip_binary && compression::detection::is_binary(&content) {
            return Err(GrepError::BinaryFile(path.to_path_buf()));
        }

        let source_id = SourceId::file(path);
        let entries = vec![GrepEntry { source_id, content }];

        Ok(GrepEntryIterator {
            inner: GrepEntryIteratorInner::Single(entries.into_iter()),
        })
    }

    /// Open a tar archive.
    #[cfg(feature = "tar")]
    fn open_tar_archive(
        &self,
        path: &Path,
        compression: CompressionFormat,
        filter: Option<String>,
    ) -> GrepResult<GrepEntryIterator> {
        let mut reader = TarArchiveReader::open(path, compression)?;

        if let Some(pattern) = filter {
            reader = reader.with_pattern(&pattern)?;
        }

        let entries = reader.entries()?;

        // Collect entries (tar is streaming, but we need the content)
        let collected: Vec<GrepResult<GrepEntry>> = entries
            .map(|result| {
                result.map(|(source_id, _meta, content)| GrepEntry { source_id, content })
            })
            .collect();

        Ok(GrepEntryIterator {
            inner: GrepEntryIteratorInner::Archive(collected.into_iter()),
        })
    }

    /// Open a zip archive.
    #[cfg(feature = "zip")]
    fn open_zip_archive(
        &self,
        path: &Path,
        filter: Option<String>,
    ) -> GrepResult<GrepEntryIterator> {
        let mut reader = ZipArchiveReader::open(path)?;

        if let Some(pattern) = filter {
            reader = reader.with_pattern(&pattern)?;
        }

        let entries = reader.entries()?;

        // Collect entries
        let collected: Vec<GrepResult<GrepEntry>> = entries
            .map(|result| {
                result.map(|(source_id, _meta, content)| GrepEntry { source_id, content })
            })
            .collect();

        Ok(GrepEntryIterator {
            inner: GrepEntryIteratorInner::Archive(collected.into_iter()),
        })
    }

    /// Open a path string (convenience method that parses the path).
    pub fn open_str(&self, path: &str) -> GrepResult<GrepEntryIterator> {
        let grep_path = GrepPath::parse(path)?;
        self.open(&grep_path)
    }
}

impl Default for GrepSource {
    fn default() -> Self {
        Self::with_defaults()
    }
}

/// An entry from a grep source (a file or archive entry).
#[derive(Debug, Clone)]
pub struct GrepEntry {
    /// Identifies the source (file path, or archive + entry path).
    source_id: SourceId,

    /// The decompressed content.
    content: Vec<u8>,
}

impl GrepEntry {
    /// Get the source identifier.
    pub fn source_id(&self) -> &SourceId {
        &self.source_id
    }

    /// Get the content as bytes.
    pub fn content(&self) -> &[u8] {
        &self.content
    }

    /// Get the content as a UTF-8 string.
    ///
    /// Returns an error if the content is not valid UTF-8.
    pub fn content_str(&self) -> GrepResult<&str> {
        std::str::from_utf8(&self.content).map_err(|e| GrepError::InvalidUtf8 {
            file_path: self.source_id.display(),
            message: e.to_string(),
        })
    }

    /// Consume the entry and return the content.
    pub fn into_content(self) -> Vec<u8> {
        self.content
    }

    /// Check if this entry is from an archive.
    pub fn is_archive_entry(&self) -> bool {
        self.source_id.is_archive_entry()
    }
}

/// Iterator over grep entries from a source.
pub struct GrepEntryIterator {
    inner: GrepEntryIteratorInner,
}

enum GrepEntryIteratorInner {
    /// Single plain file entry.
    Single(std::vec::IntoIter<GrepEntry>),
    /// Archive entries (collected).
    Archive(std::vec::IntoIter<GrepResult<GrepEntry>>),
}

impl Iterator for GrepEntryIterator {
    type Item = GrepResult<GrepEntry>;

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.inner {
            GrepEntryIteratorInner::Single(iter) => iter.next().map(Ok),
            GrepEntryIteratorInner::Archive(iter) => iter.next(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_grep_config_default() {
        let config = GrepConfig::default();
        assert!(config.auto_decompress);
        assert!(config.skip_binary);
        assert_eq!(config.max_file_size, Some(100 * 1024 * 1024));
    }

    #[test]
    fn test_grep_config_builder() {
        let config = GrepConfig::new()
            .no_decompress()
            .no_size_limit()
            .skip_binary_files(false)
            .include_hidden_files(true);

        assert!(!config.auto_decompress);
        assert!(config.max_file_size.is_none());
        assert!(!config.skip_binary);
        assert!(config.include_hidden);
    }

    #[test]
    fn test_grep_entry() {
        let entry = GrepEntry {
            source_id: SourceId::file("test.txt"),
            content: b"Hello, World!".to_vec(),
        };

        assert_eq!(entry.content(), b"Hello, World!");
        assert_eq!(entry.content_str().expect("valid utf8"), "Hello, World!");
        assert!(!entry.is_archive_entry());
    }

    #[test]
    fn test_grep_entry_archive() {
        let entry = GrepEntry {
            source_id: SourceId::archive_entry("archive.tar.gz", "dir/file.txt"),
            content: b"test content".to_vec(),
        };

        assert!(entry.is_archive_entry());
        assert_eq!(entry.source_id().display(), "archive.tar.gz:dir/file.txt");
    }
}