diff options
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs index 9e6adc0..1b0edd4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,6 +3,7 @@ use std::error; use std::fmt; +use std::marker; use std::os::raw; use std::str; use std::sync; @@ -46,6 +47,12 @@ impl From<raw::c_int> for Error { } } +impl<'a, T: crate::Device<'a>> From<AuthenticationError<'a, T>> for Error { + fn from(err: AuthenticationError<'a, T>) -> Self { + err.error + } +} + impl From<CommandError> for Error { fn from(err: CommandError) -> Self { Error::CommandError(err) @@ -267,3 +274,52 @@ impl fmt::Display for LibraryError { }) } } + +#[derive(Debug)] +pub struct AuthenticationError<'a, T: device::Device<'a>> { + error: Error, + device: T, + marker: marker::PhantomData<dyn device::Device<'a>>, +} + +impl<'a, T: device::Device<'a>> AuthenticationError<'a, T> { + pub fn new(error: Error, device: T) -> Self { + AuthenticationError { + error, + device, + marker: marker::PhantomData, + } + } + + pub fn as_error(&self) -> &Error { + &self.error + } + + pub fn into_device(self) -> T { + self.device + } + + pub fn map_device<'b, U, F>(self, op: F) -> AuthenticationError<'b, U> + where + U: device::Device<'b>, + F: Fn(T) -> U, + { + AuthenticationError { + error: self.error, + device: op(self.device), + marker: marker::PhantomData, + } + } +} + +impl<'a, T: device::Device<'a>> error::Error for AuthenticationError<'a, T> { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + Some(&self.error) + } +} + +impl<'a, T: device::Device<'a>> fmt::Display for AuthenticationError<'a, T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.error.fmt(f) + } +} |