notmuch/
config_pairs.rs

1use std::ops::Drop;
2use std::rc::Rc;
3
4use ffi;
5use utils::ToStr;
6use Database;
7
8#[derive(Debug)]
9pub struct ConfigPairsPtr(*mut ffi::notmuch_config_pairs_t);
10
11#[derive(Clone, Debug)]
12pub struct ConfigPairs {
13    ptr: Rc<ConfigPairsPtr>,
14    _owner: Database,
15}
16
17impl Drop for ConfigPairsPtr {
18    fn drop(&mut self) {
19        unsafe { ffi::notmuch_config_pairs_destroy(self.0) };
20    }
21}
22
23impl ConfigPairs {
24    pub(crate) fn from_ptr(ptr: *mut ffi::notmuch_config_pairs_t, owner: Database) -> ConfigPairs {
25        ConfigPairs {
26            ptr: Rc::new(ConfigPairsPtr(ptr)),
27            _owner: owner,
28        }
29    }
30}
31
32impl Iterator for ConfigPairs {
33    type Item = (String, Option<String>);
34
35    fn next(&mut self) -> Option<Self::Item> {
36        let valid = unsafe { ffi::notmuch_config_pairs_valid(self.ptr.0) };
37
38        if valid == 0 {
39            return None;
40        }
41
42        let (k, v) = unsafe {
43            let k = ffi::notmuch_config_pairs_key(self.ptr.0);
44            let v = ffi::notmuch_config_pairs_value(self.ptr.0);
45
46            ffi::notmuch_config_pairs_move_to_next(self.ptr.0);
47
48            (k, v)
49        };
50
51        let value = if v.is_null() {
52            None
53        } else {
54            Some(v.to_string_lossy().to_string())
55        };
56
57        Some((k.to_string_lossy().to_string(), value))
58    }
59}