join_ai/
processor.rs

1use std::fs::{self, File};
2use std::io::{self, Write};
3use std::path::PathBuf;
4use std::sync::mpsc;
5
6/// Processes file paths received from a channel, concatenating their content into an output file.
7pub fn process_files(
8    rx: mpsc::Receiver<PathBuf>,
9    output_file_path: &PathBuf,
10) -> anyhow::Result<()> {
11    let mut output_file = File::create(output_file_path)?;
12
13    for path in rx {
14        match fs::read(&path) {
15            Ok(contents) => {
16                // A robust way to detect binary files is to check for the NUL byte.
17                if contents.contains(&0) {
18                    println!("Skipping binary file: {}", path.display());
19                    continue;
20                }
21
22                // Write the header and file content
23                writeln!(output_file, "// FILE: {}", path.display())?;
24                output_file.write_all(&contents)?;
25                writeln!(output_file)?;
26            }
27            Err(e) => {
28                // Ignore errors from special files that can't be read (e.g., pipes)
29                if e.kind() != io::ErrorKind::InvalidData {
30                    eprintln!("Failed to read file {}: {}", path.display(), e);
31                }
32            }
33        }
34    }
35
36    Ok(())
37}