src2md 0.1.8

Turn source code into a Markdown document with syntax highlighting, or extract it back.
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
use crate::writer::OUTPUT_MAGIC_BYTES;
use anyhow::Result;
use ignore::{DirEntry, WalkBuilder};
use log::{debug, trace, warn};
use memmap2::MmapOptions;
use std::collections::HashSet;
use std::ffi::OsStr;
use std::fs::File;
use std::path::{Path, PathBuf};

/// Lock file patterns that are excluded by default.
/// These files are typically auto-generated and not useful to include in documentation.
const LOCK_FILE_NAMES: &[&str] = &[
    "package-lock.json",
    "yarn.lock",
    "pnpm-lock.yaml",
    "bun.lockb",
    "Cargo.lock",
    "Gemfile.lock",
    "poetry.lock",
    "Pipfile.lock",
    "composer.lock",
    "mix.lock",
    "pubspec.lock",
    "flake.lock",
    "go.sum",
    "shrinkwrap.yaml",
];

/// File extensions that indicate lock files.
const LOCK_FILE_EXTENSIONS: &[&str] = &["lock", "lockb"];

/// Collects all files from the project root, applying ignore filters and specific path constraints.
///
/// # Arguments
///
/// * `project_root` - The root directory to start walking from
/// * `ignore_file` - Optional path to a custom ignore file (e.g., `.gitignore`, `.src2md.ignore`)
/// * `specific_paths` - If non-empty, only files within these paths are included
/// * `output_path` - Optional path to the output file being written (will be excluded to prevent race conditions)
/// * `extensions` - If non-empty, only files with these extensions are included
///
/// # Returns
///
/// A vector of `DirEntry` items representing all matching files.
///
/// # Default Exclusions
///
/// The following are always excluded:
/// - Hidden files and directories (starting with `.`)
/// - Lock files (package-lock.json, yarn.lock, Cargo.lock, etc.)
/// - The explicit `output_path` if provided
/// - Any file that starts with the src2md magic header
pub fn collect_files(
    project_root: &Path,
    ignore_file: Option<&PathBuf>,
    specific_paths: &HashSet<PathBuf>,
    output_path: Option<&PathBuf>,
    extensions: &HashSet<String>,
) -> Result<Vec<DirEntry>> {
    let mut builder = WalkBuilder::new(project_root);

    // Configure walker to skip hidden files/directories
    builder.hidden(true).ignore(false);

    // If a user-provided ignore file exists, use it
    if let Some(ignore_path) = ignore_file {
        debug!("Using ignore file: {}", ignore_path.display());
        builder.add_ignore(ignore_path);
    }

    // Canonicalize output path for reliable comparison
    let canonical_output = output_path.and_then(|p| p.canonicalize().ok());
    if let Some(ref out) = canonical_output {
        debug!("Excluding output file: {}", out.display());
    }

    if !extensions.is_empty() {
        debug!("Filtering by extensions: {:?}", extensions);
    }

    let walker = builder.build();
    let mut entries = Vec::new();
    let mut skipped_hidden = 0;
    let mut skipped_lock = 0;
    let mut skipped_outputs = 0;
    let mut skipped_extensions = 0;

    for result in walker {
        match result {
            Ok(entry) => {
                let path = entry.path();

                if !path.is_file() {
                    continue;
                }

                // Skip hidden files (the walker with hidden(true) should skip hidden dirs)
                if is_hidden(&entry) {
                    trace!("Skipping hidden file: {}", path.display());
                    skipped_hidden += 1;
                    continue;
                }

                // Skip lock files
                if is_lock_file(path) {
                    trace!("Skipping lock file: {}", path.display());
                    skipped_lock += 1;
                    continue;
                }

                // Check extension filter
                if !extensions.is_empty() && !has_matching_extension(path, extensions) {
                    trace!("Skipping file (extension filter): {}", path.display());
                    skipped_extensions += 1;
                    continue;
                }

                // Check specific paths filter
                if !specific_paths.is_empty() && !is_in_specific_paths(path, specific_paths) {
                    continue;
                }

                // Explicitly skip the output file by path (prevents race condition)
                if let Some(ref canonical_out) = canonical_output
                    && let Ok(canonical_path) = path.canonicalize()
                    && canonical_path == *canonical_out
                {
                    trace!("Skipping output file by path: {}", path.display());
                    skipped_outputs += 1;
                    continue;
                }

                // Check if file is a previous src2md output by reading its header
                if is_src2md_output(path) {
                    debug!("Skipping src2md output file: {}", path.display());
                    skipped_outputs += 1;
                    continue;
                }

                entries.push(entry);
            }
            Err(err) => {
                warn!("Error walking path: {err}");
            }
        }
    }

    // Log summary of skipped files
    if skipped_hidden > 0 {
        debug!("Skipped {} hidden file(s)", skipped_hidden);
    }
    if skipped_lock > 0 {
        debug!("Skipped {} lock file(s)", skipped_lock);
    }
    if skipped_outputs > 0 {
        debug!(
            "Skipped {} src2md output file(s) to prevent self-inclusion",
            skipped_outputs
        );
    }
    if skipped_extensions > 0 {
        debug!(
            "Skipped {} file(s) not matching extension filter",
            skipped_extensions
        );
    }

    debug!("Collected {} files", entries.len());
    Ok(entries)
}

