use std::str::FromStr;
use toml::value::Datetime;
use toml::Value;
use crate::error::{ConferError, Result};
pub fn describe(value: &Value) -> &'static str {
match value {
Value::String(_) => "string",
Value::Integer(_) => "integer",
Value::Float(_) => "float",
Value::Boolean(_) => "boolean",
Value::Datetime(_) => "datetime",
Value::Array(_) => "array",
Value::Table(_) => "table",
}
}
pub fn string(section: &str, key: &str, value: Value) -> Result<String> {
match value {
Value::String(s) => Ok(s),
other => Err(ConferError::type_mismatch(section, key, "string", describe(&other))),
}
}
pub fn integer(section: &str, key: &str, value: Value) -> Result<i64> {
match value {
Value::Integer(v) => Ok(v),
other => Err(ConferError::type_mismatch(section, key, "integer", describe(&other))),
}
}
pub fn float(section: &str, key: &str, value: Value) -> Result<f64> {
match value {
Value::Float(v) => Ok(v),
Value::Integer(v) => Ok(v as f64),
other => Err(ConferError::type_mismatch(section, key, "float", describe(&other))),
}
}
pub fn boolean(section: &str, key: &str, value: Value) -> Result<bool> {
match value {
Value::Boolean(v) => Ok(v),
other => Err(ConferError::type_mismatch(section, key, "boolean", describe(&other))),
}
}
pub fn datetime(section: &str, key: &str, value: Value) -> Result<Datetime> {
match value {
Value::Datetime(dt) => Ok(dt),
Value::String(s) => parse_datetime(section, key, &s),
other => Err(ConferError::type_mismatch(section, key, "datetime", describe(&other))),
}
}
pub fn string_vec(section: &str, key: &str, value: Value) -> Result<Vec<String>> {
to_vec(section, key, value, |section, key, element| match element {
Value::String(s) => Ok(s),
other => Err(element_mismatch(section, key, "string", &other)),
})
}
pub fn integer_vec(section: &str, key: &str, value: Value) -> Result<Vec<i64>> {
to_vec(section, key, value, |section, key, element| match element {
Value::Integer(v) => Ok(v),
other => Err(element_mismatch(section, key, "integer", &other)),
})
}
pub fn float_vec(section: &str, key: &str, value: Value) -> Result<Vec<f64>> {
to_vec(section, key, value, |section, key, element| match element {
Value::Float(v) => Ok(v),
Value::Integer(v) => Ok(v as f64),
other => Err(element_mismatch(section, key, "float", &other)),
})
}
pub fn boolean_vec(section: &str, key: &str, value: Value) -> Result<Vec<bool>> {
to_vec(section, key, value, |section, key, element| match element {
Value::Boolean(v) => Ok(v),
other => Err(element_mismatch(section, key, "boolean", &other)),
})
}
pub fn datetime_vec(section: &str, key: &str, value: Value) -> Result<Vec<Datetime>> {
to_vec(section, key, value, |section, key, element| match element {
Value::Datetime(dt) => Ok(dt),
Value::String(s) => parse_datetime(section, key, &s),
other => Err(element_mismatch(section, key, "datetime", &other)),
})
}
fn parse_datetime(section: &str, key: &str, raw: &str) -> Result<Datetime> {
Datetime::from_str(raw).map_err(|err| {
ConferError::value_parse(section, key, format!("failed to parse datetime: {err}"))
})
}
fn to_vec<T, F>(section: &str, key: &str, value: Value, mut convert: F) -> Result<Vec<T>>
where
F: FnMut(&str, &str, Value) -> Result<T>,
{
match value {
Value::Array(items) => {
let mut out = Vec::with_capacity(items.len());
for (index, item) in items.into_iter().enumerate() {
match convert(section, key, item) {
Ok(v) => out.push(v),
Err(err) => {
return Err(match err {
ConferError::TypeMismatch { .. } | ConferError::ValueParse { .. } => {
annotate_with_index(err, index)
}
other => other,
});
}
}
}
Ok(out)
}
other => Err(ConferError::type_mismatch(section, key, "array", describe(&other))),
}
}
fn element_mismatch(section: &str, key: &str, expected: &'static str, value: &Value) -> ConferError {
ConferError::value_parse(
section,
key,
format!(
"expected array elements of type {expected}, found {}",
describe(value)
),
)
}
fn annotate_with_index(error: ConferError, index: usize) -> ConferError {
match error {
ConferError::ValueParse { section, key, message } => ConferError::ValueParse {
section,
key,
message: format!("{message} (at index {index})"),
},
ConferError::TypeMismatch { section, key, expected, found } => ConferError::TypeMismatch {
section,
key,
expected,
found,
},
other => other,
}
}