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
//! Error handling and the [Result] alias
//!
//! # See also
//!
//! [C API docs](https://pipewire.pages.freedesktop.org/wireplumber/c_api/wperror_api.html)

pub use crate::auto::LibraryErrorEnum;
#[doc(no_inline)]
pub use glib::Error;
use {
	crate::prelude::*,
	glib::{error::ErrorDomain, Quark},
	std::fmt::Display,
};

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

impl fmt::Display for LibraryErrorEnum {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		let name = match self {
			LibraryErrorEnum::Invariant => "invariant check failed",
			LibraryErrorEnum::InvalidArgument => "invalid argument",
			LibraryErrorEnum::OperationFailed => "operation failed",
			LibraryErrorEnum::__Unknown(error) => return write!(f, "error {error}"),
		};
		f.write_str(name)
	}
}

impl ErrorDomain for LibraryErrorEnum {
	#[doc(alias = "WP_DOMAIN_LIBRARY")]
	#[doc(alias = "wp_domain_library_quark")]
	fn domain() -> Quark {
		unsafe { from_glib(ffi::wp_domain_library_quark()) }
	}

	fn code(self) -> i32 {
		self.into_glib()
	}

	fn from(code: i32) -> Option<Self> {
		match code {
			ffi::WP_LIBRARY_ERROR_INVARIANT
			| ffi::WP_LIBRARY_ERROR_INVALID_ARGUMENT
			| ffi::WP_LIBRARY_ERROR_OPERATION_FAILED => unsafe { Some(from_glib(code)) },
			_ => None,
		}
	}
}

macro_rules! error_constructors {
	($(
		$(#[$meta:meta])*
		$f:ident => $var:ident,
	)*) => {
		$(
			$(#[$meta])*
			pub fn $f<E: Display>(err: E) -> Error {
				Error::new(LibraryErrorEnum::$var, &err.to_string())
			}
		)*
	};
}

error_constructors! {
	/// Wrap a new [Error] under the [LibraryErrorEnum::InvalidArgument] domain
	invalid_argument => InvalidArgument,
	/// Wrap a new [Error] under the [LibraryErrorEnum::OperationFailed] domain
	operation_failed => OperationFailed,
	/// Wrap a new [Error] under the [LibraryErrorEnum::Invariant] domain
	invariant => Invariant,
}