use std;
use capi;
use std::ffi::CStr;
type ErrorInt = i32;
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct PAErr(pub ErrorInt);
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Code {
Ok = 0,
Access,
Command,
Invalid,
Exist,
NoEntity,
ConnectionRefused,
Protocol,
Timeout,
AuthKey,
Internal,
ConnectionTerminated,
Killed,
InvalidServer,
ModInitFailed,
BadState,
NoData,
Version,
TooLarge,
NotSupported,
Unknown,
NoExtension,
Obsolete,
NotImplemented,
Forked,
Io,
Busy,
}
impl PAErr {
pub fn to_string(&self) -> Option<String> {
let ptr = unsafe { capi::pa_strerror(self.0) };
if ptr.is_null() {
return None;
}
Some(unsafe { CStr::from_ptr(ptr).to_string_lossy().into_owned() })
}
}
impl std::fmt::Display for PAErr {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self.to_string() {
Some(s) => write!(f, "{}", s),
None => write!(f, ""),
}
}
}
impl Code {
pub fn to_string(self) -> Option<String> {
PAErr::from(self).to_string()
}
}
impl From<Code> for PAErr {
fn from(c: Code) -> Self {
PAErr(-(c as ErrorInt))
}
}
impl From<PAErr> for Code {
fn from(e: PAErr) -> Self {
let abs = -(e.0);
match abs {
x if x == Code::Ok as ErrorInt => Code::Ok,
x if x == Code::Access as ErrorInt => Code::Access,
x if x == Code::Command as ErrorInt => Code::Command,
x if x == Code::Invalid as ErrorInt => Code::Invalid,
x if x == Code::Exist as ErrorInt => Code::Exist,
x if x == Code::NoEntity as ErrorInt => Code::NoEntity,
x if x == Code::ConnectionRefused as ErrorInt => Code::ConnectionRefused,
x if x == Code::Protocol as ErrorInt => Code::Protocol,
x if x == Code::Timeout as ErrorInt => Code::Timeout,
x if x == Code::AuthKey as ErrorInt => Code::AuthKey,
x if x == Code::Internal as ErrorInt => Code::Internal,
x if x == Code::ConnectionTerminated as ErrorInt => Code::ConnectionTerminated,
x if x == Code::Killed as ErrorInt => Code::Killed,
x if x == Code::InvalidServer as ErrorInt => Code::InvalidServer,
x if x == Code::ModInitFailed as ErrorInt => Code::ModInitFailed,
x if x == Code::BadState as ErrorInt => Code::BadState,
x if x == Code::NoData as ErrorInt => Code::NoData,
x if x == Code::Version as ErrorInt => Code::Version,
x if x == Code::TooLarge as ErrorInt => Code::TooLarge,
x if x == Code::NotSupported as ErrorInt => Code::NotSupported,
x if x == Code::Unknown as ErrorInt => Code::Unknown,
x if x == Code::NoExtension as ErrorInt => Code::NoExtension,
x if x == Code::Obsolete as ErrorInt => Code::Obsolete,
x if x == Code::NotImplemented as ErrorInt => Code::NotImplemented,
x if x == Code::Forked as ErrorInt => Code::Forked,
x if x == Code::Io as ErrorInt => Code::Io,
x if x == Code::Busy as ErrorInt => Code::Busy,
_ => Code::Unknown,
}
}
}