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! {
invalid_argument => InvalidArgument,
operation_failed => OperationFailed,
invariant => Invariant,
}