#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#[cfg(feature = "libnv")] pub mod libnv;
#[cfg(feature = "nvpair")] pub mod nvpair;
use std::{borrow::Cow,
ffi::{CStr, CString, NulError},
io};
use quick_error::quick_error;
quick_error! {
#[derive(Debug)]
pub enum NvError {
InvalidString(err: NulError) {
from()
}
NativeError(code: i32) {}
AlreadySet {}
NotFound {}
OutOfMemory {}
Io(err: io::Error) {}
OperationNotSupported {}
InvalidStringEncoding(err: std::str::Utf8Error) {
from()
}
}
}
impl NvError {
pub(crate) fn from_errno(errno: i32) -> Self {
match errno {
libc::ENOENT => NvError::NotFound,
libc::ENOMEM => NvError::OutOfMemory,
libc::EOPNOTSUPP => NvError::OperationNotSupported,
n => NvError::Io(io::Error::from_raw_os_error(n)),
}
}
}
pub type NvResult<T> = Result<T, NvError>;
pub trait IntoCStr<'a> {
fn into_c_str(self) -> NvResult<Cow<'a, CStr>>;
}
impl<'a> IntoCStr<'a> for &'a CStr {
fn into_c_str(self) -> NvResult<Cow<'a, CStr>> { Ok(Cow::from(self)) }
}
impl<'a> IntoCStr<'a> for CString {
fn into_c_str(self) -> NvResult<Cow<'a, CStr>> { Ok(Cow::from(self)) }
}
impl<'a> IntoCStr<'a> for &str {
fn into_c_str(self) -> NvResult<Cow<'a, CStr>> {
CString::new(self).map(Cow::from).map_err(NvError::from)
}
}
impl<'a> IntoCStr<'a> for String {
fn into_c_str(self) -> NvResult<Cow<'a, CStr>> {
CString::new(self).map(Cow::from).map_err(NvError::from)
}
}