use std::fmt;
#[derive(Debug)]
pub enum GmsslError {
LibraryError(&'static str),
InvalidKey(&'static str),
InvalidInput(&'static str),
IoError(std::io::Error),
VerificationFailed,
DecryptionFailed,
}
impl fmt::Display for GmsslError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
GmsslError::LibraryError(ctx) => write!(f, "GmSSL library error: {}", ctx),
GmsslError::InvalidKey(msg) => write!(f, "Invalid key: {}", msg),
GmsslError::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
GmsslError::IoError(e) => write!(f, "I/O error: {}", e),
GmsslError::VerificationFailed => write!(f, "Verification failed"),
GmsslError::DecryptionFailed => write!(f, "Decryption failed"),
}
}
}
impl std::error::Error for GmsslError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
GmsslError::IoError(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for GmsslError {
fn from(e: std::io::Error) -> Self {
GmsslError::IoError(e)
}
}
#[inline]
pub(crate) fn ok_or_library_error(ret: i32, context: &'static str) -> Result<(), GmsslError> {
if ret == 1 {
Ok(())
} else {
Err(GmsslError::LibraryError(context))
}
}
#[inline]
pub(crate) fn verify_result(ret: i32, _context: &'static str) -> Result<bool, GmsslError> {
Ok(ret == 1)
}