mod rv;
mod rv_error;
pub use rv::*;
pub use rv_error::*;
use std::fmt;
use crate::context::Function;
#[derive(Debug)]
pub enum Error {
LibraryLoading(libloading::Error),
Pkcs11(RvError, Function),
NotSupported,
TryFromInt(std::num::TryFromIntError),
TryFromSlice(std::array::TryFromSliceError),
ParseInt(core::num::ParseIntError),
Utf8(std::str::Utf8Error),
NulError(std::ffi::NulError),
NullFunctionPointer,
InvalidValue,
PinNotSet,
AlreadyInitialized,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::LibraryLoading(e) => write!(f, "libloading error ({e})"),
Error::Pkcs11(e, funct) => write!(f, "{funct}: PKCS11 error: {e}"),
Error::NotSupported => write!(f, "Feature not supported"),
Error::TryFromInt(e) => write!(f, "Conversion between integers failed ({e})"),
Error::TryFromSlice(e) => write!(f, "Error converting slice to array ({e})"),
Error::ParseInt(e) => write!(f, "Error parsing string as integer ({e})"),
Error::Utf8(e) => write!(f, "Invalid UTF-8 ({e})"),
Error::NulError(e) => write!(f, "An interior nul byte was found ({e})"),
Error::NullFunctionPointer => write!(f, "Calling a NULL function pointer"),
Error::InvalidValue => write!(f, "The value is not one of the expected options"),
Error::PinNotSet => write!(f, "Pin has not been set before trying to log in"),
Error::AlreadyInitialized => write!(f, "PKCS11 library has already been initialized"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::LibraryLoading(e) => Some(e),
Error::TryFromInt(e) => Some(e),
Error::TryFromSlice(e) => Some(e),
Error::ParseInt(e) => Some(e),
Error::Utf8(e) => Some(e),
Error::NulError(e) => Some(e),
Error::Pkcs11(_, _)
| Error::NotSupported
| Error::NullFunctionPointer
| Error::PinNotSet
| Error::InvalidValue
| Error::AlreadyInitialized => None,
}
}
}
impl From<libloading::Error> for Error {
fn from(err: libloading::Error) -> Error {
Error::LibraryLoading(err)
}
}
impl From<std::num::TryFromIntError> for Error {
fn from(err: std::num::TryFromIntError) -> Error {
Error::TryFromInt(err)
}
}
impl From<std::array::TryFromSliceError> for Error {
fn from(err: std::array::TryFromSliceError) -> Error {
Error::TryFromSlice(err)
}
}
impl From<core::num::ParseIntError> for Error {
fn from(err: core::num::ParseIntError) -> Error {
Error::ParseInt(err)
}
}
impl From<std::str::Utf8Error> for Error {
fn from(err: std::str::Utf8Error) -> Error {
Error::Utf8(err)
}
}
impl From<std::ffi::NulError> for Error {
fn from(err: std::ffi::NulError) -> Error {
Error::NulError(err)
}
}
impl From<std::convert::Infallible> for Error {
fn from(_err: std::convert::Infallible) -> Error {
unreachable!()
}
}
pub type Result<T> = core::result::Result<T, Error>;