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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::char::TryFromCharError;
use std::ffi::NulError;
use std::fmt;
use std::num::TryFromIntError;

use crate::core;

pub struct Error {
	pub code: i32,
	pub message: String,
}

impl Error {
	#[inline]
	pub fn new(code: i32, message: impl Into<String>) -> Self {
		Self {
			code,
			message: message.into(),
		}
	}

	#[inline]
	pub fn code_as_enum(&self) -> Option<core::Code> {
		error_code_as_enum(self.code)
	}

	fn format_code(&self) -> String {
		if let Some(code) = self.code_as_enum() {
			format!("{code:?}, {}", self.code)
		} else {
			format!("{}", self.code)
		}
	}
}

impl fmt::Display for Error {
	#[inline]
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "{} (code: {})", self.message, self.format_code())
	}
}

impl fmt::Debug for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		f.debug_struct("Error")
			.field("code", &self.format_code())
			.field("message", &self.message)
			.finish()
	}
}

impl From<NulError> for Error {
	#[inline]
	fn from(_: NulError) -> Self {
		Self::new(core::StsBadArg, "Passed Rust string contains nul byte")
	}
}

impl From<TryFromCharError> for Error {
	#[inline]
	fn from(_: TryFromCharError) -> Self {
		Self::new(core::StsBadArg, "Passed Rust char can't be converted to C++ char")
	}
}

impl From<TryFromIntError> for Error {
	fn from(_: TryFromIntError) -> Self {
		Self::new(
			core::StsBadArg,
			"Passed slice argument is too long, likely length doesn't fit i32",
		)
	}
}

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

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

pub fn error_code_as_enum(code: i32) -> Option<core::Code> {
	match code {
		core::StsOk => Some(core::Code::StsOk),
		core::StsBackTrace => Some(core::Code::StsBackTrace),
		core::StsError => Some(core::Code::StsError),
		core::StsInternal => Some(core::Code::StsInternal),
		core::StsNoMem => Some(core::Code::StsNoMem),
		core::StsBadArg => Some(core::Code::StsBadArg),
		core::StsBadFunc => Some(core::Code::StsBadFunc),
		core::StsNoConv => Some(core::Code::StsNoConv),
		core::StsAutoTrace => Some(core::Code::StsAutoTrace),
		core::HeaderIsNull => Some(core::Code::HeaderIsNull),
		core::BadImageSize => Some(core::Code::BadImageSize),
		core::BadOffset => Some(core::Code::BadOffset),
		core::BadDataPtr => Some(core::Code::BadDataPtr),
		core::BadStep => Some(core::Code::BadStep),
		core::BadModelOrChSeq => Some(core::Code::BadModelOrChSeq),
		core::BadNumChannels => Some(core::Code::BadNumChannels),
		core::BadNumChannel1U => Some(core::Code::BadNumChannel1U),
		core::BadDepth => Some(core::Code::BadDepth),
		core::BadAlphaChannel => Some(core::Code::BadAlphaChannel),
		core::BadOrder => Some(core::Code::BadOrder),
		core::BadOrigin => Some(core::Code::BadOrigin),
		core::BadAlign => Some(core::Code::BadAlign),
		core::BadCallBack => Some(core::Code::BadCallBack),
		core::BadTileSize => Some(core::Code::BadTileSize),
		core::BadCOI => Some(core::Code::BadCOI),
		core::BadROISize => Some(core::Code::BadROISize),
		core::MaskIsTiled => Some(core::Code::MaskIsTiled),
		core::StsNullPtr => Some(core::Code::StsNullPtr),
		core::StsVecLengthErr => Some(core::Code::StsVecLengthErr),
		core::StsFilterStructContentErr => Some(core::Code::StsFilterStructContentErr),
		core::StsKernelStructContentErr => Some(core::Code::StsKernelStructContentErr),
		core::StsFilterOffsetErr => Some(core::Code::StsFilterOffsetErr),
		core::StsBadSize => Some(core::Code::StsBadSize),
		core::StsDivByZero => Some(core::Code::StsDivByZero),
		core::StsInplaceNotSupported => Some(core::Code::StsInplaceNotSupported),
		core::StsObjectNotFound => Some(core::Code::StsObjectNotFound),
		core::StsUnmatchedFormats => Some(core::Code::StsUnmatchedFormats),
		core::StsBadFlag => Some(core::Code::StsBadFlag),
		core::StsBadPoint => Some(core::Code::StsBadPoint),
		core::StsBadMask => Some(core::Code::StsBadMask),
		core::StsUnmatchedSizes => Some(core::Code::StsUnmatchedSizes),
		core::StsUnsupportedFormat => Some(core::Code::StsUnsupportedFormat),
		core::StsOutOfRange => Some(core::Code::StsOutOfRange),
		core::StsParseError => Some(core::Code::StsParseError),
		core::StsNotImplemented => Some(core::Code::StsNotImplemented),
		core::StsBadMemBlock => Some(core::Code::StsBadMemBlock),
		core::StsAssert => Some(core::Code::StsAssert),
		core::GpuNotSupported => Some(core::Code::GpuNotSupported),
		core::GpuApiCallError => Some(core::Code::GpuApiCallError),
		core::OpenGlNotSupported => Some(core::Code::OpenGlNotSupported),
		core::OpenGlApiCallError => Some(core::Code::OpenGlApiCallError),
		core::OpenCLApiCallError => Some(core::Code::OpenCLApiCallError),
		core::OpenCLDoubleNotSupported => Some(core::Code::OpenCLDoubleNotSupported),
		core::OpenCLInitError => Some(core::Code::OpenCLInitError),
		core::OpenCLNoAMDBlasFft => Some(core::Code::OpenCLNoAMDBlasFft),
		_ => None,
	}
}