use std::str::FromStr;
use toml::value::Datetime;
use toml::{Table, Value};
use crate::error::{Error, 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(path: &str, key: &str, value: Value) -> Result<String> {
match value {
Value::String(s) => Ok(s),
other => Err(Error::type_mismatch_error(
path,
key,
"string",
describe(&other),
)),
}
}
pub fn integer(path: &str, key: &str, value: Value) -> Result<i64> {
match value {
Value::Integer(v) => Ok(v),
other => Err(Error::type_mismatch_error(
path,
key,
"integer",
describe(&other),
)),
}
}
pub fn float(path: &str, key: &str, value: Value) -> Result<f64> {
match value {
Value::Float(v) => Ok(v),
Value::Integer(v) => Ok(v as f64),
other => Err(Error::type_mismatch_error(
path,
key,
"float",
describe(&other),
)),
}
}
pub fn boolean(path: &str, key: &str, value: Value) -> Result<bool> {
match value {
Value::Boolean(v) => Ok(v),
other => Err(Error::type_mismatch_error(
path,
key,
"boolean",
describe(&other),
)),
}
}
pub fn datetime(path: &str, key: &str, value: Value) -> Result<Datetime> {
match value {
Value::Datetime(dt) => Ok(dt),
Value::String(s) => parse_datetime(path, key, &s),
other => Err(Error::type_mismatch_error(
path,
key,
"datetime",
describe(&other),
)),
}
}
pub fn table(path: &str, key: &str, value: Value) -> Result<Table> {
match value {
Value::Table(table) => Ok(table),
other => Err(Error::type_mismatch_error(
path,
key,
"table",
describe(&other),
)),
}
}
pub fn array(path: &str, key: &str, value: Value) -> Result<Vec<Value>> {
match value {
Value::Array(items) => Ok(items),
other => Err(Error::type_mismatch_error(
path,
key,
"array",
describe(&other),
)),
}
}
pub fn table_element(path: &str, key: &str, value: Value, index: usize) -> Result<Table> {
match value {
Value::Table(table) => Ok(table),
other => Err(annotate_with_index(
element_mismatch(path, key, "table", &other),
index,
)),
}
}
pub fn string_vec(path: &str, key: &str, value: Value) -> Result<Vec<String>> {
to_vec(path, key, value, |path, key, element| match element {
Value::String(s) => Ok(s),
other => Err(element_mismatch(path, key, "string", &other)),
})
}
pub fn integer_vec(path: &str, key: &str, value: Value) -> Result<Vec<i64>> {
to_vec(path, key, value, |path, key, element| match element {
Value::Integer(v) => Ok(v),
other => Err(element_mismatch(path, key, "integer", &other)),
})
}
pub fn float_vec(path: &str, key: &str, value: Value) -> Result<Vec<f64>> {
to_vec(path, key, value, |path, key, element| match element {
Value::Float(v) => Ok(v),
Value::Integer(v) => Ok(v as f64),
other => Err(element_mismatch(path, key, "float", &other)),
})
}
pub fn boolean_vec(path: &str, key: &str, value: Value) -> Result<Vec<bool>> {
to_vec(path, key, value, |path, key, element| match element {
Value::Boolean(v) => Ok(v),
other => Err(element_mismatch(path, key, "boolean", &other)),
})
}
pub fn datetime_vec(path: &str, key: &str, value: Value) -> Result<Vec<Datetime>> {
to_vec(path, key, value, |path, key, element| match element {
Value::Datetime(dt) => Ok(dt),
Value::String(s) => parse_datetime(path, key, &s),
other => Err(element_mismatch(path, key, "datetime", &other)),
})
}
fn parse_datetime(path: &str, key: &str, raw: &str) -> Result<Datetime> {
Datetime::from_str(raw).map_err(|err| {
Error::value_parse_error(path, key, format!("failed to parse datetime: {err}"))
})
}
fn to_vec<T, F>(path: &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(path, key, item) {
Ok(v) => out.push(v),
Err(err) => {
return Err(annotate_with_index(err, index));
}
}
}
Ok(out)
}
other => Err(Error::type_mismatch_error(
path,
key,
"array",
describe(&other),
)),
}
}
fn element_mismatch(path: &str, key: &str, expected: &'static str, value: &Value) -> Error {
Error::value_parse_error(
path,
key,
format!(
"expected array elements of type {expected}, found {}",
describe(value)
),
)
}
fn annotate_with_index(error: Error, index: usize) -> Error {
match error {
Error::ValueParse {
table_path,
key,
message,
} => Error::ValueParse {
table_path,
key,
message: format!("{message} (at index {index})"),
},
other => other,
}
}