dircat 1.0.1

High-performance Rust utility that concatenates and displays directory contents, similar to the C++ DirCat.
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
mod common;

use dircat::config::{self, ConfigBuilder, ResolvedInput};
use dircat::core_types::FileInfo;
use dircat::errors::Error;
use dircat::{discover, process_files, CancellationToken, Config};
use std::fs;
use std::path::{Path, PathBuf};
use tempfile::{tempdir, TempDir};

use dircat::core_types::FileContent;
// --- Test Harness for reducing boilerplate ---

/// A helper struct to manage the environment for a single library API test.
struct TestHarness {
    _temp_dir: TempDir,
    root: PathBuf,
    token: CancellationToken,
}

impl TestHarness {
    /// Creates a new test harness with a temporary directory.
    fn new() -> Self {
        let temp_dir = tempdir().unwrap();
        let root = temp_dir.path().to_path_buf();
        Self {
            _temp_dir: temp_dir,
            root,
            token: CancellationToken::new(),
        }
    }

    /// Creates a file with content within the harness's temporary directory.
    fn file(&self, path: &str, content: &[u8]) {
        let full_path = self.root.join(path);
        fs::create_dir_all(full_path.parent().unwrap()).unwrap();
        fs::write(full_path, content).unwrap();
    }

    /// Creates a new ConfigBuilder with the input path set to the harness root.
    fn builder(&self) -> ConfigBuilder {
        ConfigBuilder::new().input_path(self.root.to_str().unwrap())
    }
}

/// Helper to build the config and resolve the input path from a builder.
fn build_and_resolve(builder: ConfigBuilder) -> (Config, ResolvedInput) {
    let config = builder.build().unwrap();
    let resolved = config::resolve_input(&config.input_path, &None, None, &None, None).unwrap();
    (config, resolved)
}

/// Helper to create a basic FileInfo for test inputs.
fn create_test_file_info(root: &Path, relative_path: &str) -> FileInfo {
    FileInfo {
        absolute_path: root.join(relative_path),
        relative_path: relative_path.into(),
        ..Default::default()
    }
}

// --- Tests ---

#[test]
fn test_discover_returns_sorted_iterator() -> anyhow::Result<()> {
    let harness = TestHarness::new();
    harness.file("c.txt", b"C");
    harness.file("a.rs", b"A");
    harness.file("b.md", b"B");

    let builder = harness.builder().process_last(vec!["*.md".to_string()]);
    let (config, resolved) = build_and_resolve(builder);

    let discovered_paths: Vec<String> = discover(&config.discovery, &resolved, &harness.token)?
        .map(|fi| fi.relative_path.to_string_lossy().to_string())
        .collect();

    assert_eq!(
        discovered_paths,
        vec!["a.rs", "c.txt", "b.md"] // Alphabetical, then --last
    );

    Ok(())
}

#[test]
fn test_discover_iterator_with_complex_filters() -> anyhow::Result<()> {
    let harness = TestHarness::new();
    harness.file("src/main.rs", b"main");
    harness.file("src/lib.rs", b"lib");
    harness.file("tests/integration.rs", b"test");
    harness.file("docs/guide.md", b"docs");

    let builder = harness
        .builder()
        .extensions(vec!["rs".to_string()]) // Only .rs files
        .exclude_path_regex(vec!["^tests/".to_string()]); // But exclude the tests dir

    let (config, resolved) = build_and_resolve(builder);

    let discovered_paths: Vec<String> = discover(&config.discovery, &resolved, &harness.token)?
        .map(|fi| fi.relative_path.to_string_lossy().replace('\\', "/"))
        .collect();

    assert_eq!(discovered_paths, vec!["src/lib.rs", "src/main.rs"]); // Sorted

    Ok(())
}

#[test]
fn test_discover_with_no_matching_files() -> anyhow::Result<()> {
    let harness = TestHarness::new();
    harness.file("a.log", b"log");

    let builder = harness.builder().extensions(vec!["rs".to_string()]); // Filter for something that doesn't exist
    let (config, resolved) = build_and_resolve(builder);

    let discovered_files: Vec<_> =
        discover(&config.discovery, &resolved, &harness.token)?.collect();
    assert!(discovered_files.is_empty());

    Ok(())
}

