docuparse 0.0.6

Document extraction and parsing - PDF text, rendering, and OCR
Documentation
use std::{
    fs::{self, File},
    io::Read,
    path::Path,
};

use crate::{
    pdf_document::LoadError,
    utils::{MAX_PDF_SIZE, MB, MIN_PDF_SIZE, PDF_MAGIC},
};

fn validate_metadata(meta: &fs::Metadata) -> Result<(), LoadError> {
    let size = meta.len();

    if size < MIN_PDF_SIZE {
        return Err(LoadError::TooSmall {
            size,
            min: MIN_PDF_SIZE,
        });
    }

    if size > MAX_PDF_SIZE {
        return Err(LoadError::FileTooLarge {
            size_mb: size / MB,
            max_mb: MAX_PDF_SIZE / MB,
        });
    }

    Ok(())
}

fn validate_magic_bytes(file: &mut File, path: &Path) -> Result<(), LoadError> {
    let mut header: [u8; 5] = [0u8; PDF_MAGIC.len()];

    file.read_exact(&mut header)
        .map_err(|source| LoadError::Io {
            path: path.to_path_buf(),
            source,
        })?;

    if header != *PDF_MAGIC {
        return Err(LoadError::InvalidMagicBytes);
    }

    Ok(())
}

pub(crate) fn validate_pdf(path: &Path) -> Result<File, LoadError> {
    let mut file = match File::open(path) {
        Ok(f) => f,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            return Err(LoadError::NotFound {
                path: path.to_path_buf(),
            });
        }
        Err(source) => {
            return Err(LoadError::Io {
                path: path.to_path_buf(),
                source,
            });
        }
    };

    let meta = file.metadata().map_err(|source| LoadError::Io {
        path: path.to_path_buf(),
        source,
    })?;

    if !meta.is_file() {
        return Err(LoadError::NotAFile {
            path: path.to_path_buf(),
        });
    }

    validate_metadata(&meta)?;
    validate_magic_bytes(&mut file, path)?;

    Ok(file)
}

#[cfg(test)]
mod tests {
    use std::{fs, io::Write};

    use rstest::{fixture, rstest};
    use tempfile::{NamedTempFile, TempDir};

    use super::*;

    #[fixture]
    fn valid_pdf() -> NamedTempFile {
        let mut f = NamedTempFile::new().expect("tempfile");
        f.write_all(PDF_MAGIC).expect("write magic");
        let padding = vec![0u8; (MIN_PDF_SIZE as usize).saturating_sub(PDF_MAGIC.len())];
        f.write_all(&padding).expect("write padding");
        f
    }

    #[fixture]
    fn non_pdf_file() -> NamedTempFile {
        let mut f = NamedTempFile::new().expect("tempfile");
        let mut content = vec![0u8; MIN_PDF_SIZE as usize];
        let fake_magic = b"NOT_PDF!";
        content[..fake_magic.len()].copy_from_slice(fake_magic);
        f.write_all(&content).expect("write");
        f
    }

    #[fixture]
    fn too_small_pdf() -> NamedTempFile {
        let mut f = NamedTempFile::new().expect("tempfile");
        f.write_all(PDF_MAGIC).expect("write magic");
        f
    }

    #[fixture]
    fn too_large_pdf() -> NamedTempFile {
        let mut f = NamedTempFile::new().expect("tempfile");
        f.write_all(PDF_MAGIC).expect("write magic");
        let overflow = vec![0u8; (MAX_PDF_SIZE as usize) + 1];
        f.write_all(&overflow).expect("write overflow");
        f
    }

    #[fixture]
    fn temp_dir() -> TempDir {
        tempfile::tempdir().expect("tempdir")
    }

    #[rstest]
    fn test_missing_path(temp_dir: TempDir) {
        let ghost = temp_dir.path().join("ghost_that_does_not_exist.pdf");
        let err = validate_pdf(&ghost).unwrap_err();
        assert!(
            matches!(err, LoadError::NotFound { .. }),
            "expected NotFound, got {err:?}"
        );
    }

