use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone)]
pub struct Error {
raw_code: Option<i64>,
raw_os_message: Option<String>,
kind: ErrorKind,
}
impl Error {
#[allow(dead_code)]
pub(crate) fn new(
raw_code: Option<i64>,
raw_os_message: Option<String>,
kind: ErrorKind,
) -> Self {
Self { raw_code, raw_os_message, kind }
}
#[inline]
pub fn not_supported(&self) -> bool {
matches!(&self.kind, ErrorKind::NotSupported(_))
}
#[inline]
pub fn error_kind(&self) -> ErrorKind {
self.kind
}
#[inline]
pub fn raw_code(&self) -> Option<i64> {
self.raw_code
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(raw_code) = self.raw_code {
write!(f, "[{raw_code:x}] ")?;
}
let msg = if let Some(raw_os_message) = self.raw_os_message.as_ref() {
raw_os_message
} else {
self.kind.as_str()
};
write!(f, "{msg}")
}
}
impl std::error::Error for Error {}
impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Self {
Error { raw_code: None, raw_os_message: None, kind }
}
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum ErrorKind {
NotFound,
InitializationFailed,
BadAccess,
OutOfMemory,
BadAttribute,
BadContext,
BadContextState,
BadConfig,
BadCurrentSurface,
BadDisplay,
BadSurface,
BadPbuffer,
BadPixmap,
BadMatch,
BadParameter,
BadNativePixmap,
BadNativeWindow,
ContextLost,
NotSupported(&'static str),
Misc,
}
impl ErrorKind {
pub(crate) fn as_str(&self) -> &'static str {
use ErrorKind::*;
match *self {
NotFound => "not found",
InitializationFailed => "initialization failed",
BadAccess => "access to the resource failed",
OutOfMemory => "out of memory",
BadAttribute => "an anrecougnized attribute or attribute value was passed",
BadContext => "argument does not name a valid context",
BadContextState => "the context is in a bad state",
BadConfig => "argument does not name a valid config",
BadCurrentSurface => "the current surface of the calling thread is no longer valid",
BadDisplay => "argument does not name a valid display",
BadSurface => "argument does not name a valid surface",
BadPbuffer => "argument does not name a valid pbuffer",
BadPixmap => "argument does not name a valid pixmap",
BadMatch => "arguments are inconsistance",
BadParameter => "one or more argument values are invalid",
BadNativePixmap => "argument does not refer to a valid native pixmap",
BadNativeWindow => "argument does not refer to a valid native window",
ContextLost => "context loss",
NotSupported(reason) => reason,
Misc => "misc platform error",
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}