micropdf 0.17.0

A pure Rust PDF library - A pure Rust PDF library with fz_/pdf_ API compatibility
//! Error handling — compatibility shim.
//!
//! `Error` is a re-export of `MpError`; all logic lives in `crate::micropdf::error`.

pub use crate::micropdf::error::MpError as Error;
pub use crate::micropdf::error::MpResult as Result;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_error_generic() {
        let e = Error::generic("test error");
        assert!(matches!(e, Error::Generic(_)));
        assert_eq!(format!("{}", e), "test error");
    }

    #[test]
    fn test_error_argument() {
        let e = Error::argument("bad argument");
        assert!(matches!(e, Error::Argument(_)));
        assert!(format!("{}", e).contains("bad argument"));
    }

    #[test]
    fn test_error_limit() {
        let e = Error::limit("size exceeded");
        assert!(matches!(e, Error::Limit(_)));
        assert!(format!("{}", e).contains("size exceeded"));
    }

    #[test]
    fn test_error_unsupported() {
        let e = Error::unsupported("feature not supported");
        assert!(matches!(e, Error::Unsupported(_)));
        assert!(format!("{}", e).contains("feature not supported"));
    }

    #[test]
    fn test_error_format() {
        let e = Error::format("invalid format");
        assert!(matches!(e, Error::Format(_)));
        assert!(format!("{}", e).contains("invalid format"));
    }

    #[test]
    fn test_error_syntax() {
        let e = Error::syntax("syntax error at line 5");
        assert!(matches!(e, Error::Syntax(_)));
        assert!(format!("{}", e).contains("syntax error"));
    }

    #[test]
    fn test_error_pdf() {
        let e = Error::pdf("invalid PDF structure");
        assert!(matches!(e, Error::Pdf(_)));
        assert!(format!("{}", e).contains("invalid PDF"));
    }

    #[test]
    fn test_error_encryption() {
        let e = Error::encryption("wrong password");
        assert!(matches!(e, Error::Encryption(_)));
        assert!(format!("{}", e).contains("wrong password"));
    }

    #[test]
    fn test_error_font() {
        let e = Error::font("font not found");
        assert!(matches!(e, Error::Font(_)));
        assert!(format!("{}", e).contains("font not found"));
    }

    #[test]
    fn test_error_image() {
        let e = Error::image("corrupted image");
        assert!(matches!(e, Error::Image(_)));
        assert!(format!("{}", e).contains("corrupted image"));
    }

    #[test]
    fn test_error_eof() {
        let e = Error::Eof;
        assert!(matches!(e, Error::Eof));
        assert!(format!("{}", e).contains("end of file"));
    }

    #[test]
    fn test_error_abort() {
        let e = Error::Abort;
        assert!(matches!(e, Error::Abort));
        assert!(format!("{}", e).contains("aborted"));
    }

    #[test]
    fn test_error_from_io_error() {
        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
        let e: Error = io_err.into();
        assert!(matches!(e, Error::System(_)));
        assert!(format!("{}", e).contains("file not found"));
    }

    #[test]
    fn test_error_debug() {
        let e = Error::generic("test");
        let debug = format!("{:?}", e);
        assert!(debug.contains("Generic"));
    }

    #[test]
    fn test_result_type() {
        fn returns_ok() -> Result<i32> {
            Ok(42)
        }

        fn returns_err() -> Result<i32> {
            Err(Error::generic("error"))
        }

        assert_eq!(returns_ok().unwrap(), 42);
        assert!(returns_err().is_err());
    }

    #[test]
    fn test_error_constructors_with_string() {
        assert!(matches!(
            Error::generic(String::from("g")),
            Error::Generic(_)
        ));
        assert!(matches!(
            Error::argument(String::from("a")),
            Error::Argument(_)
        ));
        assert!(matches!(Error::limit(String::from("l")), Error::Limit(_)));
        assert!(matches!(
            Error::unsupported(String::from("u")),
            Error::Unsupported(_)
        ));
        assert!(matches!(Error::format(String::from("f")), Error::Format(_)));
        assert!(matches!(Error::syntax(String::from("s")), Error::Syntax(_)));
        assert!(matches!(Error::pdf(String::from("p")), Error::Pdf(_)));
        assert!(matches!(
            Error::encryption(String::from("e")),
            Error::Encryption(_)
        ));
        assert!(matches!(Error::font(String::from("fo")), Error::Font(_)));
        assert!(matches!(Error::image(String::from("i")), Error::Image(_)));
    }
}