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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use bindings::{
    oconfig_item_t, oconfig_value_s__bindgen_ty_1, oconfig_value_t, OCONFIG_TYPE_BOOLEAN,
    OCONFIG_TYPE_NUMBER, OCONFIG_TYPE_STRING,
};
use errors::ConfigError;
use std::ffi::CStr;
use std::slice;

/// A parsed value from the Collectd config
#[derive(Debug, PartialEq, Clone)]
pub enum ConfigValue<'a> {
    /// Numeric value
    Number(f64),

    /// True / false, on / off
    Boolean(bool),

    /// Contents enclosed in quote
    String(&'a str),
}

/// Parsed key, values, children objects from the Collectd config.
#[derive(Debug, PartialEq, Clone)]
pub struct ConfigItem<'a> {
    /// Key of the field, does not have to be unique
    pub key: &'a str,

    /// Values on the same line as the key
    pub values: Vec<ConfigValue<'a>>,

    /// Sub elements
    pub children: Vec<ConfigItem<'a>>,
}

impl<'a> ConfigValue<'a> {
    pub unsafe fn from(value: &oconfig_value_t) -> Result<ConfigValue, ConfigError> {
        match value.value {
            oconfig_value_s__bindgen_ty_1 { string }
                if value.type_ == OCONFIG_TYPE_STRING as i32 =>
            {
                Ok(ConfigValue::String(
                    CStr::from_ptr(string)
                        .to_str()
                        .map_err(ConfigError::StringDecode)?,
                ))
            }
            oconfig_value_s__bindgen_ty_1 { number }
                if value.type_ == OCONFIG_TYPE_NUMBER as i32 =>
            {
                Ok(ConfigValue::Number(number))
            }
            oconfig_value_s__bindgen_ty_1 { boolean }
                if value.type_ == OCONFIG_TYPE_BOOLEAN as i32 =>
            {
                Ok(ConfigValue::Boolean(boolean != 0))
            }
            _ => Err(ConfigError::UnknownType(value.type_)),
        }
    }
}

impl<'a> ConfigItem<'a> {
    pub unsafe fn from<'b>(item: &'b oconfig_item_t) -> Result<ConfigItem<'b>, ConfigError> {
        let key = CStr::from_ptr(item.key)
            .to_str()
            .map_err(ConfigError::StringDecode)?;

        let values: Result<Vec<ConfigValue<'b>>, ConfigError> =
            slice::from_raw_parts(item.values, item.values_num as usize)
                .iter()
                .map(|x| ConfigValue::from(x))
                .collect();

        let children: Result<Vec<ConfigItem<'b>>, ConfigError> =
            slice::from_raw_parts(item.children, item.children_num as usize)
                .iter()
                .map(|x| ConfigItem::from(x))
                .collect();

        Ok(ConfigItem {
            key,
            values: values?,
            children: children?,
        })
    }
}