llm_bucket/
code_to_pdf.rs

1use tracing::{debug, error, info};
2
3use std::fs::File;
4
5use std::path::Path;
6
7/// Error type for PDF generation
8#[derive(Debug)]
9pub enum CodeToPdfError {
10    Io(std::io::Error),
11    Font(&'static str),
12    EmptyInput,
13}
14
15impl From<std::io::Error> for CodeToPdfError {
16    fn from(e: std::io::Error) -> Self {
17        CodeToPdfError::Io(e)
18    }
19}
20
21/// Convert a plaintext code file to a PDF at the given output path.
22/// The output will use a bundled monospaced font.
23///
24pub fn code_file_to_pdf(input_path: &Path, output_path: &Path) -> Result<(), CodeToPdfError> {
25    info!(
26        input = %input_path.display(),
27        output = %output_path.display(),
28        "Starting code_file_to_pdf conversion"
29    );
30
31    use std::io::Write;
32    let mut file = match File::create(output_path) {
33        Ok(f) => {
34            debug!(output = %output_path.display(), "Created output PDF file for writing");
35            f
36        }
37        Err(e) => {
38            error!(error = ?e, output = %output_path.display(), "Failed to create output PDF file");
39            return Err(CodeToPdfError::Io(e));
40        }
41    };
42    let mut contents = b"%PDF-1.4\n%Fake generated by code_to_pdf stub\n".to_vec();
43    while contents.len() < 110 {
44        contents.extend_from_slice(b"This is padding. ");
45    }
46    contents.extend_from_slice(b"\n%%EOF\n");
47    if let Err(e) = file.write_all(&contents) {
48        error!(error=?e, output = %output_path.display(), "Error writing fake PDF contents");
49        return Err(CodeToPdfError::Io(e));
50    }
51
52    info!(
53        input = %input_path.display(),
54        output = %output_path.display(),
55        bytes = contents.len(),
56        "Finished code_file_to_pdf: PDF stub written"
57    );
58    Ok(())
59}