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
//! Typed API errors.

use core::any::Any;
use core::convert::Infallible;
use core::fmt;
use core::error::Error as CoreError;

use crate::ffi::Utf8Error;
use alloc::ffi::NulError;
use alloc::string::FromUtf8Error;
pub use null::*;
mod null;


#[derive(Debug)]
pub enum Error<T = ()> {
	Api(T),
	Utf8(Utf8Error),
	FromUtf8(FromUtf8Error),
	CStr(NulError),
	NullPtr(null::NullPtrError),
	#[cfg(feature = "error-ctx")]
	NullPtrCtx(null::ctx::NullPtrError),
}

impl<T: fmt::Display> fmt::Display for Error<T> {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match &self {
			Error::Api(err) => err.fmt(f),
			Error::Utf8(err) => err.fmt(f),
			Error::FromUtf8(err) => err.fmt(f),
			Error::CStr(err) => err.fmt(f),
			Error::NullPtr(err) => err.fmt(f),
			#[cfg(feature = "error-ctx")]
			Error::NullPtrCtx(err) => err.fmt(f),
		}
	}
}

impl<T> Error<T> {
	pub fn from_err<E>(error: Error<E>) -> Self
		where T: From<E> {
		match error {
			Error::Api(err) => Error::Api(err.into()),
			Error::Utf8(err) => Error::Utf8(err),
			Error::FromUtf8(err) => Error::FromUtf8(err),
			Error::CStr(err) => Error::CStr(err),
			Error::NullPtr(err) => Error::NullPtr(err),
			#[cfg(feature = "error-ctx")]
			Error::NullPtrCtx(err) => Error::NullPtrCtx(err),
		}
	}
}


impl<T> From<Utf8Error> for Error<T> {
	fn from(error: Utf8Error) -> Self { Self::Utf8(error) }
}

impl<T> From<FromUtf8Error> for Error<T> {
	fn from(error: FromUtf8Error) -> Self { Self::FromUtf8(error) }
}

impl<T> From<null::NullPtrError> for Error<T> {
	fn from(error: null::NullPtrError) -> Self { Self::NullPtr(error) }
}

impl<T> From<NulError> for Error<T> {
	fn from(error: NulError) -> Self { Self::CStr(error) }
}

impl<T> From<Infallible> for Error<T> {
	fn from(_: Infallible) -> Self { unreachable!() }
}

#[cfg(feature = "error-ctx")]
impl<T> From<null::ctx::NullPtrError> for Error<T> {
	fn from(error: null::ctx::NullPtrError) -> Self { Self::NullPtrCtx(error) }
}


impl<T: Any> CoreError for Error<T> where T: fmt::Display + fmt::Debug {
	fn source(&self) -> Option<&(dyn CoreError + 'static)> {
		match self {
			Error::Utf8(err) => Some(err),
			Error::FromUtf8(err) => Some(err),
			Error::CStr(err) => Some(err),
			Error::NullPtr(err) => Some(err),
			#[cfg(feature = "error-ctx")]
			Error::NullPtrCtx(err) => Some(err),
			Error::Api(err) => (err as &dyn Any).downcast_ref::<&dyn CoreError>().copied(),
		}
	}

	// Removed text from str to do not store this in output binary.
	/// `description()` is deprecated; use `Display`
	fn description(&self) -> &str { "" }
}