#[test]
fn test_process_iterator_reads_and_filters_content() -> anyhow::Result<()> {
    let harness = TestHarness::new();
    harness.file("a.rs", b"// comment\nfn main() {}");
    harness.file("b.txt", b"Hello");
    harness.file("c.bin", b"binary\0data");

    let files_to_process = vec![
        create_test_file_info(&harness.root, "a.rs"),
        create_test_file_info(&harness.root, "b.txt"),
        create_test_file_info(&harness.root, "c.bin"),
    ];

    let builder = harness.builder().remove_comments(true);
    let (config, _) = build_and_resolve(builder);

    let successful_files: Vec<FileInfo> = process_files(
        files_to_process.into_iter(),
        &config.processing,
        &harness.token,
    )
    .collect::<Result<Vec<_>, Error>>()?;

    assert_eq!(successful_files.len(), 2); // Binary file was filtered out

    let file_a = successful_files
        .iter()
        .find(|fi| fi.relative_path.to_str() == Some("a.rs"))
        .unwrap();
    let file_b = successful_files
        .iter()
        .find(|fi| fi.relative_path.to_str() == Some("b.txt"))
        .unwrap();

    assert_eq!(file_a.processed_content, Some("fn main() {}".to_string()));
    assert_eq!(file_b.processed_content, Some("Hello".to_string()));

    Ok(())
}

#[test]
fn test_process_iterator_includes_binary_when_configured() -> anyhow::Result<()> {
    let harness = TestHarness::new();
    harness.file("data.bin", b"binary\0data");

    let files_to_process = vec![create_test_file_info(&harness.root, "data.bin")];

    let builder = harness.builder().include_binary(true);
    let (config, _) = build_and_resolve(builder);

    let successful_files: Vec<FileInfo> = process_files(
        files_to_process.into_iter(),
        &config.processing,
        &harness.token,
    )
    .collect::<Result<Vec<_>, Error>>()?;

    assert_eq!(successful_files.len(), 1);
    let binary_file = &successful_files[0];
    assert!(binary_file.is_binary);
    assert_eq!(
        binary_file.processed_content,
        Some(String::from_utf8_lossy(b"binary\0data").to_string())
    );

    Ok(())
}

#[test]
fn test_process_iterator_handles_io_error() -> anyhow::Result<()> {
    let harness = TestHarness::new();
    harness.file("a.txt", b"Content A");
    harness.file("c.txt", b"Content C");

    let files_to_process = vec![
        create_test_file_info(&harness.root, "a.txt"),
        create_test_file_info(&harness.root, "non_existent.txt"),
        create_test_file_info(&harness.root, "c.txt"),
    ];

    let (config, _) = build_and_resolve(harness.builder());
    let processed_iter = process_files(
        files_to_process.into_iter(),
        &config.processing,
        &harness.token,
    );

    // With parallel processing, order is not guaranteed. Collect all results and inspect.
    let results: Vec<_> = processed_iter.collect();
    assert_eq!(results.len(), 3);

    let mut ok_count = 0;
    let mut err_count = 0;
    let mut ok_paths = std::collections::HashSet::new();

    for result in results {
        match result {
            Ok(fi) => {
                ok_count += 1;
                ok_paths.insert(fi.relative_path.to_string_lossy().to_string());
            }
            Err(Error::Io { .. }) => {
                err_count += 1;
            }
            Err(e) => panic!("Unexpected error type: {:?}", e),
        }
    }

    assert_eq!(ok_count, 2);
    assert_eq!(err_count, 1);
    assert!(ok_paths.contains("a.txt"));
    assert!(ok_paths.contains("c.txt"));

    Ok(())
}

#[test]
fn test_process_content_decoupled_from_io() -> anyhow::Result<()> {
    let harness = TestHarness::new();
    // No files are actually created on the filesystem.

    let files_content = vec![
        FileContent {
            relative_path: "a.rs".into(),
            content: b"// comment\nfn main() {}".to_vec(),
            ..Default::default()
        },
        FileContent {
            relative_path: "b.txt".into(),
            content: b"Hello".to_vec(),
            ..Default::default()
        },
        FileContent {
            relative_path: "c.bin".into(),
            content: b"binary\0data".to_vec(),
            ..Default::default()
        },
    ];

    let builder = harness.builder().remove_comments(true);
    let (config, _) = build_and_resolve(builder);
    let opts = dircat::processing::ProcessingOptions::from(&config);

    let successful_files: Vec<FileInfo> =
        dircat::process_content(files_content.into_iter(), opts, &harness.token)
            .collect::<Result<Vec<_>, Error>>()?;

    assert_eq!(successful_files.len(), 2); // Binary file was filtered out

    let file_a = successful_files
        .iter()
        .find(|fi| fi.relative_path.to_str() == Some("a.rs"))
        .unwrap();
    let file_b = successful_files
        .iter()
        .find(|fi| fi.relative_path.to_str() == Some("b.txt"))
        .unwrap();

    assert_eq!(file_a.processed_content, Some("fn main() {}".to_string()));
    assert_eq!(file_b.processed_content, Some("Hello".to_string()));

    Ok(())
}

