conferencier 0.1.0

Minimal, typed TOML configuration for Tokio applications
Documentation
//! Helpers for converting TOML values into strongly typed Rust values.

use std::str::FromStr;

use toml::value::Datetime;
use toml::{Table, Value};

use crate::error::{Error, Result};

/// Human-readable description of a TOML [`Value`] type.
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",
    }
}

/// Converts a TOML value to `String`, producing a type-mismatch error when incompatible.
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),
        )),
    }
}

/// Converts a TOML value to `i64`, producing a type-mismatch error when incompatible.
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),
        )),
    }
}

/// Converts a TOML value to `f64`, accepting integers and floats.
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),
        )),
    }
}

/// Converts a TOML value to `bool`, producing a type-mismatch error when incompatible.
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),
        )),
    }
}

/// Converts a TOML value to [`Datetime`], parsing strings when necessary.
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),
        )),
    }
}

/// Converts a TOML value to a [`Table`], producing a type-mismatch error when incompatible.
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),
        )),
    }
}

/// Converts a TOML value to its array elements without converting them.
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),
        )),
    }
}

/// Converts one element of an array of tables, reporting its index on mismatch.
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,
        )),
    }
}

/// Converts a TOML value to `Vec<String>`, validating element types.
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)),
    })
}

/// Converts a TOML value to `Vec<i64>`, validating element types.
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)),
    })
}

/// Converts a TOML value to `Vec<f64>`, upcasting integer elements when needed.
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)),
    })
}

/// Converts a TOML value to `Vec<bool>`, validating element types.
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)),
    })
}

/// Converts a TOML value to `Vec<Datetime>`, parsing string elements when necessary.
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)),
    })
}

/// Parses a TOML datetime from `raw`, annotating errors with path/key context.
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}"))
    })
}

/// Converts a TOML array to `Vec<T>` using the provided element conversion callback.
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),
        )),
    }
}

/// Builds a [`Error::ValueParse`] describing an invalid array element type.
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)
        ),
    )
}

/// Adds index context to element-related errors to aid debugging.
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,
    }
}