use crate::bindings::{
oconfig_item_t, oconfig_value_s__bindgen_ty_1, oconfig_value_t, OCONFIG_TYPE_BOOLEAN,
OCONFIG_TYPE_NUMBER, OCONFIG_TYPE_STRING,
};
use crate::errors::ConfigError;
use std::ffi::CStr;
use std::slice;
#[derive(Debug, PartialEq, Clone)]
pub enum ConfigValue<'a> {
Number(f64),
Boolean(bool),
String(&'a str),
}
#[derive(Debug, PartialEq, Clone)]
pub struct ConfigItem<'a> {
pub key: &'a str,
pub values: Vec<ConfigValue<'a>>,
pub children: Vec<ConfigItem<'a>>,
}
impl ConfigValue<'_> {
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 ConfigItem<'_> {
pub unsafe fn from(item: &oconfig_item_t) -> Result<ConfigItem, ConfigError> {
let key = CStr::from_ptr(item.key)
.to_str()
.map_err(ConfigError::StringDecode)?;
let values = if !item.values.is_null() {
slice::from_raw_parts(item.values, item.values_num.max(0) as usize)
.iter()
.map(|x| ConfigValue::from(x))
.collect::<Result<Vec<_>, _>>()?
} else {
Vec::new()
};
let children = if !item.children.is_null() {
slice::from_raw_parts(item.children, item.children_num.max(0) as usize)
.iter()
.map(|x| ConfigItem::from(x))
.collect::<Result<Vec<_>, _>>()?
} else {
Vec::new()
};
Ok(ConfigItem {
key,
values,
children,
})
}
}