/// Checks if a file is a src2md output by reading its magic header.
///
/// This uses memory-mapped I/O to efficiently read just the first few bytes
/// without loading the entire file into memory.
fn is_src2md_output(path: &Path) -> bool {
    let file = match File::open(path) {
        Ok(f) => f,
        Err(_) => return false,
    };

    // Get file metadata to check size
    let metadata = match file.metadata() {
        Ok(m) => m,
        Err(_) => return false,
    };

    // File must be at least as long as the magic header
    if metadata.len() < OUTPUT_MAGIC_BYTES.len() as u64 {
        return false;
    }

    // Memory-map just enough to check the header
    // SAFETY: We only read from the memory-mapped region and the file
    // remains open for the duration of the check.
    let mmap = match unsafe { MmapOptions::new().len(OUTPUT_MAGIC_BYTES.len()).map(&file) } {
        Ok(m) => m,
        Err(_) => return false,
    };

    mmap[..] == OUTPUT_MAGIC_BYTES[..]
}

/// Checks if a file itself is hidden (filename starts with a dot).
/// This only checks the filename, not the full path, since we filter
/// directories during the walk and shouldn't check parent directories
/// outside the project root.
fn is_hidden(entry: &DirEntry) -> bool {
    entry
        .file_name()
        .to_str()
        .is_some_and(|s| s.starts_with('.'))
}

/// Checks if a file is a lock file based on its name or extension.
fn is_lock_file(path: &Path) -> bool {
    let file_name = path.file_name().and_then(OsStr::to_str).unwrap_or("");

    // Check exact filename matches
    if LOCK_FILE_NAMES.contains(&file_name) {
        return true;
    }

    // Check extension
    if let Some(ext) = path.extension().and_then(OsStr::to_str)
        && LOCK_FILE_EXTENSIONS
            .iter()
            .any(|&e| e == ext.to_lowercase())
    {
        return true;
    }

    false
}

/// Checks if a file has an extension matching the provided set.
fn has_matching_extension(path: &Path, extensions: &HashSet<String>) -> bool {
    path.extension()
        .and_then(OsStr::to_str)
        .map(|ext| extensions.contains(&ext.to_lowercase()))
        .unwrap_or(false)
}