#[test]
fn test_process_iterator_handles_cancellation() {
    let harness = TestHarness::new();
    harness.file("a.txt", b"A");
    harness.file("b.txt", b"B");

    let files_to_process = vec![
        create_test_file_info(&harness.root, "a.txt"),
        create_test_file_info(&harness.root, "b.txt"),
    ];

    let (config, _) = build_and_resolve(harness.builder());
    harness.token.cancel(); // Cancel *before* processing

    let results: Vec<_> = process_files(
        files_to_process.into_iter(),
        &config.processing,
        &harness.token,
    )
    .collect();

    // With parallel execution, we might get one or more Interrupted errors.
    // We should check that at least one is present.
    assert!(!results.is_empty());
    assert!(results.iter().any(|r| matches!(r, Err(Error::Interrupted))));
}

#[test]
fn test_process_with_empty_iterator() -> anyhow::Result<()> {
    let harness = TestHarness::new();
    let (config, _) = build_and_resolve(harness.builder());
    let files_to_process: Vec<FileInfo> = vec![];

    let processed_files: Vec<_> = process_files(
        files_to_process.into_iter(),
        &config.processing,
        &harness.token,
    )
    .collect::<Result<Vec<_>, Error>>()?;

    assert!(processed_files.is_empty());
    Ok(())
}

#[test]
fn test_discover_and_process_chaining() -> anyhow::Result<()> {
    let harness = TestHarness::new();
    harness.file("src/main.rs", b"// main\nfn main(){}");
    harness.file("src/data.bin", b"\0\x01\x02");
    harness.file("README.md", b"# Project");
    harness.file(".gitignore", b"*.log");
    harness.file("debug.log", b"Log data");

    let builder = harness
        .builder()
        .remove_comments(true)
        .process_last(vec!["README.md".to_string()]);
    let (config, resolved) = build_and_resolve(builder);

    // --- The Streaming Pipeline ---
    let discovered_iter = discover(&config.discovery, &resolved, &harness.token)?;
    let processed_iter = process_files(discovered_iter, &config.processing, &harness.token);
    let mut final_files: Vec<FileInfo> = processed_iter.collect::<Result<Vec<_>, Error>>()?;
    // -----------------------------

    // Re-sort the files after parallel processing, which does not preserve order.
    final_files.sort_by_key(|fi| {
        (
            fi.is_process_last,
            fi.process_last_order,
            fi.relative_path.clone(),
        )
    });

    assert_eq!(final_files.len(), 2);

    assert_eq!(
        final_files[0]
            .relative_path
            .to_string_lossy()
            .replace('\\', "/"),
        "src/main.rs"
    );
    assert_eq!(
        final_files[0].processed_content,
        Some("fn main(){}".to_string())
    );

    assert_eq!(
        final_files[1]
            .relative_path
            .to_string_lossy()
            .replace('\\', "/"),
        "README.md"
    );
    assert_eq!(
        final_files[1].processed_content,
        Some("# Project".to_string())
    );
    assert!(final_files[1].is_process_last);

    Ok(())
}

#[cfg(feature = "git")]
mod git_feature_tests {
    use super::*;
    use dircat::output::OutputFormatter;

    // A custom formatter for testing purposes that relies on serde_json from the 'git' feature
    struct JsonFormatter;
    impl OutputFormatter for JsonFormatter {
        fn format(
            &self,
            files: &[FileInfo],
            _opts: &dircat::OutputConfig,
            writer: &mut dyn std::io::Write,
        ) -> anyhow::Result<()> {
            // serde_json is available because the tests run with the 'git' feature enabled
            let paths: Vec<_> = files
                .iter()
                .map(|f| f.relative_path.to_string_lossy())
                .collect();
            let json = ::serde_json::json!({ "files": paths });
            write!(writer, "{}", json)?;
            Ok(())
        }
        fn format_dry_run(
            &self,
            files: &[FileInfo],
            _opts: &dircat::OutputConfig,
            writer: &mut dyn std::io::Write,
        ) -> anyhow::Result<()> {
            let paths: Vec<_> = files
                .iter()
                .map(|f| f.relative_path.to_string_lossy())
                .collect();
            let json = ::serde_json::json!({ "dry_run_files": paths });
            write!(writer, "{}", json)?;
            Ok(())
        }
    }

    #[test]
    fn test_dircat_result_format_with_custom_formatter() -> anyhow::Result<()> {
        let harness = TestHarness::new();
        harness.file("a.rs", b"A");
        harness.file("b.txt", b"B");

        let builder = harness.builder();
        let (config, _resolved) = build_and_resolve(builder);

        // Use the full pipeline via dircat::execute
        let result = dircat::execute(&config, &harness.token, None)?;

        // Format using the custom JSON formatter
        let mut buffer = Vec::new();
        let output_config = dircat::OutputConfig::from(&config);
        result.format_with(&JsonFormatter, &output_config, &mut buffer)?;

        let output_str = String::from_utf8(buffer)?;
        // The order is deterministic because `execute` sorts the files.
        let expected_json = r#"{"files":["a.rs","b.txt"]}"#;
        assert_eq!(output_str, expected_json);

        Ok(())
    }
}