1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// #![deny(missing_docs)]

//! Rust bindings to Name/Value pairs libraries [libnv](man_page) and [nvpair](zol_nvpair)
//! It kinda acts like `Map<&str, T>` for poor people.
//! Library split into two modules: `libnv` and `nvpairs`. `libnv` is FreeBSD implementation that
//! isn't compatible with `nvpairs` that is Solaris implementation.
//!
//! [man_page]: https://www.freebsd.org/cgi/man.cgi?query=nv
//! [zol_nvpair]: https://github.com/zfsonlinux/zfs/tree/master/module/nvpair

extern crate libc;
#[macro_use] extern crate quick_error;

#[cfg(feature = "libnv")] extern crate libnv_sys;

#[cfg(feature = "nvpair")]
extern crate nvpair_sys;

#[cfg(feature = "libnv")] pub mod libnv;

#[cfg(feature = "nvpair")] pub mod nvpair;

use std::{ffi::NulError, io};
quick_error! {
    #[derive(Debug)]
    /// Error kinds for Name/Value library.
    pub enum NvError {
        /// Name a.k.a. key can't contain NULL byte. You going to get this error if you try so.
        InvalidString(err: NulError) {
            from()
        }
        /// error return by ffi. See libc for more information.
        NativeError(code: i32) {}
        /// Trying to set an error on n/v list that already has error
        AlreadySet {}
        /// No value found for given name.
        NotFound {}
        /// Library failed to allocate.
        OutOfMemory {}
        /// Other IO errors
        Io(err: io::Error) {}
        /// Operation not support on a list given flags used to create the list.
        OperationNotSupported {}
        /// Got non-utf8 string from the library.
        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,
            45 => NvError::OperationNotSupported,
            n => NvError::Io(io::Error::from_raw_os_error(n)),
        }
    }
}

/// Short-cut to Result<T, NvError>.
pub type NvResult<T> = Result<T, NvError>;