/// Checks if a given path is part of the explicitly included paths.
///
/// If the specific path is a file, it must match exactly.
/// If it's a directory, the path must be a descendant of that directory.
fn is_in_specific_paths(path: &Path, specific_paths: &HashSet<PathBuf>) -> bool {
    specific_paths.iter().any(|p| {
        if p.is_file() {
            path == p
        } else {
            path.starts_with(p)
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::writer::OUTPUT_MAGIC_HEADER;
    use std::fs;
    use std::io::Write;
    use tempfile::tempdir;

    #[test]
    fn test_collect_files_basic() -> Result<()> {
        let temp_dir = tempdir()?;
        let root = temp_dir.path();

        fs::write(root.join("file1.rs"), "// rust")?;
        fs::write(root.join("file2.txt"), "text")?;

        let entries = collect_files(root, None, &HashSet::new(), None, &HashSet::new())?;

        assert_eq!(entries.len(), 2);
        Ok(())
    }

    #[test]
    fn test_collect_files_ignores_hidden() -> Result<()> {
        let temp_dir = tempdir()?;
        let root = temp_dir.path();

        fs::write(root.join("visible.rs"), "// visible")?;
        fs::write(root.join(".hidden.rs"), "// hidden")?;

        let entries = collect_files(root, None, &HashSet::new(), None, &HashSet::new())?;

        assert_eq!(entries.len(), 1);
        assert!(entries[0].path().to_string_lossy().contains("visible"));
        Ok(())
    }

    #[test]
    fn test_collect_files_ignores_hidden_directories() -> Result<()> {
        let temp_dir = tempdir()?;
        let root = temp_dir.path();

        fs::write(root.join("visible.rs"), "// visible")?;

        // Create hidden directory with files
        let hidden_dir = root.join(".hidden");
        fs::create_dir_all(&hidden_dir)?;
        fs::write(hidden_dir.join("secret.rs"), "// secret")?;

        let entries = collect_files(root, None, &HashSet::new(), None, &HashSet::new())?;

        assert_eq!(entries.len(), 1);
        assert!(entries[0].path().to_string_lossy().contains("visible"));
        Ok(())
    }

    #[test]
    fn test_collect_files_ignores_lock_files() -> Result<()> {
        let temp_dir = tempdir()?;
        let root = temp_dir.path();

        fs::write(root.join("source.rs"), "// source")?;
        fs::write(root.join("package-lock.json"), "{}")?;
        fs::write(root.join("yarn.lock"), "")?;
        fs::write(root.join("Cargo.lock"), "")?;
        fs::write(root.join("something.lock"), "")?;

        let entries = collect_files(root, None, &HashSet::new(), None, &HashSet::new())?;

        assert_eq!(entries.len(), 1);
        assert!(entries[0].path().to_string_lossy().contains("source.rs"));
        Ok(())
    }

    #[test]
    fn test_collect_files_ignores_nested_lock_files() -> Result<()> {
        let temp_dir = tempdir()?;
        let root = temp_dir.path();

        fs::write(root.join("source.rs"), "// source")?;

        let subdir = root.join("packages/frontend");
        fs::create_dir_all(&subdir)?;
        fs::write(subdir.join("app.ts"), "// app")?;
        fs::write(subdir.join("package-lock.json"), "{}")?;

        let entries = collect_files(root, None, &HashSet::new(), None, &HashSet::new())?;

        assert_eq!(entries.len(), 2);
        let paths: Vec<_> = entries.iter().map(|e| e.path().to_path_buf()).collect();
        assert!(
            paths
                .iter()
                .any(|p| p.to_string_lossy().contains("source.rs"))
        );
        assert!(paths.iter().any(|p| p.to_string_lossy().contains("app.ts")));
        assert!(
            !paths
                .iter()
                .any(|p| p.to_string_lossy().contains("package-lock"))
        );
        Ok(())
    }

    #[test]
    fn test_collect_files_with_extension_filter() -> Result<()> {
        let temp_dir = tempdir()?;
        let root = temp_dir.path();

        fs::write(root.join("main.rs"), "// rust")?;
        fs::write(root.join("app.ts"), "// typescript")?;
        fs::write(root.join("style.css"), "/* css */")?;
        fs::write(root.join("readme.md"), "# readme")?;

        let mut extensions = HashSet::new();
        extensions.insert("rs".to_string());
        extensions.insert("ts".to_string());

        let entries = collect_files(root, None, &HashSet::new(), None, &extensions)?;

        assert_eq!(entries.len(), 2);
        let paths: Vec<_> = entries.iter().map(|e| e.path().to_path_buf()).collect();
        assert!(
            paths
                .iter()
                .any(|p| p.to_string_lossy().contains("main.rs"))
        );
        assert!(paths.iter().any(|p| p.to_string_lossy().contains("app.ts")));
        assert!(
            !paths
                .iter()
                .any(|p| p.to_string_lossy().contains("style.css"))
        );
        assert!(
            !paths
                .iter()
                .any(|p| p.to_string_lossy().contains("readme.md"))
        );
        Ok(())
    }

    #[test]
    fn test_collect_files_extension_filter_case_insensitive() -> Result<()> {
        let temp_dir = tempdir()?;
        let root = temp_dir.path();

        // Use different base names to avoid case-insensitive filesystem issues
        fs::write(root.join("uppercase.RS"), "// rust uppercase")?;
        fs::write(root.join("lowercase.rs"), "// rust lowercase")?;
        fs::write(root.join("mixed.Rs"), "// rust mixed")?;
        fs::write(root.join("other.txt"), "not included")?;

        let mut extensions = HashSet::new();
        extensions.insert("rs".to_string());

        let entries = collect_files(root, None, &HashSet::new(), None, &extensions)?;

        // Should include all .rs files regardless of extension case
        assert_eq!(entries.len(), 3);
        Ok(())
    }

    #[test]
    fn test_collect_files_with_specific_paths() -> Result<()> {
        let temp_dir = tempdir()?;
        let root = temp_dir.path();

        fs::write(root.join("included.rs"), "// included")?;
        fs::write(root.join("excluded.rs"), "// excluded")?;

        let mut specific = HashSet::new();
        specific.insert(root.join("included.rs"));

        let entries = collect_files(root, None, &specific, None, &HashSet::new())?;

        assert_eq!(entries.len(), 1);
        assert!(entries[0].path().to_string_lossy().contains("included"));
        Ok(())
    }

    #[test]
    fn test_collect_files_with_subdirectory() -> Result<()> {
        let temp_dir = tempdir()?;
        let root = temp_dir.path();

        let subdir = root.join("src");
        fs::create_dir_all(&subdir)?;
        fs::write(subdir.join("main.rs"), "// main")?;
        fs::write(root.join("other.rs"), "// other")?;

        let mut specific = HashSet::new();
        specific.insert(subdir);

        let entries = collect_files(root, None, &specific, None, &HashSet::new())?;

        assert_eq!(entries.len(), 1);
        assert!(entries[0].path().to_string_lossy().contains("main.rs"));
        Ok(())
    }

    #[test]
    fn test_collect_files_excludes_output_path() -> Result<()> {
        let temp_dir = tempdir()?;
        let root = temp_dir.path();

        fs::write(root.join("source.rs"), "// source")?;
        let output_path = root.join("output.md");
        fs::write(&output_path, "# Output")?;

        let entries = collect_files(
            root,
            None,
            &HashSet::new(),
            Some(&output_path),
            &HashSet::new(),
        )?;

        assert_eq!(entries.len(), 1);
        assert!(entries[0].path().to_string_lossy().contains("source.rs"));
        Ok(())
    }

    #[test]
    fn test_collect_files_excludes_src2md_output() -> Result<()> {
        let temp_dir = tempdir()?;
        let root = temp_dir.path();

        fs::write(root.join("source.rs"), "// source")?;

        // Create a file that looks like a previous src2md output
        let mut output_file = fs::File::create(root.join("previous_output.md"))?;
        output_file.write_all(OUTPUT_MAGIC_HEADER.as_bytes())?;
        output_file.write_all(b"\n## file.rs\n\n```rust\n// code\n```\n")?;

        let entries = collect_files(root, None, &HashSet::new(), None, &HashSet::new())?;

        assert_eq!(entries.len(), 1);
        assert!(entries[0].path().to_string_lossy().contains("source.rs"));
        Ok(())
    }

    #[test]
    fn test_collect_files_excludes_nested_src2md_outputs() -> Result<()> {
        let temp_dir = tempdir()?;
        let root = temp_dir.path();

        // Create source files
        fs::write(root.join("source.rs"), "// source")?;

        // Create nested directories with src2md outputs
        let docs_dir = root.join("docs");
        fs::create_dir_all(&docs_dir)?;
        fs::write(docs_dir.join("readme.md"), "# Readme")?;

        // Create a src2md output in the nested directory
        let mut output_file = fs::File::create(docs_dir.join("generated.md"))?;
        output_file.write_all(OUTPUT_MAGIC_HEADER.as_bytes())?;
        output_file.write_all(b"\n## nested/file.rs\n\n```rust\n// nested\n```\n")?;

        let entries = collect_files(root, None, &HashSet::new(), None, &HashSet::new())?;

        // Should include source.rs and readme.md but not generated.md
        assert_eq!(entries.len(), 2);
        let paths: Vec<_> = entries.iter().map(|e| e.path().to_path_buf()).collect();
        assert!(
            paths
                .iter()
                .any(|p| p.to_string_lossy().contains("source.rs"))
        );
        assert!(
            paths
                .iter()
                .any(|p| p.to_string_lossy().contains("readme.md"))
        );
        assert!(
            !paths
                .iter()
                .any(|p| p.to_string_lossy().contains("generated.md"))
        );
        Ok(())
    }

    #[test]
    fn test_is_src2md_output() -> Result<()> {
        let temp_dir = tempdir()?;
        let root = temp_dir.path();

        // File with magic header
        let mut output_file = fs::File::create(root.join("output.md"))?;
        output_file.write_all(OUTPUT_MAGIC_HEADER.as_bytes())?;
        output_file.write_all(b"content")?;
        drop(output_file);

        // Regular file
        fs::write(root.join("regular.md"), "# Regular markdown")?;

        // Empty file
        fs::write(root.join("empty.md"), "")?;

        // File with partial header
        fs::write(root.join("partial.md"), "<!-- src")?;

        assert!(is_src2md_output(&root.join("output.md")));
        assert!(!is_src2md_output(&root.join("regular.md")));
        assert!(!is_src2md_output(&root.join("empty.md")));
        assert!(!is_src2md_output(&root.join("partial.md")));

        Ok(())
    }

    #[test]
    fn test_is_lock_file() {
        assert!(is_lock_file(Path::new("package-lock.json")));
        assert!(is_lock_file(Path::new("yarn.lock")));
        assert!(is_lock_file(Path::new("Cargo.lock")));
        assert!(is_lock_file(Path::new("pnpm-lock.yaml")));
        assert!(is_lock_file(Path::new("something.lock")));
        assert!(is_lock_file(Path::new("/path/to/package-lock.json")));

        assert!(!is_lock_file(Path::new("main.rs")));
        assert!(!is_lock_file(Path::new("package.json")));
        assert!(!is_lock_file(Path::new("lockfile.txt")));
    }

    #[test]
    fn test_is_hidden() -> Result<()> {
        let temp_dir = tempdir()?;
        let root = temp_dir.path();

        // Create test files
        fs::write(root.join("visible.rs"), "// visible")?;
        fs::write(root.join(".hidden"), "secret")?;

        // Walk to get DirEntry objects
        let mut visible_found = false;
        let mut hidden_found = false;

        let walker = ignore::WalkBuilder::new(root).hidden(false).build();
        for entry in walker.flatten() {
            if entry.path().is_file() {
                let file_name = entry.file_name().to_string_lossy();
                if file_name == "visible.rs" {
                    assert!(!is_hidden(&entry));
                    visible_found = true;
                } else if file_name == ".hidden" {
                    assert!(is_hidden(&entry));
                    hidden_found = true;
                }
            }
        }

        assert!(visible_found, "visible.rs should be found");
        assert!(hidden_found, ".hidden should be found (with hidden=false)");

        Ok(())
    }

    #[test]
    fn test_has_matching_extension() {
        let mut extensions = HashSet::new();
        extensions.insert("rs".to_string());
        extensions.insert("ts".to_string());

        assert!(has_matching_extension(Path::new("main.rs"), &extensions));
        assert!(has_matching_extension(Path::new("app.ts"), &extensions));
        assert!(has_matching_extension(Path::new("FILE.RS"), &extensions)); // case insensitive

        assert!(!has_matching_extension(Path::new("style.css"), &extensions));
        assert!(!has_matching_extension(Path::new("README"), &extensions)); // no extension
    }
}