aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.rs
blob: dbabe213a5dc24588d60d4f95d704e3d6fb78a8a (plain) (blame)
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
use std::{
    ffi,
    str,
};

use std::os::unix::ffi::OsStrExt;

use libc;

pub trait NotmuchEnum {
    type NotmuchT;

    fn from_notmuch_t(notmuch_t: Self::NotmuchT) -> Self;
    fn to_notmuch_t(self) -> Self::NotmuchT;
}

pub trait ToCString {
    fn to_cstring(&self) -> Result<ffi::CString, ffi::NulError>;
}

impl<T: ffi::AsOsStr> ToCString for T {
    fn to_cstring(&self) -> Result<ffi::CString, ffi::NulError> {
        self.as_os_str().to_cstring()
    }
}

pub trait ToStr {
    fn to_str<'a>(&self) -> Result<&'a str, str::Utf8Error>;
}

impl ToStr for *const libc::c_char {
    fn to_str<'a>(&self) -> Result<&'a str, str::Utf8Error> {
        str::from_utf8(unsafe {
            ffi::CStr::from_ptr(*self)
        }.to_bytes())
    }
}

pub trait ToStaticStr {
    fn to_static_str(&self) -> Result<&'static str, str::Utf8Error>;
}

impl ToStaticStr for *const libc::c_char {
    fn to_static_str(&self) -> Result<&'static str, str::Utf8Error> {
        str::from_utf8(unsafe {
            ffi::CStr::from_ptr(*self)
        }.to_bytes())
    }
}