rs_vips/
error.rs

1// (c) Copyright 2025 mrdkprj
2use crate::bindings::vips_error_buffer;
3use std::ffi::CStr;
4
5#[derive(Debug)]
6pub enum Error {
7    InitializationError(String),
8    IOError(String),
9    OperationError(String),
10}
11
12impl std::fmt::Display for Error {
13    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14        match self {
15            Error::InitializationError(msg) => {
16                write!(
17                    f,
18                    "vips error: InitializationError - {}",
19                    msg
20                )
21            }
22            Error::OperationError(msg) => write!(
23                f,
24                "vips error: OperationError - {}",
25                msg
26            ),
27            Error::IOError(msg) => write!(
28                f,
29                "vips error: IOError - {}",
30                msg
31            ),
32        }
33    }
34}
35
36impl Error {
37    pub(crate) fn extend(self) -> Self {
38        let erro_buffer = unsafe { vips_error_buffer() };
39        if erro_buffer.is_null() {
40            return self;
41        }
42
43        if let Ok(detail) = unsafe { CStr::from_ptr(erro_buffer).to_str() } {
44            match self {
45                Error::InitializationError(msg) => Error::InitializationError(format!(
46                    "{}. {}",
47                    msg, detail
48                )),
49                Error::OperationError(msg) => Error::OperationError(format!(
50                    "{}. {}",
51                    msg, detail
52                )),
53                Error::IOError(msg) => Error::IOError(format!(
54                    "{}. {}",
55                    msg, detail
56                )),
57            }
58        } else {
59            self
60        }
61    }
62}
63
64impl std::error::Error for Error {}