rs_vips/
error.rs

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