    #[rstest]
    fn test_path_is_directory(temp_dir: TempDir) {
        let err = validate_pdf(temp_dir.path()).unwrap_err();
        assert!(
            matches!(err, LoadError::NotAFile { .. }),
            "expected NotAFile, got {err:?}"
        );
    }

    #[rstest]
    fn test_too_small(too_small_pdf: NamedTempFile) {
        let err = validate_pdf(too_small_pdf.path()).unwrap_err();
        assert!(
            matches!(err, LoadError::TooSmall { .. }),
            "expected TooSmall, got {err:?}"
        );
    }

    #[rstest]
    fn test_too_large(too_large_pdf: NamedTempFile) {
        let err = validate_pdf(too_large_pdf.path()).unwrap_err();

        let LoadError::FileTooLarge { size_mb: _, max_mb } = err else {
            panic!("expected FileTooLarge, got {err:?}");
        };

        assert_eq!(max_mb, MAX_PDF_SIZE / MB);
    }

    #[rstest]
    fn test_wrong_magic(non_pdf_file: NamedTempFile) {
        let err = validate_pdf(non_pdf_file.path()).unwrap_err();
        assert!(
            matches!(err, LoadError::InvalidMagicBytes),
            "expected InvalidMagicBytes, got {err:?}"
        );
    }

    #[rstest]
    fn test_wrong_magic_at_minimum_size(temp_dir: TempDir) {
        // Pins validation order: size passes, magic fires.
        // If someone reorders the checks, this test breaks.
        let path = temp_dir.path().join("wrong_magic_min_size.pdf");
        let mut content = b"NOT_PDF!".to_vec();
        content.resize(MIN_PDF_SIZE as usize, 0);
        fs::write(&path, &content).expect("write");

        let err = validate_pdf(&path).unwrap_err();
        assert!(
            matches!(err, LoadError::InvalidMagicBytes),
            "expected InvalidMagicBytes on min-size file with wrong magic, got {err:?}"
        );
    }

    #[rstest]
    fn test_empty_file(temp_dir: TempDir) {
        // Empty file fails TooSmall before reaching magic byte validation -
        // this is the correct pipeline order.
        let empty = temp_dir.path().join("empty.pdf");
        fs::write(&empty, b"").expect("write empty");
        let err = validate_pdf(&empty).unwrap_err();
        assert!(
            matches!(err, LoadError::TooSmall { .. }),
            "expected TooSmall on empty file, got {err:?}"
        );
    }

    #[rstest]
    fn test_validate_pdf_happy_path(valid_pdf: NamedTempFile) {
        assert!(validate_pdf(valid_pdf.path()).is_ok());
    }

    #[rstest]
    #[case(MIN_PDF_SIZE - 1, true)]
    #[case(MIN_PDF_SIZE, false)]
    #[case(MIN_PDF_SIZE + 1, false)]
    fn test_min_size_boundary(#[case] size: u64, #[case] expect_err: bool, temp_dir: TempDir) {
        let path = temp_dir.path().join("boundary.pdf");
        let mut content = PDF_MAGIC.to_vec();
        let body_len = (size as usize).saturating_sub(PDF_MAGIC.len());
        content.extend(vec![0u8; body_len]);
        content.truncate(size as usize);
        fs::write(&path, &content).expect("write boundary file");

        let result = validate_pdf(&path);
        assert_eq!(
            result.is_err(),
            expect_err,
            "size={size}, expect_err={expect_err}, got={result:?}"
        );
    }

    #[rstest]
    #[case(MAX_PDF_SIZE - 1, false)]
    #[case(MAX_PDF_SIZE, false)]
    #[case(MAX_PDF_SIZE + 1, true)]
    fn test_max_size_boundary(#[case] size: u64, #[case] expect_err: bool, temp_dir: TempDir) {
        let path = temp_dir.path().join("max_boundary.pdf");
        let mut content = PDF_MAGIC.to_vec();
        let body_len = (size as usize).saturating_sub(PDF_MAGIC.len());
        content.extend(vec![0u8; body_len]);
        fs::write(&path, &content).expect("write max boundary file");

        let result = validate_pdf(&path);
        assert_eq!(
            result.is_err(),
            expect_err,
            "size={size}, expect_err={expect_err}, got={result:?}"
        );
    }
}