1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use ofx_sys::*;
use std::fmt;
use std::fmt::Display;
use types::*;

pub use ofx_sys::eOfxStatus_ErrBadHandle;
pub use ofx_sys::eOfxStatus_ErrBadIndex;
pub use ofx_sys::eOfxStatus_ErrValue;
pub use ofx_sys::eOfxStatus_ReplyDefault;
pub use ofx_sys::eOfxStatus_OK;

#[derive(Debug, Clone, Copy)]
pub enum Error {
	PluginNotFound,
	InvalidAction,
	InvalidImageEffectAction,
	InvalidNameEncoding,
	InvalidResultEncoding,
	InvalidHandle,
	InvalidValue,
	InvalidSuite,
	InvalidIndex,
	PluginNotReady,
	PropertyIndexOutOfBounds,
	HostNotReady,
	EnumNotFound,
	SuiteNotInitialized,
	Unimplemented,
	UnknownError,
}

pub const OK: Result<Int> = Ok(eOfxStatus_OK);
pub const REPLY_DEFAULT: Result<Int> = Ok(eOfxStatus_ReplyDefault);
pub const FAILED: Result<Int> = Ok(eOfxStatus_Failed);
pub const UNIMPLEMENTED: Result<Int> = Err(Error::Unimplemented);

impl From<OfxStatus> for Error {
	fn from(status: OfxStatus) -> Error {
		match status {
			ofx_sys::eOfxStatus_ErrBadHandle => Error::InvalidHandle,
			ofx_sys::eOfxStatus_ErrBadIndex => Error::InvalidIndex,
			ofx_sys::eOfxStatus_ErrValue => Error::InvalidValue,
			_ => Error::UnknownError,
		}
	}
}

macro_rules! to_result {
	{$ofx_status:expr => $result:expr} => {
		match $ofx_status {
			ofx_sys::eOfxStatus_OK => Ok($result),
			other => Err(Error::from(other)),
		}
	};
	($ofx_status:expr) => {
		to_result!($ofx_status => ())
	};
}

impl From<std::ffi::NulError> for Error {
	fn from(_src: std::ffi::NulError) -> Error {
		Error::InvalidNameEncoding
	}
}

impl From<std::ffi::IntoStringError> for Error {
	fn from(_src: std::ffi::IntoStringError) -> Error {
		Error::InvalidNameEncoding
	}
}

impl From<std::ffi::FromBytesWithNulError> for Error {
	fn from(_src: std::ffi::FromBytesWithNulError) -> Error {
		Error::InvalidResultEncoding
	}
}

impl From<std::str::Utf8Error> for Error {
	fn from(_src: std::str::Utf8Error) -> Error {
		Error::InvalidResultEncoding
	}
}

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "Openfx error")
	}
}

impl std::error::Error for Error {}

pub type Result<T> = std::result::Result<T, Error>;